2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.be.utils;
23 import com.fasterxml.jackson.databind.ObjectMapper;
24 import com.fasterxml.jackson.databind.type.MapType;
25 import com.fasterxml.jackson.databind.type.TypeFactory;
29 import static org.assertj.core.api.Assertions.assertThat;
30 import static org.openecomp.sdc.be.utils.FixtureHelpers.fixture;
32 public class JsonTester {
33 private static final ObjectMapper MAPPER = new ObjectMapper();
39 public static <T> void testJson(T object, String fixturePath) throws Exception {
40 testJson(object, fixturePath, MAPPER);
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);
49 assertThat(actualJson).isEqualTo(expectedJson);
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);
58 String actualJson = mapper.writeValueAsString(map);
60 assertThat(actualJson).isEqualTo(expectedJson);