a727e05756c01111d2284db5665faffb6d02b54b
[policy/pap.git] / main / src / test / java / org / onap / policy / pap / main / notification / PolicyTrackerDataTest.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.assertSame;
26 import static org.junit.Assert.assertTrue;
27
28 import java.util.Arrays;
29 import java.util.Collection;
30 import java.util.List;
31 import org.junit.Before;
32 import org.junit.Test;
33 import org.onap.policy.models.pap.concepts.PolicyStatus;
34 import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;
35
36 public class PolicyTrackerDataTest {
37
38     private static final ToscaPolicyTypeIdentifier TYPE = new ToscaPolicyTypeIdentifier("my-type", "1.2.3");
39     private static final String PDP1 = "pdp-1";
40     private static final String PDP2 = "pdp-2";
41     private static final String PDP3 = "pdp-3";
42     private static final String PDP4 = "pdp-4";
43     private static final String PDP5 = "pdp-5";
44     private static final String PDP6 = "pdp-6";
45
46     private Collection<String> fullSet;
47     private PolicyTrackerData data;
48
49     @Before
50     public void setUp() {
51         fullSet = Arrays.asList(PDP1, PDP2, PDP3, PDP4, PDP5, PDP6);
52         data = new PolicyTrackerData(TYPE);
53     }
54
55     @Test
56     public void testPolicyTrackerData_testGetPolicyType() {
57         assertSame(TYPE, data.getPolicyType());
58     }
59
60     @Test
61     public void testIsComplete() {
62         assertTrue(data.isComplete());
63
64         data.addPdps(Arrays.asList(PDP1, PDP2));
65         assertFalse(data.isComplete());
66
67         data.success(PDP1);
68         assertFalse(data.isComplete());
69
70         data.fail(PDP2);
71         assertTrue(data.isComplete());
72     }
73
74     @Test
75     public void testIsEmpty() {
76         assertTrue(data.isEmpty());
77
78         data.addPdps(Arrays.asList(PDP1, PDP2));
79         assertFalse(data.isEmpty());
80
81         data.success(PDP1);
82         assertFalse(data.isEmpty());
83
84         data.fail(PDP2);
85         assertFalse(data.isEmpty());
86
87         data.removePdp(PDP1);
88         assertFalse(data.isEmpty());
89
90         data.removePdp(PDP2);
91         assertTrue(data.isEmpty());
92     }
93
94     @Test
95     public void testPutValuesInto() {
96         data.addPdps(fullSet);
97         data.success(PDP1);
98         data.fail(PDP2);
99         data.fail(PDP3);
100
101         PolicyStatus status = new PolicyStatus();
102         data.putValuesInto(status);
103
104         assertEquals(1, status.getSuccessCount());
105         assertEquals(2, status.getFailureCount());
106         assertEquals(3, status.getIncompleteCount());
107     }
108
109     @Test
110     public void testAddPdps_testSuccess_testFail() {
111         data.addPdps(Arrays.asList(PDP1, PDP2, PDP3, PDP4));
112         assertEquals("[0, 0, 4]", getCounts().toString());
113
114         data.success(PDP1);
115         assertEquals("[1, 0, 3]", getCounts().toString());
116
117         data.success(PDP2);
118         assertEquals("[2, 0, 2]", getCounts().toString());
119
120         // repeat
121         data.success(PDP2);
122         assertEquals("[2, 0, 2]", getCounts().toString());
123
124         data.fail(PDP3);
125         assertEquals("[2, 1, 1]", getCounts().toString());
126
127         // repeat
128         data.fail(PDP3);
129         assertEquals("[2, 1, 1]", getCounts().toString());
130
131         data.addPdps(Arrays.asList(PDP2, PDP3, PDP4, PDP5));
132
133         // PDP1 is still success
134         assertEquals("[1, 0, 4]", getCounts().toString());
135     }
136
137     @Test
138     public void testRemovePdps() {
139         data.addPdps(Arrays.asList(PDP1, PDP2, PDP3, PDP4, PDP5, PDP6));
140         data.success(PDP1);
141         data.success(PDP2);
142         data.fail(PDP3);
143         data.fail(PDP4);
144         assertFalse(data.removePdps(Arrays.asList(PDP1, PDP3, PDP5)));
145         assertEquals("[1, 1, 1]", getCounts().toString());
146     }
147
148     /**
149      * Tests removePdps(), where nothing is removed from the "incomplete" set.
150      */
151     @Test
152     public void testRemovePdpsNoIncompleteRemove() {
153         assertFalse(data.removePdps(Arrays.asList(PDP1, PDP2)));
154         assertEquals("[0, 0, 0]", getCounts().toString());
155     }
156
157     /**
158      * Tests removePdps(), where remaining incomplete items are removed.
159      */
160     @Test
161     public void testRemovePdpsAllComplete() {
162         data.addPdps(Arrays.asList(PDP1));
163         assertTrue(data.removePdps(Arrays.asList(PDP1)));
164
165         data.addPdps(Arrays.asList(PDP1, PDP2, PDP3));
166         assertFalse(data.removePdps(Arrays.asList(PDP1)));
167         assertTrue(data.removePdps(Arrays.asList(PDP2, PDP3)));
168     }
169
170     @Test
171     public void testRemovePdp() {
172         data.addPdps(Arrays.asList(PDP1, PDP2, PDP3, PDP4, PDP5, PDP6));
173         data.success(PDP1);
174         data.success(PDP2);
175         data.fail(PDP3);
176         data.fail(PDP4);
177
178         assertFalse(data.removePdp(PDP1));
179         assertEquals("[1, 2, 2]", getCounts().toString());
180
181         assertFalse(data.removePdp(PDP2));
182         assertEquals("[0, 2, 2]", getCounts().toString());
183
184         assertFalse(data.removePdp(PDP3));
185         assertEquals("[0, 1, 2]", getCounts().toString());
186
187         assertFalse(data.removePdp(PDP4));
188         assertEquals("[0, 0, 2]", getCounts().toString());
189
190         assertFalse(data.removePdp(PDP5));
191         assertEquals("[0, 0, 1]", getCounts().toString());
192
193         assertTrue(data.removePdp(PDP6));
194         assertEquals("[0, 0, 0]", getCounts().toString());
195     }
196
197     /**
198      * Tests removePdps(), where nothing is removed from the "incomplete" set.
199      */
200     @Test
201     public void testRemovePdpNoIncompleteRemove() {
202         assertFalse(data.removePdp(PDP1));
203         assertEquals("[0, 0, 0]", getCounts().toString());
204     }
205
206     /**
207      * Tests removePdps(), where remaining incomplete items are removed.
208      */
209     @Test
210     public void testRemovePdpAllComplete() {
211         data.addPdps(Arrays.asList(PDP1, PDP2));
212         assertFalse(data.removePdp(PDP1));
213
214         assertTrue(data.removePdp(PDP2));
215     }
216
217     @Test
218     public void testComplete() {
219         // attempt to remove a PDP that isn't in the data
220         assertFalse(data.success(PDP1));
221
222         // remove one that was incomplete
223         data.addPdps(Arrays.asList(PDP1));
224         assertTrue(data.success(PDP1));
225
226         // move from one set to the other
227         assertTrue(data.fail(PDP1));
228
229         // already in the correct set
230         assertFalse(data.fail(PDP1));
231     }
232
233     private List<Integer> getCounts() {
234         PolicyStatus status = new PolicyStatus();
235         data.putValuesInto(status);
236
237         return Arrays.asList(status.getSuccessCount(), status.getFailureCount(), status.getIncompleteCount());
238     }
239 }