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