Sonar Critical Fix
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-tca / src / test / java / org / openecomp / dcae / apod / analytics / cdap / tca / BaseAnalyticsCDAPTCAUnitTest.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.cdap.tca;\r
22 \r
23 import co.cask.cdap.api.flow.flowlet.AbstractFlowlet;\r
24 import co.cask.cdap.api.flow.flowlet.FlowletContext;\r
25 import co.cask.cdap.internal.flow.DefaultFlowletConfigurer;\r
26 import com.fasterxml.jackson.core.type.TypeReference;\r
27 import com.fasterxml.jackson.databind.ObjectMapper;\r
28 import com.google.common.base.Suppliers;\r
29 import org.junit.Assert;\r
30 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCAPolicyPreferences;\r
31 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCATestAppConfig;\r
32 import org.openecomp.dcae.apod.analytics.cdap.tca.settings.TCATestAppPreferences;\r
33 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener;\r
34 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;\r
35 import org.openecomp.dcae.apod.analytics.model.util.AnalyticsModelIOUtils;\r
36 import org.openecomp.dcae.apod.analytics.model.util.json.AnalyticsModelObjectMapperSupplier;\r
37 import org.openecomp.dcae.apod.analytics.test.BaseDCAEAnalyticsUnitTest;\r
38 \r
39 import java.io.IOException;\r
40 import java.io.InputStream;\r
41 import java.util.HashMap;\r
42 import java.util.LinkedHashMap;\r
43 import java.util.List;\r
44 import java.util.Map;\r
45 import java.util.Properties;\r
46 \r
47 import static org.hamcrest.CoreMatchers.is;\r
48 import static org.junit.Assert.assertThat;\r
49 import static org.mockito.Mockito.mock;\r
50 import static org.mockito.Mockito.when;\r
51 \r
52 /**\r
53  * @author Rajiv Singla . Creation Date: 10/25/2016.\r
54  */\r
55 public abstract class BaseAnalyticsCDAPTCAUnitTest extends BaseDCAEAnalyticsUnitTest {\r
56 \r
57     /**\r
58      * Object mapper to be used for all TCA Json Parsing\r
59      */\r
60     protected static final ObjectMapper ANALYTICS_MODEL_OBJECT_MAPPER =\r
61             Suppliers.memoize(new AnalyticsModelObjectMapperSupplier()).get();\r
62 \r
63     protected static final String TCA_POLICY_JSON_FILE_LOCATION = "data/json/policy/tca_policy.json";\r
64     protected static final String CEF_MESSAGES_JSON_FILE_LOCATION = "data/json/cef/cef_messages.json";\r
65     protected static final String CEF_MESSAGE_JSON_FILE_LOCATION = "data/json/cef/cef_message.json";\r
66     protected static final String CEF_MESSAGE_WITH_THRESHOLD_VIOLATION_JSON_FILE_LOCATION =\r
67             "data/json/cef/cef_message_with_threshold_violation.json";\r
68     protected static final String TCA_APP_CONFIG_FILE_LOCATION = "data/json/config/controller_app_config.json";\r
69     protected static final String TCA_ALERT_JSON_FILE_LOCATION = "data/json/facade/tca_ves_cef_response.json";\r
70 \r
71 \r
72     protected static final String TCA_CONTROLLER_POLICY_FILE_LOCATION =\r
73             "data/properties/tca_controller_policy.properties";\r
74 \r
75     protected static final String TCA_CONTROLLER_POLICY_FROM_JSON_FILE_LOCATION =\r
76             "data/properties/tca_controller_policy_from_json.properties";\r
77 \r
78 \r
79     protected static final String TCA_TEST_APP_CONFIG_NAME = "testTCAAppName";\r
80     protected static final String TCA_TEST_APP_CONFIG_DESCRIPTION = "testTCAAppDescription";\r
81     protected static final String TCA_TEST_APP_CONFIG_SUBSCRIBER_OUTPUT_STREAM_NAME =\r
82             "testTcaSubscriberOutputStreamName";\r
83     protected static final String TCA_TEST_APP_CONFIG_VES_ALERT_TABLE_NAME = "testTcaVESAlertsTableName";\r
84     protected static final String TCA_TEST_APP_CONFIG_VES_MESSAGE_STATUS_TABLE_NAME =\r
85             "testTcaVESMessageStatusTableName";\r
86 \r
87 \r
88     /**\r
89      * Provides TCA Policy that can be used for testing\r
90      *\r
91      * @return test TCA Policy Object\r
92      */\r
93     protected static TCAPolicy getSampleTCAPolicy() {\r
94         return deserializeJsonFileToModel(TCA_POLICY_JSON_FILE_LOCATION, TCAPolicy.class);\r
95     }\r
96 \r
97     /**\r
98      * Provides TCA Policy that can be used for testing\r
99      *\r
100      * @return test {@link TCAPolicyPreferences}\r
101      */\r
102     protected static TCAPolicyPreferences getSampleTCAPolicyPreferences() {\r
103         return deserializeJsonFileToModel(TCA_POLICY_JSON_FILE_LOCATION, TCAPolicyPreferences.class);\r
104     }\r
105 \r
106     /**\r
107      * Provides list containing 350 CEF messages\r
108      *\r
109      * @return CEF Test Message\r
110      *\r
111      * @throws Exception Exception\r
112      */\r
113     protected static List<EventListener> getCEFMessages() throws Exception {\r
114         final String cefMessageAsString = fromStream(CEF_MESSAGES_JSON_FILE_LOCATION);\r
115         final TypeReference<List<EventListener>> eventListenerListTypeReference =\r
116                 new TypeReference<List<EventListener>>() {\r
117                 };\r
118         return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, eventListenerListTypeReference);\r
119     }\r
120 \r
121     /**\r
122      * Provides 1 valid CEF messages which does not violate Threshold as String\r
123      *\r
124      * @return CEF Test Message String\r
125      *\r
126      * @throws Exception Exception\r
127      */\r
128     protected static String getValidCEFMessage() throws Exception {\r
129         return fromStream(CEF_MESSAGE_JSON_FILE_LOCATION);\r
130     }\r
131 \r
132 \r
133     /**\r
134      * Provides single CEF Test Message\r
135      *\r
136      * @return CEF Test Message\r
137      *\r
138      * @throws Exception Exception\r
139      */\r
140     protected static EventListener getCEFEventListener() throws Exception {\r
141         final String cefMessageAsString = fromStream(CEF_MESSAGE_JSON_FILE_LOCATION);\r
142         return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, EventListener.class);\r
143     }\r
144 \r
145     /**\r
146      * Deserialize given Json file location to given model class and returns it back without any validation check\r
147      *\r
148      * @param jsonFileLocation Classpath location of the json file\r
149      * @param modelClass Model Class type\r
150      * @param <T> Json Model Type\r
151      *\r
152      * @return Json model object\r
153      */\r
154     public static <T> T deserializeJsonFileToModel(String jsonFileLocation, Class<T> modelClass) {\r
155         final InputStream jsonFileInputStream =\r
156                 BaseDCAEAnalyticsUnitTest.class.getClassLoader().getResourceAsStream(jsonFileLocation);\r
157         Assert.assertNotNull("Json File Location must be valid", jsonFileInputStream);\r
158         try {\r
159             return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(jsonFileInputStream, modelClass);\r
160         } catch (IOException ex) {\r
161             LOG.error("Error while doing assert Json for fileLocation: {}, modelClass: {}, Exception {}",\r
162                     jsonFileLocation, modelClass, ex);\r
163             throw new RuntimeException(ex);\r
164         } finally {\r
165             try {\r
166                 jsonFileInputStream.close();\r
167             } catch (IOException e) {\r
168                 LOG.error("Error while closing input stream at file location: {}", jsonFileLocation);\r
169                 throw new RuntimeException(e);\r
170             }\r
171         }\r
172     }\r
173 \r
174     protected static TCATestAppConfig getTCATestAppConfig() {\r
175         final TCATestAppConfig tcaAppConfig = new TCATestAppConfig();\r
176         tcaAppConfig.setAppName(TCA_TEST_APP_CONFIG_NAME);\r
177         tcaAppConfig.setAppDescription(TCA_TEST_APP_CONFIG_DESCRIPTION);\r
178         tcaAppConfig.setTcaSubscriberOutputStreamName(TCA_TEST_APP_CONFIG_SUBSCRIBER_OUTPUT_STREAM_NAME);\r
179         tcaAppConfig.setTcaVESAlertsTableName(TCA_TEST_APP_CONFIG_VES_ALERT_TABLE_NAME);\r
180         tcaAppConfig.setTcaVESMessageStatusTableName(TCA_TEST_APP_CONFIG_VES_MESSAGE_STATUS_TABLE_NAME);\r
181         return tcaAppConfig;\r
182     }\r
183 \r
184     /**\r
185      * Provides a test application preference for unit testing\r
186      *\r
187      * @return tca app preferences\r
188      */\r
189     protected static TCATestAppPreferences getTCATestAppPreferences() {\r
190         final TCATestAppPreferences tcaTestAppPreferences = new TCATestAppPreferences();\r
191         tcaTestAppPreferences.setSubscriberHostName("SUBSCRIBER_HOST_NAME");\r
192         tcaTestAppPreferences.setSubscriberHostPortNumber(10000);\r
193         tcaTestAppPreferences.setSubscriberTopicName("SUBSCRIBER_TOPIC_NAME");\r
194         tcaTestAppPreferences.setSubscriberUserName("SUBSCRIBER_USERNAME");\r
195         tcaTestAppPreferences.setSubscriberUserPassword("SUBSCRIBER_PASSWORD");\r
196         tcaTestAppPreferences.setSubscriberProtocol("https");\r
197         tcaTestAppPreferences.setSubscriberContentType("application/json");\r
198         tcaTestAppPreferences.setSubscriberConsumerId("SUBSCRIBER_CONSUMER_ID");\r
199         tcaTestAppPreferences.setSubscriberConsumerGroup("SUBSCRIBER_CONSUMER_GROUP_NAME");\r
200         tcaTestAppPreferences.setSubscriberTimeoutMS(10);\r
201         tcaTestAppPreferences.setSubscriberMessageLimit(100);\r
202         tcaTestAppPreferences.setSubscriberPollingInterval(1000);\r
203 \r
204         tcaTestAppPreferences.setPublisherHostName("PUBLISHER_HOST_NAME");\r
205         tcaTestAppPreferences.setPublisherHostPort(1234);\r
206         tcaTestAppPreferences.setPublisherTopicName("PUBLISHER_TOPIC_NAME");\r
207         tcaTestAppPreferences.setPublisherUserName("PUBLISHER_USERNAME");\r
208         tcaTestAppPreferences.setPublisherUserPassword("PUBLISHER_PASSWORD");\r
209         tcaTestAppPreferences.setPublisherProtocol("https");\r
210         tcaTestAppPreferences.setPublisherContentType("application/json");\r
211         tcaTestAppPreferences.setPublisherMaxBatchSize(100);\r
212         tcaTestAppPreferences.setPublisherMaxRecoveryQueueSize(100);\r
213         tcaTestAppPreferences.setPublisherPollingInterval(6000);\r
214 \r
215         tcaTestAppPreferences.setEnableAAIEnrichment(true);\r
216         tcaTestAppPreferences.setAaiEnrichmentHost("AAI_ENRICHMENT_HOST");\r
217         tcaTestAppPreferences.setAaiEnrichmentPortNumber(8443);\r
218         tcaTestAppPreferences.setAaiEnrichmentProtocol("https");\r
219         tcaTestAppPreferences.setAaiEnrichmentUserName("AAI_USERNAME");\r
220         tcaTestAppPreferences.setAaiEnrichmentUserPassword("AAI_USERPASSWORD");\r
221         tcaTestAppPreferences.setAaiEnrichmentIgnoreSSLCertificateErrors(true);\r
222         tcaTestAppPreferences.setAaiVMEnrichmentAPIPath("VM_ENRICHMENT_PATH");\r
223         tcaTestAppPreferences.setAaiVNFEnrichmentAPIPath("VNF_ENRICHMENT_PATH");\r
224         return tcaTestAppPreferences;\r
225     }\r
226 \r
227     protected static Map<String, String> getPreferenceMap() {\r
228         Map<String, String> preference = new HashMap<>();\r
229         preference.put("subscriberHostName", "mrlocal-mtnjftle01.homer.com");\r
230         preference.put("subscriberHostPort", "3905");\r
231         preference.put("subscriberTopicName", "com.dcae.dmaap.mtnje2.DcaeTestVESPub");\r
232         preference.put("subscriberProtocol", "https");\r
233         preference.put("subscriberUserName", "USER");\r
234         preference.put("subscriberUserPassword", "PASSWORD");\r
235         preference.put("subscriberContentType", "application/json");\r
236         preference.put("subscriberConsumerId", "123");\r
237         preference.put("subscriberConsumerGroup", "testTCAConsumerName-123");\r
238         preference.put("subscriberTimeoutMS", "-1");\r
239         preference.put("subscriberMessageLimit", "-1");\r
240         preference.put("subscriberPollingInterval", "30000");\r
241 \r
242         preference.put("publisherHostName", "publisherHostName");\r
243         preference.put("publisherHostPort", "3905");\r
244         preference.put("publisherTopicName", "publisherTopicName");\r
245         preference.put("publisherProtocol", "https");\r
246         preference.put("publisherUserName", "publisherUserName");\r
247         preference.put("publisherContentType", "application/json");\r
248         preference.put("publisherMaxBatchSize", "1000");\r
249         preference.put("publisherMaxRecoveryQueueSize", "100");\r
250         preference.put("publisherPollingInterval", "6000");\r
251         return preference;\r
252     }\r
253 \r
254     protected static <T extends AbstractFlowlet> void assertFlowletNameAndDescription(\r
255             final String expectedName, final String expectedDescription, final T flowlet) {\r
256         final DefaultFlowletConfigurer defaultFlowletConfigurer =\r
257                 new DefaultFlowletConfigurer(flowlet);\r
258         flowlet.configure(defaultFlowletConfigurer);\r
259 \r
260         final String flowletName = getPrivateFiledValue(defaultFlowletConfigurer, "name", String.class);\r
261         final String flowletDescription =\r
262                 getPrivateFiledValue(defaultFlowletConfigurer, "description", String.class);\r
263 \r
264         assertThat("Flowlet name must match with CDAPComponentsConstants",\r
265                 flowletName, is(expectedName));\r
266 \r
267         assertThat("Flowlet description must match with CDAPComponentsConstants",\r
268                 flowletDescription, is(expectedDescription));\r
269 \r
270     }\r
271 \r
272     protected static FlowletContext getTestFlowletContextWithValidPolicy() {\r
273         return createNewFlowletContextFromPropertiesFile(TCA_CONTROLLER_POLICY_FILE_LOCATION);\r
274     }\r
275 \r
276     protected static FlowletContext getTestFlowletContextWithValidPolicyFromJSON() {\r
277         return createNewFlowletContextFromPropertiesFile(TCA_CONTROLLER_POLICY_FROM_JSON_FILE_LOCATION);\r
278     }\r
279 \r
280     private static FlowletContext createNewFlowletContextFromPropertiesFile(final String propertyFileLocation) {\r
281         final Properties controllerProperties =\r
282                 AnalyticsModelIOUtils.loadPropertiesFile(propertyFileLocation, new Properties());\r
283 \r
284         Map<String, String> runtimeArgs = new LinkedHashMap<>();\r
285         for (Map.Entry<Object, Object> property : controllerProperties.entrySet()) {\r
286             runtimeArgs.put(property.getKey().toString(), property.getValue().toString());\r
287         }\r
288 \r
289         final FlowletContext flowletContext = mock(FlowletContext.class);\r
290         when(flowletContext.getRuntimeArguments()).thenReturn(runtimeArgs);\r
291         return flowletContext;\r
292     }\r
293 \r
294 }\r