1 package org.openecomp.sdc.be.utils;
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import com.fasterxml.jackson.databind.type.MapType;
5 import com.fasterxml.jackson.databind.type.TypeFactory;
9 import static org.assertj.core.api.Assertions.assertThat;
10 import static org.openecomp.sdc.be.utils.FixtureHelpers.fixture;
12 public class JsonTester {
13 private static final ObjectMapper MAPPER = new ObjectMapper();
19 public static <T> void testJson(T object, String fixturePath) throws Exception {
20 testJson(object, fixturePath, MAPPER);
23 @SuppressWarnings("unchecked")
24 public static <T> void testJson(T object, String fixturePath, ObjectMapper mapper) throws Exception {
25 T expectedObject = (T) mapper.readValue(fixture(fixturePath), object.getClass());
26 String expectedJson = mapper.writeValueAsString(expectedObject);
27 String actualJson = mapper.writeValueAsString(object);
29 assertThat(actualJson).isEqualTo(expectedJson);
32 @SuppressWarnings("unchecked")
33 public static <T> void testJsonMap(Map<String, T> map, Class<T> valueClass, String fixturePath, ObjectMapper mapper) throws Exception {
34 MapType mapType = TypeFactory.defaultInstance().constructMapType(Map.class, String.class, valueClass);
35 Map<String, T> expectedObject = mapper.readValue(fixture(fixturePath), mapType);
36 String expectedJson = mapper.writeValueAsString(expectedObject);
38 String actualJson = mapper.writeValueAsString(map);
40 assertThat(actualJson).isEqualTo(expectedJson);