2 * ===============================LICENSE_START======================================
\r
4 * ================================================================================
\r
5 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
\r
6 * ================================================================================
\r
7 * Licensed under the Apache License, Version 2.0 (the "License");
\r
8 * you may not use this file except in compliance with the License.
\r
9 * You may obtain a copy of the License at
\r
11 * http://www.apache.org/licenses/LICENSE-2.0
\r
13 * Unless required by applicable law or agreed to in writing, software
\r
14 * distributed under the License is distributed on an "AS IS" BASIS,
\r
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
\r
16 * See the License for the specific language governing permissions and
\r
17 * limitations under the License.
\r
18 * ============================LICENSE_END===========================================
\r
21 package org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca;
\r
23 import co.cask.cdap.api.dataset.DatasetProperties;
\r
24 import co.cask.cdap.api.dataset.lib.ObjectMappedTable;
\r
25 import com.google.common.collect.ImmutableList;
\r
26 import org.junit.Assert;
\r
27 import org.junit.Before;
\r
28 import org.junit.Test;
\r
29 import org.openecomp.dcae.apod.analytics.cdap.common.BaseAnalyticsCDAPCommonUnitTest;
\r
30 import org.openecomp.dcae.apod.analytics.model.domain.cef.CommonEventHeader;
\r
31 import org.openecomp.dcae.apod.analytics.model.domain.cef.Domain;
\r
32 import org.openecomp.dcae.apod.analytics.model.domain.cef.Event;
\r
33 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener;
\r
34 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventSeverity;
\r
35 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Direction;
\r
36 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerEventName;
\r
37 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;
\r
38 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold;
\r
39 import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFProcessorContext;
\r
40 import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;
\r
42 import static org.mockito.ArgumentMatchers.any;
\r
43 import static org.mockito.ArgumentMatchers.anyString;
\r
44 import static org.mockito.Mockito.mock;
\r
45 import static org.mockito.Mockito.times;
\r
46 import static org.mockito.Mockito.verify;
\r
47 import static org.mockito.Mockito.when;
\r
50 * @author Rajiv Singla . Creation Date: 2/16/2017.
\r
52 public class TCAMessageStatusPersisterTest extends BaseAnalyticsCDAPCommonUnitTest {
\r
54 private static final int TEST_INSTANCE_ID = 0;
\r
55 private static final Domain TEST_DOMAIN = Domain.other;
\r
56 private static final String TEST_EVENT_NAME = "TEST_EVENT_NAME";
\r
58 private ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable;
\r
59 private TCACEFProcessorContext processorContext;
\r
60 private EventListener eventListener;
\r
61 private Event event;
\r
62 private CommonEventHeader commonEventHeader;
\r
66 @SuppressWarnings("unchecked")
\r
67 public void before() {
\r
68 vesMessageStatusTable = mock(ObjectMappedTable.class);
\r
69 processorContext = mock(TCACEFProcessorContext.class);
\r
70 eventListener = mock(EventListener.class);
\r
71 event = mock(Event.class);
\r
72 commonEventHeader = mock(CommonEventHeader.class);
\r
73 when(processorContext.getMessage()).thenReturn("testMessage");
\r
74 when(processorContext.getCEFEventListener()).thenReturn(eventListener);
\r
75 when(eventListener.getEvent()).thenReturn(event);
\r
76 when(event.getCommonEventHeader()).thenReturn(commonEventHeader);
\r
77 when(commonEventHeader.getEventName()).thenReturn(TEST_EVENT_NAME);
\r
78 when(commonEventHeader.getDomain()).thenReturn(TEST_DOMAIN);
\r
83 public void testPersistWithInApplicableMessage() throws Exception {
\r
84 TCAMessageStatusPersister.persist
\r
85 (processorContext, TEST_INSTANCE_ID, TCACalculatorMessageType.INAPPLICABLE, vesMessageStatusTable);
\r
86 verify(vesMessageStatusTable, times(1)).write(anyString(),
\r
87 any(TCAMessageStatusEntity.class));
\r
91 public void testPersistWithNonCompliantMessage() throws Exception {
\r
92 final MetricsPerEventName metricsPerEventName = mock(MetricsPerEventName.class);
\r
93 final Threshold threshold = mock(Threshold.class);
\r
94 when(processorContext.getMetricsPerEventName()).thenReturn(metricsPerEventName);
\r
95 when((metricsPerEventName.getThresholds())).thenReturn(ImmutableList.of(threshold));
\r
96 when(threshold.getDirection()).thenReturn(Direction.GREATER);
\r
97 when(threshold.getSeverity()).thenReturn(EventSeverity.CRITICAL);
\r
98 TCAMessageStatusPersister.persist(
\r
99 processorContext, TEST_INSTANCE_ID, TCACalculatorMessageType.NON_COMPLIANT,
\r
100 vesMessageStatusTable, "testAlert");
\r
101 verify(vesMessageStatusTable, times(1)).write(anyString(),
\r
102 any(TCAMessageStatusEntity.class));
\r
106 public void testPersistWithCompliantMessage() throws Exception {
\r
107 final String cefMessage = fromStream(CEF_MESSAGE_FILE_LOCATION);
\r
108 final String tcaPolicyString = fromStream(TCA_POLICY_FILE_LOCATION);
\r
110 final TCAPolicy tcaPolicy = ANALYTICS_MODEL_OBJECT_MAPPER.readValue(tcaPolicyString, TCAPolicy.class);
\r
111 final TCACEFProcessorContext tcacefProcessorContext = TCAUtils.filterCEFMessage(cefMessage, tcaPolicy);
\r
112 final TCACEFProcessorContext finalProcessorContext =
\r
113 TCAUtils.computeThresholdViolations(tcacefProcessorContext);
\r
115 TCAMessageStatusPersister.persist(finalProcessorContext, TEST_INSTANCE_ID,
\r
116 TCACalculatorMessageType.COMPLIANT, vesMessageStatusTable, "testAlert");
\r
117 verify(vesMessageStatusTable, times(1)).write(anyString(),
\r
118 any(TCAMessageStatusEntity.class));
\r
122 public void testGetDatasetProperties() throws Exception {
\r
123 final DatasetProperties datasetProperties = TCAMessageStatusPersister.getDatasetProperties(20000);
\r
124 Assert.assertNotNull(datasetProperties);
\r