JsonHelper.java

  1. package com.syntifi.near.api.json;

  2. import com.fasterxml.jackson.core.JsonProcessingException;
  3. import com.fasterxml.jackson.databind.JsonMappingException;
  4. import com.fasterxml.jackson.databind.ObjectMapper;
  5. import com.syntifi.near.api.service.NearObjectMapper;

  6. import java.io.ByteArrayOutputStream;
  7. import java.io.File;
  8. import java.io.FileInputStream;
  9. import java.io.IOException;
  10. import java.io.InputStream;

  11. /**
  12.  * Basic support functionality for test case classes
  13.  *
  14.  * @author Alexandre Carvalho
  15.  * @author Andre Bertolace
  16.  * @since 0.0.1
  17.  */
  18. public final class JsonHelper {
  19.     public static final ObjectMapper OBJECT_MAPPER = new NearObjectMapper();

  20.     /**
  21.      * Loads test json from resources
  22.      *
  23.      * @param file file to load from
  24.      * @return file content
  25.      * @throws IOException thrown if error reading file
  26.      */
  27.     public static String loadJsonFromFile(File file) throws IOException {
  28.         String fileJson;
  29.         ByteArrayOutputStream baos = new ByteArrayOutputStream();

  30.         try (InputStream is = new FileInputStream(file)) {
  31.             // copy stream
  32.             byte[] buffer = new byte[1024];
  33.             int bytesRead;
  34.             while ((bytesRead = is.read(buffer)) != -1) {
  35.                 baos.write(buffer, 0, bytesRead);
  36.             }

  37.             fileJson = baos.toString();
  38.         }
  39.         return fileJson;
  40.     }

  41.     /**
  42.      * Loads test json from resources
  43.      *
  44.      * @param filename filename to load
  45.      * @return file content
  46.      * @throws IOException thrown if error reading file
  47.      */
  48.     public static String loadJsonFromResourceFile(String filename) throws IOException {
  49.         String fileJson;
  50.         ByteArrayOutputStream baos = new ByteArrayOutputStream();

  51.         try (InputStream is = JsonHelper.class.getClassLoader().getResourceAsStream(filename)) {
  52.             // copy stream
  53.             byte[] buffer = new byte[1024];
  54.             int bytesRead;
  55.             while (true) {
  56.                 assert is != null;
  57.                 if ((bytesRead = is.read(buffer)) == -1) break;
  58.                 baos.write(buffer, 0, bytesRead);
  59.             }

  60.             fileJson = baos.toString();
  61.         }
  62.         return fileJson;
  63.     }

  64.     /**
  65.      * Prettifies json for assertion consistency
  66.      *
  67.      * @param json json string to prettify
  68.      * @return prettified json
  69.      * @throws JsonMappingException thrown if error mapping json
  70.      * @throws JsonProcessingException thrown if error processing json
  71.      */
  72.     public static String getPrettyJson(String json) throws JsonMappingException, JsonProcessingException {
  73.         Object jsonObject = OBJECT_MAPPER.readValue(json, Object.class);
  74.         return getPrettyJson(jsonObject);
  75.     }

  76.     /**
  77.      * Prettifies json for assertion consistency
  78.      *
  79.      * @param jsonObject object to serialize and prettify
  80.      * @return prettified json
  81.      * @throws JsonMappingException thrown if error mapping json
  82.      * @throws JsonProcessingException thrown if error processing json
  83.      */
  84.     public static String getPrettyJson(Object jsonObject) throws JsonProcessingException {
  85.         return OBJECT_MAPPER.writerWithDefaultPrettyPrinter().writeValueAsString(jsonObject);
  86.     }
  87. }