54fe8707a7873752d409e688d5aa6d0fa6fd7456
[dcaegen2/analytics/tca.git] / dcae-analytics-cdap-common / src / test / java / org / onap / dcae / apod / analytics / cdap / common / persistance / tca / TCAAlertsAbatementPersisterTest.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.onap.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.base.Joiner;
26 import com.google.common.collect.ImmutableList;
27 import org.junit.Before;
28 import org.junit.Test;
29 import org.onap.dcae.apod.analytics.cdap.common.BaseAnalyticsCDAPCommonUnitTest;
30 import org.onap.dcae.apod.analytics.common.utils.PersistenceUtils;
31 import org.onap.dcae.apod.analytics.model.domain.cef.CommonEventHeader;
32 import org.onap.dcae.apod.analytics.model.domain.cef.Event;
33 import org.onap.dcae.apod.analytics.model.domain.cef.EventListener;
34 import org.onap.dcae.apod.analytics.model.domain.policy.tca.MetricsPerEventName;
35 import org.onap.dcae.apod.analytics.model.domain.policy.tca.Threshold;
36 import org.onap.dcae.apod.analytics.model.facade.tca.TCAVESResponse;
37
38 import static org.junit.Assert.assertEquals;
39 import static org.junit.Assert.assertNotNull;
40 import static org.mockito.ArgumentMatchers.any;
41 import static org.mockito.ArgumentMatchers.anyString;
42 import static org.mockito.ArgumentMatchers.eq;
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: 9/13/2017.
50  */
51 public class TCAAlertsAbatementPersisterTest extends BaseAnalyticsCDAPCommonUnitTest {
52
53     private static final String EVENT_NAME = "testEventName";
54     private static final String SOURCE_NAME = "testSourceName";
55     private static final String REPORTING_ENTITY_NAME = "testReportingEntityName";
56     private static final String THRESHOLD_CLOSED_LOOP_CONTROL_NAME = "testControlLoopName";
57     private static final String THRESHOLD_FIELD_PATH = "testFieldPath";
58     private static final String EXPECTED_LOOKUP_KEY = Joiner.on(PersistenceUtils.ROW_KEY_DELIMITER).join(
59             ImmutableList.of(EVENT_NAME, SOURCE_NAME, REPORTING_ENTITY_NAME,
60                     THRESHOLD_CLOSED_LOOP_CONTROL_NAME, THRESHOLD_FIELD_PATH));
61
62     private ObjectMappedTable<TCAAlertsAbatementEntity> alertsAbatementTable;
63     private EventListener eventListener;
64     private MetricsPerEventName violatedMetricsPerEventName;
65     private TCAVESResponse tcavesResponse;
66     private String abatementTS;
67     private Event event;
68     private CommonEventHeader commonEventHeader;
69     private Threshold violatedThreshold;
70
71     @Before
72     public void before() throws Exception {
73         alertsAbatementTable = mock(ObjectMappedTable.class);
74         eventListener = mock(EventListener.class);
75         event = mock(Event.class);
76         commonEventHeader = mock(CommonEventHeader.class);
77
78         when(eventListener.getEvent()).thenReturn(event);
79         when(event.getCommonEventHeader()).thenReturn(commonEventHeader);
80         when(commonEventHeader.getEventName()).thenReturn(EVENT_NAME);
81         when(commonEventHeader.getSourceName()).thenReturn(SOURCE_NAME);
82         when(commonEventHeader.getReportingEntityName()).thenReturn(REPORTING_ENTITY_NAME);
83
84         violatedMetricsPerEventName = mock(MetricsPerEventName.class);
85         when(violatedMetricsPerEventName.getEventName()).thenReturn(EVENT_NAME);
86         violatedThreshold = mock(Threshold.class);
87         when(violatedMetricsPerEventName.getThresholds()).thenReturn(ImmutableList.of(violatedThreshold));
88         when(violatedThreshold.getClosedLoopControlName()).thenReturn(THRESHOLD_CLOSED_LOOP_CONTROL_NAME);
89         when(violatedThreshold.getFieldPath()).thenReturn(THRESHOLD_FIELD_PATH);
90         tcavesResponse = mock(TCAVESResponse.class);
91         abatementTS = "1234";
92     }
93
94     @Test
95     public void testGetDatasetProperties() throws Exception {
96         final DatasetProperties datasetProperties = TCAAlertsAbatementPersister.getDatasetProperties(20000);
97         assertNotNull(datasetProperties);
98     }
99
100     @Test
101     public void testPersist() throws Exception {
102
103         TCAAlertsAbatementPersister.persist(eventListener, violatedMetricsPerEventName, tcavesResponse,
104                 abatementTS, alertsAbatementTable, null);
105         verify(alertsAbatementTable, times(1)).write(anyString(),
106                 any(TCAAlertsAbatementEntity.class));
107
108     }
109
110     @Test
111     public void testLookUpByKey() throws Exception {
112         TCAAlertsAbatementPersister.lookUpByKey(eventListener, violatedMetricsPerEventName, alertsAbatementTable, null);
113         verify(alertsAbatementTable, times(1)).read(eq(EXPECTED_LOOKUP_KEY));
114     }
115
116     @Test
117     public void testCreateKey() throws Exception {
118         final String createdKey = TCAAlertsAbatementPersister.createKey(eventListener, violatedMetricsPerEventName);
119         assertEquals(createdKey, EXPECTED_LOOKUP_KEY);
120
121     }
122
123 }