Fixing checkstyle and javadoc errors
[dcaegen2/analytics/tca.git] / dcae-analytics-model / src / test / java / org / openecomp / dcae / apod / analytics / model / BaseAnalyticsModelUnitTest.java
1 /*
2  * ===============================LICENSE_START======================================
3  *  dcae-analytics
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
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.dcae.apod.analytics.model;
22
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;
31
32 import java.io.IOException;
33 import java.io.InputStream;
34
35 import static org.hamcrest.CoreMatchers.is;
36 import static org.junit.Assert.assertThat;
37
38 /**
39  * @author Rajiv Singla . Creation Date: 10/17/2016.
40  */
41 public abstract class BaseAnalyticsModelUnitTest extends BaseDCAEAnalyticsUnitTest {
42
43
44     protected static ObjectMapper objectMapper;
45
46     /**
47      * Before running test cases need to assign object mapper.
48      */
49     @BeforeClass
50     public static void beforeClass() {
51         final AnalyticsModelObjectMapperSupplier analyticsModelObjectMapperSupplier =
52                 new AnalyticsModelObjectMapperSupplier();
53         objectMapper = Suppliers.memoize(analyticsModelObjectMapperSupplier).get();
54     }
55
56
57     /**
58      * Deserialize given Json file location to given model class and returns it back without any validation check.
59      *
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
64      */
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);
69         try {
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);
75         } finally {
76             try {
77                 jsonFileInputStream.close();
78             } catch (IOException e) {
79                 LOG.error("Error while closing input stream at file location: {}", jsonFileLocation);
80                 throw new RuntimeException(e);
81             }
82         }
83     }
84
85     /**
86      * Deserialize given Json file location to given model class and then validates deserialization by comparing it
87      * with given expected Object.
88      *
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
94      */
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));
98         return actualValue;
99     }
100
101     /**
102      * Serialize model to json.
103      * @param model model
104      * @return json
105      * @throws JsonProcessingException when fails to process object
106      */
107     public static String serializeModelToJson(Object model) throws JsonProcessingException {
108         return objectMapper.writeValueAsString(model);
109     }
110
111     /**
112      * Converts given model to json string and compare it with json present at given file location.
113      *
114      * @param model                    Model which needs to be compared
115      * @param expectedJsonFileLocation Location of file containing expected json string
116      *
117      * @return If assertion passes returns the input model
118      */
119     public static <T> T assertJsonSerialization(T model, String expectedJsonFileLocation) {
120         try {
121             final String actualModelString = serializeModelToJson(model);
122             final String expectedModelString = fromStream(expectedJsonFileLocation);
123             assertJson(expectedModelString, actualModelString);
124             return model;
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);
129         }
130     }
131
132
133     /**
134      * Checks both serialization and deserialization.
135      * </p>
136      * First checks deserialization and then serialize the deserialized object back to json
137      * and check if matches the given json file location string
138      *
139      * @param jsonFileLocation   Classpath location of the json file
140      * @param modelClass         Class type
141      * @param <T>                Json Model Type
142      *
143      * @return If assertion passes, returns deserialized object
144      */
145
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);
151
152         return actualValue;
153     }
154 }