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.onap.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.onap.dcae.apod.analytics.cdap.tca.settings.TCAPolicyPreferences;
31 import org.onap.dcae.apod.analytics.cdap.tca.settings.TCATestAppConfig;
32 import org.onap.dcae.apod.analytics.cdap.tca.settings.TCATestAppPreferences;
33 import org.onap.dcae.apod.analytics.model.domain.cef.EventListener;
34 import org.onap.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;
35 import org.onap.dcae.apod.analytics.model.util.AnalyticsModelIOUtils;
36 import org.onap.dcae.apod.analytics.model.util.json.AnalyticsModelObjectMapperSupplier;
37 import org.onap.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);
215 tcaTestAppPreferences.setEnableAAIEnrichment(true);
216 tcaTestAppPreferences.setAaiEnrichmentHost("AAI_ENRICHMENT_HOST");
217 tcaTestAppPreferences.setAaiEnrichmentPortNumber(8443);
218 tcaTestAppPreferences.setAaiEnrichmentProtocol("https");
219 tcaTestAppPreferences.setAaiEnrichmentUserName("AAI_USERNAME");
220 tcaTestAppPreferences.setAaiEnrichmentUserPassword("AAI_USERPASSWORD");
221 tcaTestAppPreferences.setAaiEnrichmentIgnoreSSLCertificateErrors(true);
222 tcaTestAppPreferences.setAaiVMEnrichmentAPIPath("VM_ENRICHMENT_PATH");
223 tcaTestAppPreferences.setAaiVNFEnrichmentAPIPath("VNF_ENRICHMENT_PATH");
224 return tcaTestAppPreferences;
227 protected static Map<String, String> getPreferenceMap() {
228 Map<String, String> preference = new HashMap<>();
229 preference.put("subscriberHostName", "mrlocal-mtnjftle01.homer.com");
230 preference.put("subscriberHostPort", "3905");
231 preference.put("subscriberTopicName", "com.dcae.dmaap.mtnje2.DcaeTestVESPub");
232 preference.put("subscriberProtocol", "https");
233 preference.put("subscriberUserName", "USER");
234 preference.put("subscriberUserPassword", "PASSWORD");
235 preference.put("subscriberContentType", "application/json");
236 preference.put("subscriberConsumerId", "123");
237 preference.put("subscriberConsumerGroup", "testTCAConsumerName-123");
238 preference.put("subscriberTimeoutMS", "-1");
239 preference.put("subscriberMessageLimit", "-1");
240 preference.put("subscriberPollingInterval", "30000");
242 preference.put("publisherHostName", "publisherHostName");
243 preference.put("publisherHostPort", "3905");
244 preference.put("publisherTopicName", "publisherTopicName");
245 preference.put("publisherProtocol", "https");
246 preference.put("publisherUserName", "publisherUserName");
247 preference.put("publisherContentType", "application/json");
248 preference.put("publisherMaxBatchSize", "1000");
249 preference.put("publisherMaxRecoveryQueueSize", "100");
250 preference.put("publisherPollingInterval", "6000");
254 protected static <T extends AbstractFlowlet> void assertFlowletNameAndDescription(
255 final String expectedName, final String expectedDescription, final T flowlet) {
256 final DefaultFlowletConfigurer defaultFlowletConfigurer =
257 new DefaultFlowletConfigurer(flowlet);
258 flowlet.configure(defaultFlowletConfigurer);
260 final String flowletName = getPrivateFiledValue(defaultFlowletConfigurer, "name", String.class);
261 final String flowletDescription =
262 getPrivateFiledValue(defaultFlowletConfigurer, "description", String.class);
264 assertThat("Flowlet name must match with CDAPComponentsConstants",
265 flowletName, is(expectedName));
267 assertThat("Flowlet description must match with CDAPComponentsConstants",
268 flowletDescription, is(expectedDescription));
272 protected static FlowletContext getTestFlowletContextWithValidPolicy() {
273 return createNewFlowletContextFromPropertiesFile(TCA_CONTROLLER_POLICY_FILE_LOCATION);
276 protected static FlowletContext getTestFlowletContextWithValidPolicyFromJSON() {
277 return createNewFlowletContextFromPropertiesFile(TCA_CONTROLLER_POLICY_FROM_JSON_FILE_LOCATION);
280 private static FlowletContext createNewFlowletContextFromPropertiesFile(final String propertyFileLocation) {
281 final Properties controllerProperties =
282 AnalyticsModelIOUtils.loadPropertiesFile(propertyFileLocation, new Properties());
284 Map<String, String> runtimeArgs = new LinkedHashMap<>();
285 for (Map.Entry<Object, Object> property : controllerProperties.entrySet()) {
286 runtimeArgs.put(property.getKey().toString(), property.getValue().toString());
289 final FlowletContext flowletContext = mock(FlowletContext.class);
290 when(flowletContext.getRuntimeArguments()).thenReturn(runtimeArgs);
291 return flowletContext;