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
index f0cfd1b..76f225d 100644 (file)
-/*
- * ===============================LICENSE_START======================================
- *  dcae-analytics
- * ================================================================================
- *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.
- * ================================================================================
- *  Licensed under the Apache License, Version 2.0 (the "License");
- *  you may not use this file except in compliance with the License.
- *   You may obtain a copy of the License at
- *
- *          http://www.apache.org/licenses/LICENSE-2.0
- *
- *  Unless required by applicable law or agreed to in writing, software
- *  distributed under the License is distributed on an "AS IS" BASIS,
- *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- *  See the License for the specific language governing permissions and
- *  limitations under the License.
- *  ============================LICENSE_END===========================================
- */
-
-package org.openecomp.dcae.apod.analytics.model;
-
-import com.fasterxml.jackson.core.JsonProcessingException;
-import com.fasterxml.jackson.databind.ObjectMapper;
-import com.google.common.base.Suppliers;
-import org.json.JSONException;
-import org.junit.Assert;
-import org.junit.BeforeClass;
-import org.openecomp.dcae.apod.analytics.model.util.json.AnalyticsModelObjectMapperSupplier;
-import org.openecomp.dcae.apod.analytics.test.BaseDCAEAnalyticsUnitTest;
-
-import java.io.IOException;
-import java.io.InputStream;
-
-import static org.hamcrest.CoreMatchers.is;
-import static org.junit.Assert.assertThat;
-
-/**
- * @author Rajiv Singla . Creation Date: 10/17/2016.
- */
-public abstract class BaseAnalyticsModelUnitTest extends BaseDCAEAnalyticsUnitTest {
-
-
-    protected static ObjectMapper objectMapper;
-
-    /**
-     * Before running test cases need to assign object mapper.
-     */
-    @BeforeClass
-    public static void beforeClass() {
-        final AnalyticsModelObjectMapperSupplier analyticsModelObjectMapperSupplier =
-                new AnalyticsModelObjectMapperSupplier();
-        objectMapper = Suppliers.memoize(analyticsModelObjectMapperSupplier).get();
-    }
-
-
-    /**
-     * Deserialize given Json file location to given model class and returns it back without any validation check.
-     *
-     * @param jsonFileLocation Classpath location of the json file
-     * @param modelClass       Model Class type
-     * @param <T>              Json Model Type
-     * @return                  Deserialized Model Object
-     */
-    public static <T> T deserializeJsonFileToModel(String jsonFileLocation, Class<T> modelClass) {
-        final InputStream jsonFileInputStream =
-                BaseDCAEAnalyticsUnitTest.class.getClassLoader().getResourceAsStream(jsonFileLocation);
-        Assert.assertNotNull("Json File Location must be valid", jsonFileInputStream);
-        try {
-            return objectMapper.readValue(jsonFileInputStream, modelClass);
-        } catch (IOException ex) {
-            LOG.error("Error while doing assert Json for fileLocation: {}, modelClass: {}, Exception {}",
-                    jsonFileLocation, modelClass, ex);
-            throw new RuntimeException(ex);
-        } finally {
-            try {
-                jsonFileInputStream.close();
-            } catch (IOException e) {
-                LOG.error("Error while closing input stream at file location: {}", jsonFileLocation);
-                throw new RuntimeException(e);
-            }
-        }
-    }
-
-    /**
-     * Deserialize given Json file location to given model class and then validates deserialization by comparing it
-     * with given expected Object.
-     *
-     * @param jsonFileLocation   Classpath location of the json file
-     * @param modelClass         Model Class type
-     * @param expectedJsonObject Expected Json Object
-     * @param <T>                Json Model Type
-     * @return deserialized actual value if expected Json Object matches deserialized object
-     */
-    public static <T> T assertJsonDeserialization(String jsonFileLocation, Class<T> modelClass, T expectedJsonObject) {
-        final T actualValue = deserializeJsonFileToModel(jsonFileLocation, modelClass);
-        assertThat(actualValue, is(expectedJsonObject));
-        return actualValue;
-    }
-
-    /**
-     * Serialize model to json.
-     * @param model model
-     * @return json
-     * @throws JsonProcessingException when fails to process object
-     */
-    public static String serializeModelToJson(Object model) throws JsonProcessingException {
-        return objectMapper.writeValueAsString(model);
-    }
-
-    /**
-     * Converts given model to json string and compare it with json present at given file location.
-     *
-     * @param model                    Model which needs to be compared
-     * @param expectedJsonFileLocation Location of file containing expected json string
-     *
-     * @return If assertion passes returns the input model
-     */
-    public static <T> T assertJsonSerialization(T model, String expectedJsonFileLocation) {
-        try {
-            final String actualModelString = serializeModelToJson(model);
-            final String expectedModelString = fromStream(expectedJsonFileLocation);
-            assertJson(expectedModelString, actualModelString);
-            return model;
-        } catch (IOException | JSONException ex) {
-            LOG.error("Error while doing assert Json serialization Assertion: model: {}, "
-                + "expected Json File Location: {}, Exception {}", model, expectedJsonFileLocation, ex);
-            throw new RuntimeException(ex);
-        }
-    }
-
-
-    /**
-     * Checks both serialization and deserialization.
-     * </p>
-     * First checks deserialization and then serialize the deserialized object back to json
-     * and check if matches the given json file location string
-     *
-     * @param jsonFileLocation   Classpath location of the json file
-     * @param modelClass         Class type
-     * @param <T>                Json Model Type
-     *
-     * @return If assertion passes, returns deserialized object
-     */
-
-    public static <T> T assertJsonConversions(String jsonFileLocation, Class<T> modelClass) {
-        //first check deserialization
-        final T actualValue = deserializeJsonFileToModel(jsonFileLocation, modelClass);
-        //then check serialization
-        assertJsonSerialization(actualValue, jsonFileLocation);
-
-        return actualValue;
-    }
-}
+/*\r
+ * ===============================LICENSE_START======================================\r
+ *  dcae-analytics\r
+ * ================================================================================\r
+ *    Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
+ * ================================================================================\r
+ *  Licensed under the Apache License, Version 2.0 (the "License");\r
+ *  you may not use this file except in compliance with the License.\r
+ *   You may obtain a copy of the License at\r
+ *\r
+ *          http://www.apache.org/licenses/LICENSE-2.0\r
+ *\r
+ *  Unless required by applicable law or agreed to in writing, software\r
+ *  distributed under the License is distributed on an "AS IS" BASIS,\r
+ *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
+ *  See the License for the specific language governing permissions and\r
+ *  limitations under the License.\r
+ *  ============================LICENSE_END===========================================\r
+ */\r
+\r
+package org.openecomp.dcae.apod.analytics.model;\r
+\r
+import com.fasterxml.jackson.core.JsonProcessingException;\r
+import com.fasterxml.jackson.databind.ObjectMapper;\r
+import com.google.common.base.Suppliers;\r
+import org.json.JSONException;\r
+import org.junit.Assert;\r
+import org.junit.BeforeClass;\r
+import org.openecomp.dcae.apod.analytics.model.util.json.AnalyticsModelObjectMapperSupplier;\r
+import org.openecomp.dcae.apod.analytics.test.BaseDCAEAnalyticsUnitTest;\r
+\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+\r
+import static org.hamcrest.CoreMatchers.is;\r
+import static org.junit.Assert.assertThat;\r
+\r
+/**\r
+ * @author Rajiv Singla . Creation Date: 10/17/2016.\r
+ */\r
+public abstract class BaseAnalyticsModelUnitTest extends BaseDCAEAnalyticsUnitTest {\r
+\r
+\r
+    protected static ObjectMapper objectMapper;\r
+\r
+    /**\r
+     * Before running test cases need to assign object mapper.\r
+     */\r
+    @BeforeClass\r
+    public static void beforeClass() {\r
+        final AnalyticsModelObjectMapperSupplier analyticsModelObjectMapperSupplier =\r
+                new AnalyticsModelObjectMapperSupplier();\r
+        objectMapper = Suppliers.memoize(analyticsModelObjectMapperSupplier).get();\r
+    }\r
+\r
+\r
+    /**\r
+     * Deserialize given Json file location to given model class and returns it back without any validation check.\r
+     *\r
+     * @param jsonFileLocation Classpath location of the json file\r
+     * @param modelClass       Model Class type\r
+     * @param <T>              Json Model Type\r
+     * @return                  Deserialized Model Object\r
+     */\r
+    public static <T> T deserializeJsonFileToModel(String jsonFileLocation, Class<T> modelClass) {\r
+        final InputStream jsonFileInputStream =\r
+                BaseDCAEAnalyticsUnitTest.class.getClassLoader().getResourceAsStream(jsonFileLocation);\r
+        Assert.assertNotNull("Json File Location must be valid", jsonFileInputStream);\r
+        try {\r
+            return objectMapper.readValue(jsonFileInputStream, modelClass);\r
+        } catch (IOException ex) {\r
+            LOG.error("Error while doing assert Json for fileLocation: {}, modelClass: {}, Exception {}",\r
+                    jsonFileLocation, modelClass, ex);\r
+            throw new RuntimeException(ex);\r
+        } finally {\r
+            try {\r
+                jsonFileInputStream.close();\r
+            } catch (IOException e) {\r
+                LOG.error("Error while closing input stream at file location: {}", jsonFileLocation);\r
+                throw new RuntimeException(e);\r
+            }\r
+        }\r
+    }\r
+\r
+    /**\r
+     * Deserialize given Json file location to given model class and then validates deserialization by comparing it\r
+     * with given expected Object.\r
+     *\r
+     * @param jsonFileLocation   Classpath location of the json file\r
+     * @param modelClass         Model Class type\r
+     * @param expectedJsonObject Expected Json Object\r
+     * @param <T>                Json Model Type\r
+     * @return deserialized actual value if expected Json Object matches deserialized object\r
+     */\r
+    public static <T> T assertJsonDeserialization(String jsonFileLocation, Class<T> modelClass, T expectedJsonObject) {\r
+        final T actualValue = deserializeJsonFileToModel(jsonFileLocation, modelClass);\r
+        assertThat(actualValue, is(expectedJsonObject));\r
+        return actualValue;\r
+    }\r
+\r
+    /**\r
+     * Serialize model to json.\r
+     * @param model model\r
+     * @return json\r
+     * @throws JsonProcessingException when fails to process object\r
+     */\r
+    public static String serializeModelToJson(Object model) throws JsonProcessingException {\r
+        return objectMapper.writeValueAsString(model);\r
+    }\r
+\r
+    /**\r
+     * Converts given model to json string and compare it with json present at given file location.\r
+     *\r
+     * @param model                    Model which needs to be compared\r
+     * @param expectedJsonFileLocation Location of file containing expected json string\r
+     *\r
+     * @return If assertion passes returns the input model\r
+     */\r
+    public static <T> T assertJsonSerialization(T model, String expectedJsonFileLocation) {\r
+        try {\r
+            final String actualModelString = serializeModelToJson(model);\r
+            final String expectedModelString = fromStream(expectedJsonFileLocation);\r
+            assertJson(expectedModelString, actualModelString);\r
+            return model;\r
+        } catch (IOException | JSONException ex) {\r
+            LOG.error("Error while doing assert Json serialization Assertion: model: {}, "\r
+                + "expected Json File Location: {}, Exception {}", model, expectedJsonFileLocation, ex);\r
+            throw new RuntimeException(ex);\r
+        }\r
+    }\r
+\r
+\r
+    /**\r
+     * Checks both serialization and deserialization.\r
+     * </p>\r
+     * First checks deserialization and then serialize the deserialized object back to json\r
+     * and check if matches the given json file location string\r
+     *\r
+     * @param jsonFileLocation   Classpath location of the json file\r
+     * @param modelClass         Class type\r
+     * @param <T>                Json Model Type\r
+     *\r
+     * @return If assertion passes, returns deserialized object\r
+     */\r
+\r
+    public static <T> T assertJsonConversions(String jsonFileLocation, Class<T> modelClass) {\r
+        //first check deserialization\r
+        final T actualValue = deserializeJsonFileToModel(jsonFileLocation, modelClass);\r
+        //then check serialization\r
+        assertJsonSerialization(actualValue, jsonFileLocation);\r
+\r
+        return actualValue;\r
+    }\r
+}\r