2 * ===============================LICENSE_START======================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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===========================================
21 package org.openecomp.dcae.apod.analytics.cdap.tca;
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;
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;
45 import java.util.Properties;
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;
53 * @author Rajiv Singla . Creation Date: 10/25/2016.
55 public abstract class BaseAnalyticsCDAPTCAUnitTest extends BaseDCAEAnalyticsUnitTest {
58 * Object mapper to be used for all TCA Json Parsing
60 protected static final ObjectMapper ANALYTICS_MODEL_OBJECT_MAPPER =
61 Suppliers.memoize(new AnalyticsModelObjectMapperSupplier()).get();
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";
72 protected static final String TCA_CONTROLLER_POLICY_FILE_LOCATION =
73 "data/properties/tca_controller_policy.properties";
75 protected static final String TCA_CONTROLLER_POLICY_FROM_JSON_FILE_LOCATION =
76 "data/properties/tca_controller_policy_from_json.properties";
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";
89 * Provides TCA Policy that can be used for testing
91 * @return test TCA Policy Object
93 protected static TCAPolicy getSampleTCAPolicy() {
94 return deserializeJsonFileToModel(TCA_POLICY_JSON_FILE_LOCATION, TCAPolicy.class);
98 * Provides TCA Policy that can be used for testing
100 * @return test {@link TCAPolicyPreferences}
102 protected static TCAPolicyPreferences getSampleTCAPolicyPreferences() {
103 return deserializeJsonFileToModel(TCA_POLICY_JSON_FILE_LOCATION, TCAPolicyPreferences.class);
107 * Provides list containing 350 CEF messages
109 * @return CEF Test Message
111 * @throws Exception Exception
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>>() {
118 return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, eventListenerListTypeReference);
122 * Provides 1 valid CEF messages which does not violate Threshold as String
124 * @return CEF Test Message String
126 * @throws Exception Exception
128 protected static String getValidCEFMessage() throws Exception {
129 return fromStream(CEF_MESSAGE_JSON_FILE_LOCATION);
134 * Provides single CEF Test Message
136 * @return CEF Test Message
138 * @throws Exception Exception
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);
146 * Deserialize given Json file location to given model class and returns it back without any validation check
148 * @param jsonFileLocation Classpath location of the json file
149 * @param modelClass Model Class type
150 * @param <T> Json Model Type
152 * @return Json model object
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);
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);
166 jsonFileInputStream.close();
167 } catch (IOException e) {
168 LOG.error("Error while closing input stream at file location: {}", jsonFileLocation);
169 throw new RuntimeException(e);
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);
185 * Provides a test application preference for unit testing
187 * @return tca app preferences
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);
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;
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");
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");
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);
250 final String flowletName = getPrivateFiledValue(defaultFlowletConfigurer, "name", String.class);
251 final String flowletDescription =
252 getPrivateFiledValue(defaultFlowletConfigurer, "description", String.class);
254 assertThat("Flowlet name must match with CDAPComponentsConstants",
255 flowletName, is(expectedName));
257 assertThat("Flowlet description must match with CDAPComponentsConstants",
258 flowletDescription, is(expectedDescription));
262 protected static FlowletContext getTestFlowletContextWithValidPolicy() {
263 return createNewFlowletContextFromPropertiesFile(TCA_CONTROLLER_POLICY_FILE_LOCATION);
266 protected static FlowletContext getTestFlowletContextWithValidPolicyFromJSON() {
267 return createNewFlowletContextFromPropertiesFile(TCA_CONTROLLER_POLICY_FROM_JSON_FILE_LOCATION);
270 private static FlowletContext createNewFlowletContextFromPropertiesFile(final String propertyFileLocation) {
271 final Properties controllerProperties =
272 AnalyticsModelIOUtils.loadPropertiesFile(propertyFileLocation, new Properties());
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());
279 final FlowletContext flowletContext = mock(FlowletContext.class);
280 when(flowletContext.getRuntimeArguments()).thenReturn(runtimeArgs);
281 return flowletContext;