Added oparent to sdc main
[sdc.git] / catalog-dao / src / test / java / org / openecomp / sdc / be / utils / JsonTester.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.openecomp.sdc.be.utils;
22
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.fasterxml.jackson.databind.type.MapType;
25 import com.fasterxml.jackson.databind.type.TypeFactory;
26
27 import java.util.Map;
28
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.openecomp.sdc.be.utils.FixtureHelpers.fixture;
31
32 public class JsonTester {
33     private static final ObjectMapper MAPPER = new ObjectMapper();
34
35     public JsonTester() {
36
37     }
38
39     public static <T> void testJson(T object, String fixturePath) throws Exception {
40         testJson(object, fixturePath, MAPPER);
41     }
42
43     @SuppressWarnings("unchecked")
44     public static <T> void testJson(T object, String fixturePath, ObjectMapper mapper) throws Exception {
45         T expectedObject = (T) mapper.readValue(fixture(fixturePath), object.getClass());
46         String expectedJson = mapper.writeValueAsString(expectedObject);
47         String actualJson = mapper.writeValueAsString(object);
48
49         assertThat(actualJson).isEqualTo(expectedJson);
50     }
51
52     @SuppressWarnings("unchecked")
53     public static <T> void testJsonMap(Map<String, T> map, Class<T> valueClass, String fixturePath, ObjectMapper mapper) throws Exception {
54         MapType mapType = TypeFactory.defaultInstance().constructMapType(Map.class, String.class, valueClass);
55         Map<String, T> expectedObject = mapper.readValue(fixture(fixturePath), mapType);
56         String expectedJson = mapper.writeValueAsString(expectedObject);
57
58         String actualJson = mapper.writeValueAsString(map);
59
60         assertThat(actualJson).isEqualTo(expectedJson);
61     }
62 }