Policy notifications appear to be reversed
[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-2020 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     /**
62      * Simple test with one PDP that immediately responds with success.
63      */
64     @Test
65     public void testSimpleImmediateSuccess() {
66         tracker.addData(makeData(policy1, PDP1));
67
68         // indicate that PDP1 has succeeded (i.e., undeployed)
69         List<PolicyStatus> statusList = new ArrayList<>();
70         tracker.processResponse(PDP1, Arrays.asList(), statusList);
71
72         assertEquals(1, statusList.size());
73         assertEquals(policy1, statusList.get(0).getPolicy());
74         assertEquals(type, statusList.get(0).getPolicyType());
75         assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString());
76
77         // indicate that PDP1 has succeeded again - should be no output
78         statusList.clear();
79         tracker.processResponse(PDP1, Arrays.asList(), statusList);
80         assertEquals(0, statusList.size());
81
82         // indicate failure (i.e., still deployed) - no output, because no longer tracked
83         statusList.clear();
84         tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
85         assertEquals(0, statusList.size());
86
87         // indicate that PDP1 has failed again - still no output
88         statusList.clear();
89         tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
90         assertEquals(0, statusList.size());
91
92         // indicate that PDP1 has succeeded again - still no output
93         statusList.clear();
94         tracker.processResponse(PDP1, Arrays.asList(), statusList);
95         assertEquals(0, statusList.size());
96     }
97
98     /**
99      * Simple test with one PDP that immediately responds with success.
100      */
101     @Test
102     public void testSimpleImmediateFail() {
103         tracker.addData(makeData(policy1, PDP1));
104
105         // indicate that PDP1 has failed (i.e., still deployed)
106         List<PolicyStatus> statusList = new ArrayList<>();
107         tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
108         assertEquals(1, statusList.size());
109         assertEquals("[0, 1, 0]", getCounts(statusList.get(0)).toString());
110
111         // indicate that PDP1 has failed again - should be no output
112         statusList.clear();
113         tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
114         assertEquals(0, statusList.size());
115
116         // indicate success (i.e., undeployed)
117         tracker.processResponse(PDP1, Arrays.asList(), statusList);
118
119         assertEquals(1, statusList.size());
120         assertEquals(policy1, statusList.get(0).getPolicy());
121         assertEquals(type, statusList.get(0).getPolicyType());
122         assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString());
123
124         // indicate that PDP1 has succeeded again - should be no output
125         statusList.clear();
126         tracker.processResponse(PDP1, Arrays.asList(), statusList);
127         assertEquals(0, statusList.size());
128
129         // indicate that PDP1 has failed again - still no output
130         statusList.clear();
131         tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
132         assertEquals(0, statusList.size());
133     }
134
135     /**
136      * Simple test where PDP is removed and then it responds.
137      */
138     @Test
139     public void testSimpleRemove() {
140         tracker.addData(makeData(policy1, PDP1));
141
142         // remove the PDP
143         List<PolicyStatus> statusList = new ArrayList<>();
144         tracker.removePdp(PDP1, statusList);
145         assertEquals(1, statusList.size());
146         assertEquals(policy1, statusList.get(0).getPolicy());
147         assertEquals(type, statusList.get(0).getPolicyType());
148         assertEquals("[0, 0, 0]", getCounts(statusList.get(0)).toString());
149
150         /*
151          * indicate that PDP1 has succeeded (i.e., undeployed) - should be no message
152          * since PDP was removed from the policy
153          */
154         statusList.clear();
155         tracker.processResponse(PDP1, Arrays.asList(), statusList);
156         assertEquals(0, statusList.size());
157
158         /*
159          * indicate that PDP1 has failed (i.e., still deployed) - should be no message
160          * since PDP was removed from the policy
161          */
162         statusList.clear();
163         tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
164         assertEquals(0, statusList.size());
165     }
166
167     /**
168      * Test with multiple PDPs.
169      */
170     @Test
171     public void testMulti() {
172         tracker.addData(makeData(policy1, PDP1, PDP2));
173
174         // indicate that PDP2 has been undeployed
175         tracker.processResponse(PDP2, Collections.emptyList(), new ArrayList<>(0));
176
177         // indicate that PDP1 has been undeployed
178         List<PolicyStatus> statusList = new ArrayList<>();
179         tracker.processResponse(PDP1, Collections.emptyList(), statusList);
180
181         assertEquals(1, statusList.size());
182         assertEquals(policy1, statusList.get(0).getPolicy());
183         assertEquals(type, statusList.get(0).getPolicyType());
184         assertEquals("[2, 0, 0]", getCounts(statusList.get(0)).toString());
185
186         /*
187          * indicate that PDP1 has been re-deployed - should not get a notification,
188          * because policy is gone
189          */
190         statusList.clear();
191         tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
192         assertTrue(statusList.isEmpty());
193     }
194
195     /**
196      * Test with multiple PDPs, and one is removed while it is in a failure state.
197      */
198     @Test
199     public void testMultiRemove() {
200         tracker.addData(makeData(policy1, PDP1, PDP2));
201
202         // indicate that PDP2 has been undeployed
203         tracker.processResponse(PDP2, Collections.emptyList(), new ArrayList<>(0));
204
205         // indicate that PDP1 has failed (i.e., still deployed)
206         List<PolicyStatus> statusList = new ArrayList<>();
207         tracker.processResponse(PDP1, Arrays.asList(policy1), statusList);
208
209         assertEquals(1, statusList.size());
210         assertEquals(policy1, statusList.get(0).getPolicy());
211         assertEquals(type, statusList.get(0).getPolicyType());
212         assertEquals("[1, 1, 0]", getCounts(statusList.get(0)).toString());
213
214         // remove PDP1 - expect message AND policy should be removed
215         statusList.clear();
216         tracker.removePdp(PDP1, statusList);
217         assertEquals(1, statusList.size());
218         assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString());
219
220         // re-add PDP1
221         tracker.addData(makeData(policy1, PDP1));
222
223         // indicate that PDP1 has succeeded; policy is now new, so doesn't include PDP2
224         statusList.clear();
225         tracker.processResponse(PDP1, Arrays.asList(), statusList);
226         assertEquals(1, statusList.size());
227         assertEquals("[1, 0, 0]", getCounts(statusList.get(0)).toString());
228     }
229
230     @Test
231     public void testUpdateData() {
232         // when success returns false
233         assertFalse(tracker.updateData(PDP1, data, false));
234         verify(data).success(PDP1);
235         verify(data, never()).fail(any());
236
237         // when inactive
238         assertFalse(tracker.updateData(PDP1, data, true));
239         verify(data).success(PDP1);
240         verify(data).fail(any());
241
242         // when success & fail return true
243         when(data.success(PDP1)).thenReturn(true);
244         when(data.fail(PDP1)).thenReturn(true);
245         assertTrue(tracker.updateData(PDP1, data, false));
246         verify(data, times(2)).success(PDP1);
247         verify(data, times(1)).fail(PDP1);
248
249         // when inactive
250         assertTrue(tracker.updateData(PDP1, data, true));
251         verify(data, times(2)).success(PDP1);
252         verify(data, times(2)).fail(PDP1);
253     }
254
255     @Test
256     public void testShouldRemove() {
257         // when data is not complete
258         assertFalse(tracker.shouldRemove(data));
259
260         // when data has succeeded
261         when(data.allSucceeded()).thenReturn(true);
262         assertTrue(tracker.shouldRemove(data));
263     }
264 }