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