aboutsummaryrefslogtreecommitdiffstats
path: root/main/src/test/java/org/onap/policy/pap/main/notification/PolicyTrackerDataTest.java
blob: ff797809672c33faf0524b97eac317ad648c8233 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
/*-
 * ============LICENSE_START=======================================================
 * ONAP PAP
 * ================================================================================
 * Copyright (C) 2019-2020 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.onap.policy.pap.main.notification;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertTrue;

import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import org.junit.Before;
import org.junit.Test;
import org.onap.policy.models.pap.concepts.PolicyStatus;
import org.onap.policy.models.tosca.authorative.concepts.ToscaPolicyTypeIdentifier;

public class PolicyTrackerDataTest {

    private static final ToscaPolicyTypeIdentifier TYPE = new ToscaPolicyTypeIdentifier("my-type", "1.2.3");
    private static final String PDP1 = "pdp-1";
    private static final String PDP2 = "pdp-2";
    private static final String PDP3 = "pdp-3";
    private static final String PDP4 = "pdp-4";
    private static final String PDP5 = "pdp-5";
    private static final String PDP6 = "pdp-6";

    private Collection<String> fullSet;
    private PolicyTrackerData data;

    @Before
    public void setUp() {
        fullSet = Arrays.asList(PDP1, PDP2, PDP3, PDP4, PDP5, PDP6);
        data = new PolicyTrackerData(TYPE);
    }

    @Test
    public void testPolicyTrackerData_testGetPolicyType() {
        assertSame(TYPE, data.getPolicyType());
    }

    @Test
    public void testIsComplete() {
        assertTrue(data.isComplete());

        data.addPdps(Arrays.asList(PDP1, PDP2));
        assertFalse(data.isComplete());

        data.success(PDP1);
        assertFalse(data.isComplete());

        data.fail(PDP2);
        assertTrue(data.isComplete());
    }

    @Test
    public void testAllSucceeded() {
        assertTrue(data.allSucceeded());

        data.addPdps(Arrays.asList(PDP1, PDP2));
        assertFalse(data.allSucceeded());

        data.success(PDP1);
        assertFalse(data.allSucceeded());

        data.fail(PDP2);
        assertFalse(data.allSucceeded());

        data.success(PDP2);
        assertTrue(data.allSucceeded());

        data.fail(PDP2);
        assertFalse(data.allSucceeded());

        data.success(PDP2);
        assertTrue(data.allSucceeded());
    }

    @Test
    public void testIsEmpty() {
        assertTrue(data.isEmpty());

        data.addPdps(Arrays.asList(PDP1, PDP2));
        assertFalse(data.isEmpty());

        data.success(PDP1);
        assertFalse(data.isEmpty());

        data.fail(PDP2);
        assertFalse(data.isEmpty());

        data.removePdp(PDP1);
        assertFalse(data.isEmpty());

        data.removePdp(PDP2);
        assertTrue(data.isEmpty());
    }

    @Test
    public void testPutValuesInto() {
        data.addPdps(fullSet);
        data.success(PDP1);
        data.fail(PDP2);
        data.fail(PDP3);

        PolicyStatus status = new PolicyStatus();
        data.putValuesInto(status);

        assertEquals(1, status.getSuccessCount());
        assertEquals(2, status.getFailureCount());
        assertEquals(3, status.getIncompleteCount());
    }

    @Test
    public void testAddPdps_testSuccess_testFail() {
        data.addPdps(Arrays.asList(PDP1, PDP2, PDP3, PDP4));
        assertEquals("[0, 0, 4]", getCounts().toString());

        data.success(PDP1);
        assertEquals("[1, 0, 3]", getCounts().toString());

        data.success(PDP2);
        assertEquals("[2, 0, 2]", getCounts().toString());

        // repeat
        data.success(PDP2);
        assertEquals("[2, 0, 2]", getCounts().toString());

        data.fail(PDP3);
        assertEquals("[2, 1, 1]", getCounts().toString());

        // repeat
        data.fail(PDP3);
        assertEquals("[2, 1, 1]", getCounts().toString());

        data.addPdps(Arrays.asList(PDP2, PDP3, PDP4, PDP5));

        // PDP1 is still success
        assertEquals("[1, 0, 4]", getCounts().toString());
    }

    @Test
    public void testRemovePdps() {
        data.addPdps(Arrays.asList(PDP1, PDP2, PDP3, PDP4, PDP5, PDP6));
        data.success(PDP1);
        data.success(PDP2);
        data.fail(PDP3);
        data.fail(PDP4);
        assertFalse(data.removePdps(Arrays.asList(PDP1, PDP3, PDP5)));
        assertEquals("[1, 1, 1]", getCounts().toString());

        assertTrue(data.removePdps(Arrays.asList(PDP6)));
        assertEquals("[1, 1, 0]", getCounts().toString());
    }

    /**
     * Tests removePdps(), where nothing is removed from the "incomplete" set.
     */
    @Test
    public void testRemovePdpsNoIncompleteRemove() {
        assertFalse(data.removePdps(Arrays.asList(PDP1, PDP2)));
        assertEquals("[0, 0, 0]", getCounts().toString());
    }

    /**
     * Tests removePdps(), where remaining incomplete items are removed.
     */
    @Test
    public void testRemovePdpsAllComplete() {
        data.addPdps(Arrays.asList(PDP1));
        assertTrue(data.removePdps(Arrays.asList(PDP1)));

        data.addPdps(Arrays.asList(PDP1, PDP2, PDP3));
        assertFalse(data.removePdps(Arrays.asList(PDP1)));
        assertTrue(data.removePdps(Arrays.asList(PDP2, PDP3)));
    }

    /**
     * Tests removePdps() with more variations.
     */
    @Test
    public void testRemovePdpsVariations() {
        data.addPdps(Arrays.asList(PDP1, PDP2, PDP3));
        data.success(PDP1);
        data.fail(PDP2);
        assertEquals("[1, 1, 1]", getCounts().toString());

        // remove PDP1, which checks removal from "success" set, while incomplete
        assertFalse(data.removePdps(Arrays.asList(PDP1)));
        assertEquals("[0, 1, 1]", getCounts().toString());

        // remove PDP2, which checks removal from "failure" set, while incomplete
        assertFalse(data.removePdps(Arrays.asList(PDP2)));
        assertEquals("[0, 0, 1]", getCounts().toString());

        // re-add 1 & 2
        data.addPdps(Arrays.asList(PDP1, PDP2));
        data.success(PDP1);
        data.fail(PDP2);
        assertEquals("[1, 1, 1]", getCounts().toString());

        // remove PDP3, which checks removal from "incomplete" set
        assertTrue(data.removePdps(Arrays.asList(PDP3)));
        assertEquals("[1, 1, 0]", getCounts().toString());

        // remove PDP1, which checks removal from "success" set, while complete
        assertTrue(data.removePdps(Arrays.asList(PDP1)));
        assertEquals("[0, 1, 0]", getCounts().toString());

        // remove PDP2, which checks removal from "failure" set, while complete
        assertTrue(data.removePdps(Arrays.asList(PDP2)));
        assertEquals("[0, 0, 0]", getCounts().toString());

        // re-add 1 and then remove it again
        data.addPdps(Arrays.asList(PDP1));
        assertTrue(data.removePdps(Arrays.asList(PDP1)));
        assertEquals("[0, 0, 0]", getCounts().toString());
    }

    @Test
    public void testRemovePdp() {
        data.addPdps(Arrays.asList(PDP1, PDP2, PDP3, PDP4, PDP5, PDP6));
        data.success(PDP1);
        data.success(PDP2);
        data.fail(PDP3);
        data.fail(PDP4);

        assertFalse(data.removePdp(PDP1));
        assertEquals("[1, 2, 2]", getCounts().toString());

        assertFalse(data.removePdp(PDP2));
        assertEquals("[0, 2, 2]", getCounts().toString());

        assertFalse(data.removePdp(PDP3));
        assertEquals("[0, 1, 2]", getCounts().toString());

        assertFalse(data.removePdp(PDP4));
        assertEquals("[0, 0, 2]", getCounts().toString());

        assertFalse(data.removePdp(PDP5));
        assertEquals("[0, 0, 1]", getCounts().toString());

        assertTrue(data.removePdp(PDP6));
        assertEquals("[0, 0, 0]", getCounts().toString());
    }

    /**
     * Tests removePdps(), where nothing is removed from the "incomplete" set.
     */
    @Test
    public void testRemovePdpNoIncompleteRemove() {
        assertFalse(data.removePdp(PDP1));
        assertEquals("[0, 0, 0]", getCounts().toString());
    }

    /**
     * Tests removePdps(), where remaining incomplete items are removed.
     */
    @Test
    public void testRemovePdpAllComplete() {
        data.addPdps(Arrays.asList(PDP1, PDP2));
        assertFalse(data.removePdp(PDP1));

        assertTrue(data.removePdp(PDP2));
    }

    /**
     * Tests removePdp() with more variations.
     */
    @Test
    public void testRemovePdpVariations() {
        data.addPdps(Arrays.asList(PDP1, PDP2, PDP3));
        data.success(PDP1);
        data.fail(PDP2);
        assertEquals("[1, 1, 1]", getCounts().toString());

        // remove PDP1, which checks removal from "success" set, while incomplete
        assertFalse(data.removePdp(PDP1));
        assertEquals("[0, 1, 1]", getCounts().toString());

        // remove PDP2, which checks removal from "failure" set, while incomplete
        assertFalse(data.removePdp(PDP2));
        assertEquals("[0, 0, 1]", getCounts().toString());

        // re-add 1 & 2
        data.addPdps(Arrays.asList(PDP1, PDP2));
        data.success(PDP1);
        data.fail(PDP2);
        assertEquals("[1, 1, 1]", getCounts().toString());

        // remove PDP3, which checks removal from "incomplete" set
        assertTrue(data.removePdp(PDP3));
        assertEquals("[1, 1, 0]", getCounts().toString());

        // remove PDP1, which checks removal from "success" set, while complete
        assertTrue(data.removePdp(PDP1));
        assertEquals("[0, 1, 0]", getCounts().toString());

        // remove PDP2, which checks removal from "failure" set, while complete
        assertTrue(data.removePdp(PDP2));
        assertEquals("[0, 0, 0]", getCounts().toString());

        // re-add 1 and then remove it again
        data.addPdps(Arrays.asList(PDP1));
        assertTrue(data.removePdp(PDP1));
        assertEquals("[0, 0, 0]", getCounts().toString());
    }

    @Test
    public void testComplete() {
        // attempt to remove a PDP that isn't in the data
        assertFalse(data.success(PDP1));

        // remove one that was incomplete
        data.addPdps(Arrays.asList(PDP1));
        assertTrue(data.success(PDP1));

        // move from one set to the other
        assertTrue(data.fail(PDP1));

        // already in the correct set
        assertFalse(data.fail(PDP1));
    }

    private List<Integer> getCounts() {
        PolicyStatus status = new PolicyStatus();
        data.putValuesInto(status);

        return Arrays.asList(status.getSuccessCount(), status.getFailureCount(), status.getIncompleteCount());
    }
}