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 / TCAAlertsAbatementPersisterTest.java
1 package org.openecomp.dcae.apod.analytics.cdap.common.persistance.tca;
2
3 import co.cask.cdap.api.dataset.DatasetProperties;
4 import co.cask.cdap.api.dataset.lib.ObjectMappedTable;
5 import com.google.common.base.Joiner;
6 import com.google.common.collect.ImmutableList;
7 import org.junit.Before;
8 import org.junit.Test;
9 import org.openecomp.dcae.apod.analytics.cdap.common.BaseAnalyticsCDAPCommonUnitTest;
10 import org.openecomp.dcae.apod.analytics.common.utils.PersistenceUtils;
11 import org.openecomp.dcae.apod.analytics.model.domain.cef.CommonEventHeader;
12 import org.openecomp.dcae.apod.analytics.model.domain.cef.Event;
13 import org.openecomp.dcae.apod.analytics.model.domain.cef.EventListener;
14 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.MetricsPerEventName;
15 import org.openecomp.dcae.apod.analytics.model.domain.policy.tca.Threshold;
16 import org.openecomp.dcae.apod.analytics.model.facade.tca.TCAVESResponse;
17
18 import static org.junit.Assert.assertEquals;
19 import static org.junit.Assert.assertNotNull;
20 import static org.mockito.ArgumentMatchers.any;
21 import static org.mockito.ArgumentMatchers.anyString;
22 import static org.mockito.ArgumentMatchers.eq;
23 import static org.mockito.Mockito.mock;
24 import static org.mockito.Mockito.times;
25 import static org.mockito.Mockito.verify;
26 import static org.mockito.Mockito.when;
27
28 /**
29  * Author: rs153v (Rajiv Singla) . Creation Date: 9/13/2017.
30  */
31 public class TCAAlertsAbatementPersisterTest extends BaseAnalyticsCDAPCommonUnitTest {
32
33     private static final String EVENT_NAME = "testEventName";
34     private static final String SOURCE_NAME = "testSourceName";
35     private static final String REPORTING_ENTITY_NAME = "testReportingEntityName";
36     private static final String THRESHOLD_CLOSED_LOOP_CONTROL_NAME = "testControlLoopName";
37     private static final String THRESHOLD_FIELD_PATH = "testFieldPath";
38     private static final String EXPECTED_LOOKUP_KEY = Joiner.on(PersistenceUtils.ROW_KEY_DELIMITER).join(
39             ImmutableList.of(EVENT_NAME, SOURCE_NAME, REPORTING_ENTITY_NAME,
40                     THRESHOLD_CLOSED_LOOP_CONTROL_NAME, THRESHOLD_FIELD_PATH));
41
42     private ObjectMappedTable<TCAAlertsAbatementEntity> alertsAbatementTable;
43     private EventListener eventListener;
44     private MetricsPerEventName violatedMetricsPerEventName;
45     private TCAVESResponse tcavesResponse;
46     private String abatementTS;
47     private Event event;
48     private CommonEventHeader commonEventHeader;
49     private Threshold violatedThreshold;
50
51     @Before
52     public void before() throws Exception {
53         alertsAbatementTable = mock(ObjectMappedTable.class);
54         eventListener = mock(EventListener.class);
55         event = mock(Event.class);
56         commonEventHeader = mock(CommonEventHeader.class);
57
58         when(eventListener.getEvent()).thenReturn(event);
59         when(event.getCommonEventHeader()).thenReturn(commonEventHeader);
60         when(commonEventHeader.getEventName()).thenReturn(EVENT_NAME);
61         when(commonEventHeader.getSourceName()).thenReturn(SOURCE_NAME);
62         when(commonEventHeader.getReportingEntityName()).thenReturn(REPORTING_ENTITY_NAME);
63
64         violatedMetricsPerEventName = mock(MetricsPerEventName.class);
65         when(violatedMetricsPerEventName.getEventName()).thenReturn(EVENT_NAME);
66         violatedThreshold = mock(Threshold.class);
67         when(violatedMetricsPerEventName.getThresholds()).thenReturn(ImmutableList.of(violatedThreshold));
68         when(violatedThreshold.getClosedLoopControlName()).thenReturn(THRESHOLD_CLOSED_LOOP_CONTROL_NAME);
69         when(violatedThreshold.getFieldPath()).thenReturn(THRESHOLD_FIELD_PATH);
70         tcavesResponse = mock(TCAVESResponse.class);
71         abatementTS = "1234";
72     }
73
74     @Test
75     public void testGetDatasetProperties() throws Exception {
76         final DatasetProperties datasetProperties = TCAAlertsAbatementPersister.getDatasetProperties(20000);
77         assertNotNull(datasetProperties);
78     }
79
80     @Test
81     public void testPersist() throws Exception {
82
83         TCAAlertsAbatementPersister.persist(eventListener, violatedMetricsPerEventName, tcavesResponse,
84                 abatementTS, alertsAbatementTable);
85         verify(alertsAbatementTable, times(1)).write(anyString(),
86                 any(TCAAlertsAbatementEntity.class));
87
88     }
89
90     @Test
91     public void testLookUpByKey() throws Exception {
92         TCAAlertsAbatementPersister.lookUpByKey(eventListener, violatedMetricsPerEventName, alertsAbatementTable);
93         verify(alertsAbatementTable, times(1)).read(eq(EXPECTED_LOOKUP_KEY));
94     }
95
96     @Test
97     public void testCreateKey() throws Exception {
98         final String createdKey = TCAAlertsAbatementPersister.createKey(eventListener, violatedMetricsPerEventName);
99         assertEquals(createdKey, EXPECTED_LOOKUP_KEY);
100
101     }
102
103 }