2 * ===============================LICENSE_START======================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============================LICENSE_END===========================================
\r
21 package org.openecomp.dcae.apod.analytics.model;
\r
23 import com.fasterxml.jackson.core.JsonProcessingException;
\r
24 import com.fasterxml.jackson.databind.ObjectMapper;
\r
25 import com.google.common.base.Suppliers;
\r
26 import org.json.JSONException;
\r
27 import org.junit.Assert;
\r
28 import org.junit.BeforeClass;
\r
29 import org.openecomp.dcae.apod.analytics.model.util.json.AnalyticsModelObjectMapperSupplier;
\r
30 import org.openecomp.dcae.apod.analytics.test.BaseDCAEAnalyticsUnitTest;
\r
32 import java.io.IOException;
\r
33 import java.io.InputStream;
\r
35 import static org.hamcrest.CoreMatchers.is;
\r
36 import static org.junit.Assert.assertThat;
\r
39 * @author Rajiv Singla . Creation Date: 10/17/2016.
\r
41 public abstract class BaseAnalyticsModelUnitTest extends BaseDCAEAnalyticsUnitTest {
\r
44 protected static ObjectMapper objectMapper;
\r
47 * Before running test cases need to assign object mapper.
\r
50 public static void beforeClass() {
\r
51 final AnalyticsModelObjectMapperSupplier analyticsModelObjectMapperSupplier =
\r
52 new AnalyticsModelObjectMapperSupplier();
\r
53 objectMapper = Suppliers.memoize(analyticsModelObjectMapperSupplier).get();
\r
58 * Deserialize given Json file location to given model class and returns it back without any validation check.
\r
60 * @param jsonFileLocation Classpath location of the json file
\r
61 * @param modelClass Model Class type
\r
62 * @param <T> Json Model Type
\r
63 * @return Deserialized Model Object
\r
65 public static <T> T deserializeJsonFileToModel(String jsonFileLocation, Class<T> modelClass) {
\r
66 final InputStream jsonFileInputStream =
\r
67 BaseDCAEAnalyticsUnitTest.class.getClassLoader().getResourceAsStream(jsonFileLocation);
\r
68 Assert.assertNotNull("Json File Location must be valid", jsonFileInputStream);
\r
70 return objectMapper.readValue(jsonFileInputStream, modelClass);
\r
71 } catch (IOException ex) {
\r
72 LOG.error("Error while doing assert Json for fileLocation: {}, modelClass: {}, Exception {}",
\r
73 jsonFileLocation, modelClass, ex);
\r
74 throw new RuntimeException(ex);
\r
77 jsonFileInputStream.close();
\r
78 } catch (IOException e) {
\r
79 LOG.error("Error while closing input stream at file location: {}", jsonFileLocation);
\r
80 throw new RuntimeException(e);
\r
86 * Deserialize given Json file location to given model class and then validates deserialization by comparing it
\r
87 * with given expected Object.
\r
89 * @param jsonFileLocation Classpath location of the json file
\r
90 * @param modelClass Model Class type
\r
91 * @param expectedJsonObject Expected Json Object
\r
92 * @param <T> Json Model Type
\r
93 * @return deserialized actual value if expected Json Object matches deserialized object
\r
95 public static <T> T assertJsonDeserialization(String jsonFileLocation, Class<T> modelClass, T expectedJsonObject) {
\r
96 final T actualValue = deserializeJsonFileToModel(jsonFileLocation, modelClass);
\r
97 assertThat(actualValue, is(expectedJsonObject));
\r
102 * Serialize model to json.
\r
103 * @param model model
\r
105 * @throws JsonProcessingException when fails to process object
\r
107 public static String serializeModelToJson(Object model) throws JsonProcessingException {
\r
108 return objectMapper.writeValueAsString(model);
\r
112 * Converts given model to json string and compare it with json present at given file location.
\r
114 * @param model Model which needs to be compared
\r
115 * @param expectedJsonFileLocation Location of file containing expected json string
\r
117 * @return If assertion passes returns the input model
\r
119 public static <T> T assertJsonSerialization(T model, String expectedJsonFileLocation) {
\r
121 final String actualModelString = serializeModelToJson(model);
\r
122 final String expectedModelString = fromStream(expectedJsonFileLocation);
\r
123 assertJson(expectedModelString, actualModelString);
\r
125 } catch (IOException | JSONException ex) {
\r
126 LOG.error("Error while doing assert Json serialization Assertion: model: {}, "
\r
127 + "expected Json File Location: {}, Exception {}", model, expectedJsonFileLocation, ex);
\r
128 throw new RuntimeException(ex);
\r
134 * Checks both serialization and deserialization.
\r
136 * First checks deserialization and then serialize the deserialized object back to json
\r
137 * and check if matches the given json file location string
\r
139 * @param jsonFileLocation Classpath location of the json file
\r
140 * @param modelClass Class type
\r
141 * @param <T> Json Model Type
\r
143 * @return If assertion passes, returns deserialized object
\r
146 public static <T> T assertJsonConversions(String jsonFileLocation, Class<T> modelClass) {
\r
147 //first check deserialization
\r
148 final T actualValue = deserializeJsonFileToModel(jsonFileLocation, modelClass);
\r
149 //then check serialization
\r
150 assertJsonSerialization(actualValue, jsonFileLocation);
\r
152 return actualValue;
\r