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