Add support for ABATED alerts within CDAP TCA
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-common / src / test / java / org / openecomp / dcae / apod / analytics / cdap / common / persistance / tca / TCAMessageStatusPersisterTest.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.common.persistance.tca;
22
23 import co.cask.cdap.api.dataset.DatasetProperties;
24 import co.cask.cdap.api.dataset.lib.ObjectMappedTable;
25 import com.google.common.collect.ImmutableList;
26 import org.junit.Assert;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.openecomp.dcae.apod.analytics.cdap.common.BaseAnalyticsCDAPCommonUnitTest;
30 import org.openecomp.dcae.apod.analytics.model.domain.cef.CommonEventHeader;
31 import org.openecomp.dcae.apod.analytics.model.domain.cef.Domain;
32 import org.openecomp.dcae.apod.analytics.model.domain.cef.Event;
33 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener;
34 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventSeverity;
35 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Direction;
36 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerEventName;
37 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.TCAPolicy;
38 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold;
39 import org.openecomp.dcae.apod.analytics.tca.processor.TCACEFProcessorContext;
40 import org.openecomp.dcae.apod.analytics.tca.utils.TCAUtils;
41
42 import static org.mockito.ArgumentMatchers.any;
43 import static org.mockito.ArgumentMatchers.anyString;
44 import static org.mockito.Mockito.mock;
45 import static org.mockito.Mockito.times;
46 import static org.mockito.Mockito.verify;
47 import static org.mockito.Mockito.when;
48
49 /**
50  * @author Rajiv Singla . Creation Date: 2/16/2017.
51  */
52 public class TCAMessageStatusPersisterTest extends BaseAnalyticsCDAPCommonUnitTest {
53
54     private static final int TEST_INSTANCE_ID = 0;
55     private static final Domain TEST_DOMAIN = Domain.other;
56     private static final String TEST_EVENT_NAME = "TEST_EVENT_NAME";
57
58     private ObjectMappedTable<TCAMessageStatusEntity> vesMessageStatusTable;
59     private TCACEFProcessorContext processorContext;
60     private EventListener eventListener;
61     private Event event;
62     private CommonEventHeader commonEventHeader;
63     private String functionalRole;
64
65
66     @Before
67     public void before() {
68         vesMessageStatusTable = mock(ObjectMappedTable.class);
69         processorContext = mock(TCACEFProcessorContext.class);
70         eventListener = mock(EventListener.class);
71         event = mock(Event.class);
72         commonEventHeader = mock(CommonEventHeader.class);
73         when(processorContext.getMessage()).thenReturn("testMessage");
74         when(processorContext.getCEFEventListener()).thenReturn(eventListener);
75         when(eventListener.getEvent()).thenReturn(event);
76         when(event.getCommonEventHeader()).thenReturn(commonEventHeader);
77         when(commonEventHeader.getEventName()).thenReturn(TEST_EVENT_NAME);
78         when(commonEventHeader.getDomain()).thenReturn(TEST_DOMAIN);
79     }
80
81
82     @Test
83     public void testPersistWithInApplicableMessage() throws Exception {
84         TCAMessageStatusPersister.persist
85                 (processorContext, TEST_INSTANCE_ID, TCACalculatorMessageType.INAPPLICABLE, vesMessageStatusTable);
86         verify(vesMessageStatusTable, times(1)).write(anyString(),
87                 any(TCAMessageStatusEntity.class));
88     }
89
90     @Test
91     public void testPersistWithNonCompliantMessage() throws Exception {
92         final MetricsPerEventName metricsPerEventName = mock(MetricsPerEventName.class);
93         final Threshold threshold = mock(Threshold.class);
94         when(processorContext.getMetricsPerEventName()).thenReturn(metricsPerEventName);
95         when((metricsPerEventName.getThresholds())).thenReturn(ImmutableList.of(threshold));
96         when(threshold.getDirection()).thenReturn(Direction.GREATER);
97         when(threshold.getSeverity()).thenReturn(EventSeverity.CRITICAL);
98         TCAMessageStatusPersister.persist(
99                 processorContext, TEST_INSTANCE_ID, TCACalculatorMessageType.NON_COMPLIANT,
100                 vesMessageStatusTable, "testAlert");
101         verify(vesMessageStatusTable, times(1)).write(anyString(),
102                 any(TCAMessageStatusEntity.class));
103     }
104
105     @Test
106     public void testPersistWithCompliantMessage() throws Exception {
107         final String cefMessage = fromStream(CEF_MESSAGE_FILE_LOCATION);
108         final String tcaPolicyString = fromStream(TCA_POLICY_FILE_LOCATION);
109
110         final TCAPolicy tcaPolicy = ANALYTICS_MODEL_OBJECT_MAPPER.readValue(tcaPolicyString, TCAPolicy.class);
111         final TCACEFProcessorContext tcacefProcessorContext = TCAUtils.filterCEFMessage(cefMessage, tcaPolicy);
112         final TCACEFProcessorContext finalProcessorContext =
113                 TCAUtils.computeThresholdViolations(tcacefProcessorContext);
114
115         TCAMessageStatusPersister.persist(finalProcessorContext, TEST_INSTANCE_ID,
116                 TCACalculatorMessageType.COMPLIANT, vesMessageStatusTable, "testAlert");
117         verify(vesMessageStatusTable, times(1)).write(anyString(),
118                 any(TCAMessageStatusEntity.class));
119     }
120
121     @Test
122     public void testGetDatasetProperties() throws Exception {
123         final DatasetProperties datasetProperties = TCAMessageStatusPersister.getDatasetProperties(20000);
124         Assert.assertNotNull(datasetProperties);
125
126     }
127
128 }