re base code
[sdc.git] / catalog-dao / src / test / java / org / openecomp / sdc / be / utils / JsonTester.java
1 package org.openecomp.sdc.be.utils;
2
3 import com.fasterxml.jackson.databind.ObjectMapper;
4 import com.fasterxml.jackson.databind.type.MapType;
5 import com.fasterxml.jackson.databind.type.TypeFactory;
6
7 import java.util.Map;
8
9 import static org.assertj.core.api.Assertions.assertThat;
10 import static org.openecomp.sdc.be.utils.FixtureHelpers.fixture;
11
12 public class JsonTester {
13     private static final ObjectMapper MAPPER = new ObjectMapper();
14
15     public JsonTester() {
16
17     }
18
19     public static <T> void testJson(T object, String fixturePath) throws Exception {
20         testJson(object, fixturePath, MAPPER);
21     }
22
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);
28
29         assertThat(actualJson).isEqualTo(expectedJson);
30     }
31
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);
37
38         String actualJson = mapper.writeValueAsString(map);
39
40         assertThat(actualJson).isEqualTo(expectedJson);
41     }
42 }