ba85c4894e2a6b5569194999c4806149255ab282
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / notification / PolicyUndeployTrackerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * ONAP PAP
4  * ================================================================================
5  * Copyright (C) 2019 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.policy.pap.main.notification;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertTrue;
26 import static org.mockito.Matchers.any;
27 import static org.mockito.Mockito.never;
28 import static org.mockito.Mockito.times;
29 import static org.mockito.Mockito.verify;
30 import static org.mockito.Mockito.when;
31
32 import java.util.ArrayList;
33 import java.util.Arrays;
34 import java.util.Collections;
35 import java.util.List;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.mockito.Mock;
39 import org.mockito.MockitoAnnotations;
40 import org.onap.policy.models.pap.concepts.PolicyStatus;
41
42 public class PolicyUndeployTrackerTest extends PolicyCommonSupport {
43
44     @Mock
45     private PolicyTrackerData data;
46
47     private PolicyUndeployTracker tracker;
48
49     /**
50      * Creates various objects, including {@link #tracker}.
51      */
52     @Before
53     public void setUp() {
54         MockitoAnnotations.initMocks(this);
55
56         super.setUp();
57
58         tracker = new PolicyUndeployTracker();
59     }
60
61     @Test
62     public void test() {
63         tracker.addData(makeData(policy1, PDP1, PDP2));
64
65         // indicate that PDP2 has been undeployed
66         tracker.processResponse(PDP2, Collections.emptyList(), new ArrayList<>(0));
67
68         // indicate that PDP1 has been undeployed
69         List<PolicyStatus> statusList = new ArrayList<>();
70         tracker.processResponse(PDP1, Collections.emptyList(), statusList);
71
72         assertEquals(1, statusList.size());
73         assertEquals(policy1, statusList.get(0).getPolicy());
74         assertEquals(type, statusList.get(0).getPolicyType());
75         assertEquals("[2, 0, 0]", getCounts(statusList.get(0)).toString());
76
77         // indicate that PDP1 has been re-deployed - should not get a notification,
78         // because policy
79         // is gone
80         statusList.clear();
81         tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
82         assertTrue(statusList.isEmpty());
83     }
84
85     @Test
86     public void testUpdateData() {
87         // when success returns false
88         assertFalse(tracker.updateData(PDP1, data, false));
89         verify(data).success(PDP1);
90         verify(data, never()).fail(any());
91
92         // when inactive
93         assertFalse(tracker.updateData(PDP1, data, true));
94         verify(data).success(PDP1);
95         verify(data).fail(any());
96
97         // when success & fail return true
98         when(data.success(PDP1)).thenReturn(true);
99         when(data.fail(PDP1)).thenReturn(true);
100         assertTrue(tracker.updateData(PDP1, data, false));
101         verify(data, times(2)).success(PDP1);
102         verify(data, times(1)).fail(PDP1);
103
104         // when inactive
105         assertTrue(tracker.updateData(PDP1, data, true));
106         verify(data, times(2)).success(PDP1);
107         verify(data, times(2)).fail(PDP1);
108     }
109
110     @Test
111     public void testShouldRemove() {
112         // when data is not complete
113         assertFalse(tracker.shouldRemove(data));
114
115         // when data is complete
116         when(data.isComplete()).thenReturn(true);
117         assertTrue(tracker.shouldRemove(data));
118     }
119 }