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";
69 protected static final String TCA_CONTROLLER_POLICY_FILE_LOCATION =
70 "data/properties/tca_controller_policy.properties";
72 protected static final String TCA_CONTROLLER_POLICY_FROM_JSON_FILE_LOCATION =
73 "data/properties/tca_controller_policy_from_json.properties";
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";
86 * Provides TCA Policy that can be used for testing
88 * @return test TCA Policy Object
90 protected TCAPolicy getSampleTCAPolicy() {
91 return deserializeJsonFileToModel(TCA_POLICY_JSON_FILE_LOCATION, TCAPolicy.class);
95 * Provides TCA Policy that can be used for testing
97 * @return test {@link TCAPolicyPreferences}
99 protected TCAPolicyPreferences getSampleTCAPolicyPreferences() {
100 return deserializeJsonFileToModel(TCA_POLICY_JSON_FILE_LOCATION, TCAPolicyPreferences.class);
104 * Provides list containing 350 CEF messages
106 * @return CEF Test Message
108 * @throws Exception Exception
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>>() {
115 return ANALYTICS_MODEL_OBJECT_MAPPER.readValue(cefMessageAsString, eventListenerListTypeReference);
119 * Provides 1 valid CEF messages which does not violate Threshold as String
121 * @return CEF Test Message String
123 * @throws Exception Exception
125 protected String getValidCEFMessage() throws Exception {
126 return fromStream(CEF_MESSAGE_JSON_FILE_LOCATION);
131 * Provides single CEF Test Message
133 * @return CEF Test Message
135 * @throws Exception Exception
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);
143 * Deserialize given Json file location to given model class and returns it back without any validation check
145 * @param jsonFileLocation Classpath location of the json file
146 * @param modelClass Model Class type
147 * @param <T> Json Model Type
149 * @return Json model object
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);
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);
163 jsonFileInputStream.close();
164 } catch (IOException e) {
165 LOG.error("Error while closing input stream at file location: {}", jsonFileLocation);
166 throw new RuntimeException(e);
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);
182 * Provides a test application preference for unit testing
184 * @return tca app preferences
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);
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;
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");
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");
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);
247 final String flowletName = getPrivateFiledValue(defaultFlowletConfigurer, "name", String.class);
248 final String flowletDescription =
249 getPrivateFiledValue(defaultFlowletConfigurer, "description", String.class);
251 assertThat("Flowlet name must match with CDAPComponentsConstants",
252 flowletName, is(expectedName));
254 assertThat("Flowlet description must match with CDAPComponentsConstants",
255 flowletDescription, is(expectedDescription));
259 protected static FlowletContext getTestFlowletContextWithValidPolicy() {
260 final Properties controllerProperties =
261 AnalyticsModelIOUtils.loadPropertiesFile(TCA_CONTROLLER_POLICY_FILE_LOCATION, new Properties());
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());
268 final FlowletContext flowletContext = mock(FlowletContext.class);
269 when(flowletContext.getRuntimeArguments()).thenReturn(runtimeArgs);
270 return flowletContext;
273 protected static FlowletContext getTestFlowletContextWithValidPolicyFromJSON() {
274 final Properties controllerProperties =
275 AnalyticsModelIOUtils.loadPropertiesFile(TCA_CONTROLLER_POLICY_FROM_JSON_FILE_LOCATION,
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());
283 final FlowletContext flowletContext = mock(FlowletContext.class);
284 when(flowletContext.getRuntimeArguments()).thenReturn(runtimeArgs);
285 return flowletContext;