2 * ===============================LICENSE_START======================================
4 * ================================================================================
5 * Copyright © 2017 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.dcae.apod.analytics.model;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.ObjectMapper;
25 import com.google.common.base.Suppliers;
26 import org.json.JSONException;
27 import org.junit.Assert;
28 import org.junit.BeforeClass;
29 import org.openecomp.dcae.apod.analytics.model.util.json.AnalyticsModelObjectMapperSupplier;
30 import org.openecomp.dcae.apod.analytics.test.BaseDCAEAnalyticsUnitTest;
32 import java.io.IOException;
33 import java.io.InputStream;
35 import static org.hamcrest.CoreMatchers.is;
36 import static org.junit.Assert.assertThat;
39 * @author Rajiv Singla . Creation Date: 10/17/2016.
41 public abstract class BaseAnalyticsModelUnitTest extends BaseDCAEAnalyticsUnitTest {
44 protected static ObjectMapper objectMapper;
47 * Before running test cases need to assign object mapper.
50 public static void beforeClass() {
51 final AnalyticsModelObjectMapperSupplier analyticsModelObjectMapperSupplier =
52 new AnalyticsModelObjectMapperSupplier();
53 objectMapper = Suppliers.memoize(analyticsModelObjectMapperSupplier).get();
58 * Deserialize given Json file location to given model class and returns it back without any validation check.
60 * @param jsonFileLocation Classpath location of the json file
61 * @param modelClass Model Class type
62 * @param <T> Json Model Type
63 * @return Deserialized Model Object
65 public static <T> T deserializeJsonFileToModel(String jsonFileLocation, Class<T> modelClass) {
66 final InputStream jsonFileInputStream =
67 BaseDCAEAnalyticsUnitTest.class.getClassLoader().getResourceAsStream(jsonFileLocation);
68 Assert.assertNotNull("Json File Location must be valid", jsonFileInputStream);
70 return objectMapper.readValue(jsonFileInputStream, modelClass);
71 } catch (IOException ex) {
72 LOG.error("Error while doing assert Json for fileLocation: {}, modelClass: {}, Exception {}",
73 jsonFileLocation, modelClass, ex);
74 throw new RuntimeException(ex);
77 jsonFileInputStream.close();
78 } catch (IOException e) {
79 LOG.error("Error while closing input stream at file location: {}", jsonFileLocation);
80 throw new RuntimeException(e);
86 * Deserialize given Json file location to given model class and then validates deserialization by comparing it
87 * with given expected Object.
89 * @param jsonFileLocation Classpath location of the json file
90 * @param modelClass Model Class type
91 * @param expectedJsonObject Expected Json Object
92 * @param <T> Json Model Type
93 * @return deserialized actual value if expected Json Object matches deserialized object
95 public static <T> T assertJsonDeserialization(String jsonFileLocation, Class<T> modelClass, T expectedJsonObject) {
96 final T actualValue = deserializeJsonFileToModel(jsonFileLocation, modelClass);
97 assertThat(actualValue, is(expectedJsonObject));
102 * Serialize model to json.
105 * @throws JsonProcessingException when fails to process object
107 public static String serializeModelToJson(Object model) throws JsonProcessingException {
108 return objectMapper.writeValueAsString(model);
112 * Converts given model to json string and compare it with json present at given file location.
114 * @param model Model which needs to be compared
115 * @param expectedJsonFileLocation Location of file containing expected json string
117 * @return If assertion passes returns the input model
119 public static <T> T assertJsonSerialization(T model, String expectedJsonFileLocation) {
121 final String actualModelString = serializeModelToJson(model);
122 final String expectedModelString = fromStream(expectedJsonFileLocation);
123 assertJson(expectedModelString, actualModelString);
125 } catch (IOException | JSONException ex) {
126 LOG.error("Error while doing assert Json serialization Assertion: model: {}, "
127 + "expected Json File Location: {}, Exception {}", model, expectedJsonFileLocation, ex);
128 throw new RuntimeException(ex);
134 * Checks both serialization and deserialization.
136 * First checks deserialization and then serialize the deserialized object back to json
137 * and check if matches the given json file location string
139 * @param jsonFileLocation Classpath location of the json file
140 * @param modelClass Class type
141 * @param <T> Json Model Type
143 * @return If assertion passes, returns deserialized object
146 public static <T> T assertJsonConversions(String jsonFileLocation, Class<T> modelClass) {
147 //first check deserialization
148 final T actualValue = deserializeJsonFileToModel(jsonFileLocation, modelClass);
149 //then check serialization
150 assertJsonSerialization(actualValue, jsonFileLocation);