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