Reformat PolicyEngineAPI test cases
[policy/engine.git] / PolicyEngineAPI / src / test / java / org / onap / policy / std / test / MatchStoreTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * PolicyEngineAPI
4  * ================================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ================================================================================
7  * Modifications Copyright (C) 2019 Samsung
8  * ================================================================================
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *      http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  * ============LICENSE_END=========================================================
21  */
22
23 package org.onap.policy.std.test;
24
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertFalse;
27 import static org.junit.Assert.assertNotNull;
28 import static org.junit.Assert.assertNull;
29 import static org.junit.Assert.assertTrue;
30 import java.util.Arrays;
31 import java.util.Collection;
32 import java.util.Collections;
33 import java.util.HashMap;
34 import java.util.Map;
35 import java.util.Set;
36 import org.junit.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.onap.policy.api.LoadedPolicy;
40 import org.onap.policy.api.NotificationType;
41 import org.onap.policy.api.PDPNotification;
42 import org.onap.policy.std.MatchStore;
43 import org.onap.policy.std.Matches;
44 import org.onap.policy.std.StdLoadedPolicy;
45 import org.onap.policy.std.StdPDPNotification;
46 import org.onap.policy.std.StdRemovedPolicy;
47
48 /**
49  * The class <code>MatchStoreTest</code> contains tests for the class
50  * <code>{@link MatchStore}</code>.
51  *
52  * @generatedBy CodePro at 6/1/16 1:41 PM
53  * @version $Revision: 1.0 $
54  */
55 public class MatchStoreTest {
56
57     private static final String ATTRIBUTE_DUMMY_VALUE = "Value";
58
59     private static final String ATTRIBUTE_DUMMY_KEY = "Key";
60
61     private static final String POLICY_NAME = "ONAP";
62
63     private static final String POLICY_VERSION = "1.0.0";
64
65     private static final String ONAP_NAME_VAL = "ONAPName";
66
67     private static final String CONFIG_NAME_VAL = "ConfigName";
68
69     private static final String EMPTY_STRING = "";
70
71     /**
72      * Perform pre-test initialization.
73      *
74      * @throws Exception if the initialization fails for some reason
75      */
76     @Before
77     public void setUp() throws Exception {
78         MatchStore.getMatchStore().clear();
79     }
80
81     @Test
82     public void testCheckMatch_ShouldReturnNullWithNullPDPNotification() throws Exception {
83         final PDPNotification oldNotification = null;
84
85         final PDPNotification result = MatchStore.checkMatch(oldNotification);
86
87         assertNull(result);
88     }
89
90     @Test
91     public void testCheckMatch_ShouldReturnNullIfMatchStoreCacheIsEmpty() throws Exception {
92         final PDPNotification oldNotification = new StdPDPNotification();
93
94         final PDPNotification result = MatchStore.checkMatch(oldNotification);
95         assertNull(result);
96     }
97
98     @Test
99     public void testCheckMatch_ShouldNotReturnNullIfMatchStoreCacheIsNotEmpty() throws Exception {
100         final Matches newMatch = getMatchesInstance(EMPTY_STRING, EMPTY_STRING);
101         MatchStore.storeMatch(newMatch);
102         final PDPNotification oldNotification = new StdPDPNotification();
103
104         final PDPNotification result = MatchStore.checkMatch(oldNotification);
105         assertNotNull(result);
106     }
107
108     @Test
109     public void testGetMatchStore_ShouldNotBeNullOnStartUP() throws Exception {
110         final Set<Matches> result = MatchStore.getMatchStore();
111         assertNotNull(result);
112     }
113
114     @Test
115     public void testStoreMatch_ShouldRetrunEmptyLoadedRemovedPolicesIfNotMatchFoundInMatchStore()
116             throws Exception {
117         final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL);
118
119         MatchStore.storeMatch(newMatch);
120
121         assertFalse(MatchStore.getMatchStore().isEmpty());
122
123         final PDPNotification oldNotification = new StdPDPNotification();
124         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
125
126         assertTrue(actualPDPNotification.getLoadedPolicies().isEmpty());
127         assertTrue(actualPDPNotification.getRemovedPolicies().isEmpty());
128         assertNull(actualPDPNotification.getNotificationType());
129     }
130
131     @Test
132     public void testStoreMatch_NotificationTypeRemoved_IfRemovedPolicyExistInOldNotification()
133             throws Exception {
134         final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL);
135         final StdRemovedPolicy removedPolicy = getRemovedPolicy(POLICY_VERSION, POLICY_NAME);
136         final PDPNotification oldNotification =
137                 getPDPNotification(Arrays.asList(removedPolicy), Collections.emptySet());
138
139         MatchStore.storeMatch(newMatch);
140
141         assertFalse(MatchStore.getMatchStore().isEmpty());
142
143         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
144
145         assertEquals(NotificationType.REMOVE, actualPDPNotification.getNotificationType());
146         assertFalse(actualPDPNotification.getRemovedPolicies().isEmpty());
147
148     }
149
150     @Test
151     public void testStoreMatch_NoticficationTypeUpdate_IfStdLoadPolicyExistsWithEmptyMatches()
152             throws Exception {
153         final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL);
154         final StdLoadedPolicy stdLoadedPolicy =
155                 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, Collections.emptyMap());
156         final PDPNotification oldNotification =
157                 getPDPNotification(Collections.emptySet(), Arrays.asList(stdLoadedPolicy));
158
159         MatchStore.storeMatch(newMatch);
160
161         assertFalse(MatchStore.getMatchStore().isEmpty());
162
163         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
164
165         assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
166         assertFalse(actualPDPNotification.getLoadedPolicies().isEmpty());
167     }
168
169     @Test
170     public void testStoreMatch_NoticficationTypeUpdate_IfStdLoadPolicyExistsWithMatches()
171             throws Exception {
172         final Map<String, String> attribute = getAttributesMap();
173
174         final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, attribute);
175         final Map<String, String> matches = getPolicyMatches(ONAP_NAME_VAL, CONFIG_NAME_VAL);
176         matches.putAll(attribute);
177
178         final StdLoadedPolicy stdLoadedPolicy =
179                 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
180         final PDPNotification oldNotification =
181                 getPDPNotification(Collections.emptySet(), Arrays.asList(stdLoadedPolicy));
182
183         MatchStore.storeMatch(newMatch);
184
185         assertFalse(MatchStore.getMatchStore().isEmpty());
186
187         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
188
189         assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
190
191         final Collection<LoadedPolicy> actualLoadPolicies =
192                 actualPDPNotification.getLoadedPolicies();
193         assertFalse(actualLoadPolicies.isEmpty());
194
195         final LoadedPolicy loadedPolicy = actualLoadPolicies.iterator().next();
196         assertEquals(stdLoadedPolicy.getPolicyName(), loadedPolicy.getPolicyName());
197         assertEquals(stdLoadedPolicy.getVersionNo(), loadedPolicy.getVersionNo());
198     }
199
200     @Test
201     public void testStoreMatch_NoticficationTypeUpdate_IfStdLoadPolicyExistsWithNullMatches()
202             throws Exception {
203         final Map<String, String> attribute = getAttributesMap();
204
205         final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, attribute);
206
207         final StdLoadedPolicy stdLoadedPolicy =
208                 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, null);
209         final PDPNotification oldNotification =
210                 getPDPNotification(Collections.emptySet(), Arrays.asList(stdLoadedPolicy));
211
212         MatchStore.storeMatch(newMatch);
213
214         assertFalse(MatchStore.getMatchStore().isEmpty());
215
216         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
217
218         assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
219
220         final Collection<LoadedPolicy> actualLoadPolicies =
221                 actualPDPNotification.getLoadedPolicies();
222         assertFalse(actualLoadPolicies.isEmpty());
223
224         final LoadedPolicy loadedPolicy = actualLoadPolicies.iterator().next();
225         assertEquals(stdLoadedPolicy.getPolicyName(), loadedPolicy.getPolicyName());
226         assertEquals(stdLoadedPolicy.getVersionNo(), loadedPolicy.getVersionNo());
227     }
228
229     @Test
230     public void testStoreMatch_NoticficationTypeNull_IfStdLoadPolicyExistsWithMatchesWithOutMatchingConfigAttribute()
231             throws Exception {
232         final Matches newMatch =
233                 getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, getAttributesMap());
234         final Map<String, String> matches = getPolicyMatches(ONAP_NAME_VAL, CONFIG_NAME_VAL);
235
236         final StdLoadedPolicy stdLoadedPolicy =
237                 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
238         final PDPNotification oldNotification =
239                 getPDPNotification(Collections.emptySet(), Arrays.asList(stdLoadedPolicy));
240
241         MatchStore.storeMatch(newMatch);
242
243         assertFalse(MatchStore.getMatchStore().isEmpty());
244
245         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
246
247         assertNull(actualPDPNotification.getNotificationType());
248
249         final Collection<LoadedPolicy> actualLoadPolicies =
250                 actualPDPNotification.getLoadedPolicies();
251         assertTrue(actualLoadPolicies.isEmpty());
252
253     }
254
255     @Test
256     public void testStoreMatch_NoticficationTypeUpdate_IfMatchStoreContainMatchesWithNullConStdLoadPolicyExistsWithMatches()
257             throws Exception {
258
259         final Matches newMatch = getMatchesInstance(null, ONAP_NAME_VAL, Collections.emptyMap());
260         final Map<String, String> matches = new HashMap<>();
261         matches.put(ONAP_NAME_VAL, ONAP_NAME_VAL);
262
263         final StdLoadedPolicy stdLoadedPolicy =
264                 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
265         final PDPNotification oldNotification =
266                 getPDPNotification(Collections.emptySet(), Arrays.asList(stdLoadedPolicy));
267
268         MatchStore.storeMatch(newMatch);
269
270         assertFalse(MatchStore.getMatchStore().isEmpty());
271
272         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
273
274         assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
275
276         final Collection<LoadedPolicy> actualLoadPolicies =
277                 actualPDPNotification.getLoadedPolicies();
278         assertFalse(actualLoadPolicies.isEmpty());
279
280     }
281
282     @Test
283     public void testStoreMatch_NoticficationTypeUpdate_IfMatchStoreContainMatchingMatches()
284             throws Exception {
285
286         final Matches newMatch =
287                 getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, Collections.emptyMap());
288         final Map<String, String> matches = getPolicyMatches(ONAP_NAME_VAL, CONFIG_NAME_VAL);
289
290         final StdLoadedPolicy stdLoadedPolicy =
291                 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
292         final PDPNotification oldNotification =
293                 getPDPNotification(Collections.emptySet(), Arrays.asList(stdLoadedPolicy));
294
295         MatchStore.storeMatch(newMatch);
296
297         assertFalse(MatchStore.getMatchStore().isEmpty());
298
299         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
300
301         assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
302
303         final Collection<LoadedPolicy> actualLoadPolicies =
304                 actualPDPNotification.getLoadedPolicies();
305         assertFalse(actualLoadPolicies.isEmpty());
306
307         final LoadedPolicy loadedPolicy = actualLoadPolicies.iterator().next();
308         assertEquals(stdLoadedPolicy.getPolicyName(), loadedPolicy.getPolicyName());
309         assertEquals(stdLoadedPolicy.getVersionNo(), loadedPolicy.getVersionNo());
310     }
311
312     @Test
313     public void testStoreMatch_NoticficationTypeUpdate_IfMatchStoreContainMatchingMatches2()
314             throws Exception {
315
316         final Matches firstObj =
317                 getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, Collections.emptyMap());
318         final Map<String, String> firstPolicyObj = getPolicyMatches(ONAP_NAME_VAL, CONFIG_NAME_VAL);
319         final Map<String, String> attributesMap = getAttributesMap();
320
321         final Matches secondObj = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, attributesMap);
322         final Map<String, String> secondPolicyObj =
323                 getPolicyMatches(ONAP_NAME_VAL, CONFIG_NAME_VAL);
324         secondPolicyObj.putAll(attributesMap);
325
326         final StdLoadedPolicy stdLoadedPolicy =
327                 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, firstPolicyObj);
328         final StdLoadedPolicy secondStdLoadedPolicy =
329                 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, secondPolicyObj);
330         final PDPNotification oldNotification = getPDPNotification(Collections.emptySet(),
331                 Arrays.asList(stdLoadedPolicy, secondStdLoadedPolicy));
332
333         MatchStore.storeMatch(firstObj);
334         MatchStore.storeMatch(secondObj);
335
336         assertFalse(MatchStore.getMatchStore().isEmpty());
337
338         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
339
340         assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
341
342         final Collection<LoadedPolicy> actualLoadPolicies =
343                 actualPDPNotification.getLoadedPolicies();
344         assertFalse(actualLoadPolicies.isEmpty());
345         assertEquals(1, actualLoadPolicies.size());
346     }
347
348     @Test
349     public void testStoreMatch_NoticficationTypeBoth_IfOldNotificationContainRemovedAndLoadedPolicies()
350             throws Exception {
351
352         final Matches newMatch =
353                 getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, Collections.emptyMap());
354         final Map<String, String> matches = getPolicyMatches(ONAP_NAME_VAL, CONFIG_NAME_VAL);
355
356         final StdRemovedPolicy removedPolicy = getRemovedPolicy(POLICY_VERSION, POLICY_NAME);
357         final StdLoadedPolicy stdLoadedPolicy =
358                 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
359         final PDPNotification oldNotification =
360                 getPDPNotification(Arrays.asList(removedPolicy), Arrays.asList(stdLoadedPolicy));
361
362         MatchStore.storeMatch(newMatch);
363
364         assertFalse(MatchStore.getMatchStore().isEmpty());
365
366         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
367
368         assertEquals(NotificationType.BOTH, actualPDPNotification.getNotificationType());
369         assertFalse(actualPDPNotification.getRemovedPolicies().isEmpty());
370         assertFalse(actualPDPNotification.getLoadedPolicies().isEmpty());
371     }
372
373     private Map<String, String> getPolicyMatches(final String onapName, final String configName) {
374         final Map<String, String> matches = new HashMap<>();
375         matches.put(ONAP_NAME_VAL, onapName);
376         matches.put(CONFIG_NAME_VAL, configName);
377         return matches;
378     }
379
380     @Test
381     public void testStoreMatch_MatchesObjectShouldbeAddOnceToMatchStoreAndNoDuplication()
382             throws Exception {
383         final String[] configNames = new String[] {CONFIG_NAME_VAL, CONFIG_NAME_VAL, "ConfigName1",
384                 CONFIG_NAME_VAL, "ConfigName1", null};
385         final String[] onapNames = new String[] {ONAP_NAME_VAL, ONAP_NAME_VAL, "ONAPName1",
386                 "ONAPName1", ONAP_NAME_VAL, ONAP_NAME_VAL};
387
388         for (int i = 0; i < configNames.length; i++) {
389
390             final Matches matches =
391                     getMatchesInstance(configNames[i], onapNames[i], getAttributesMap());
392             MatchStore.storeMatch(matches);
393             MatchStore.storeMatch(matches);
394         }
395
396         assertEquals(configNames.length - 1, MatchStore.getMatchStore().size());
397
398     }
399
400     @Test
401     public void testStoreMatch_MatchesObjectShouldBeAddedToMatchStore_ConfigAttrValuesAreDifferentThenExistingOne()
402             throws Exception {
403         final Matches firstObj =
404                 getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, getAttributesMap());
405         final Matches secondObj =
406                 getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, Collections.emptyMap());
407
408         MatchStore.storeMatch(firstObj);
409         MatchStore.storeMatch(secondObj);
410         MatchStore.storeMatch(firstObj);
411
412         assertEquals(2, MatchStore.getMatchStore().size());
413
414     }
415
416     @Test
417     public void testStoreMatch_MatchesObjectShouldBeAddedToMatchStore_ConfigAttrValuesNull()
418             throws Exception {
419         final Matches firstObj = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, null);
420         final Matches secondObj = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, null);
421
422         MatchStore.storeMatch(firstObj);
423         MatchStore.storeMatch(secondObj);
424         MatchStore.storeMatch(firstObj);
425
426         assertEquals(1, MatchStore.getMatchStore().size());
427
428     }
429
430     @Test
431     public void testStoreMatch_MatchesObjectShouldBeAddedToMatchStore_OnapNameIsDifferentThenExistingOne()
432             throws Exception {
433         final Matches firstObj =
434                 getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, getAttributesMap());
435         final Matches secondObj =
436                 getMatchesInstance(CONFIG_NAME_VAL, "ONAPName1", getAttributesMap());
437
438         MatchStore.storeMatch(firstObj);
439         MatchStore.storeMatch(secondObj);
440         MatchStore.storeMatch(firstObj);
441
442         assertEquals(2, MatchStore.getMatchStore().size());
443
444     }
445
446     private Map<String, String> getAttributesMap() {
447         final Map<String, String> attribute = new HashMap<>();
448         attribute.put(ATTRIBUTE_DUMMY_KEY, ATTRIBUTE_DUMMY_VALUE);
449         return attribute;
450     }
451
452     private Matches getMatchesInstance(final String configName, final String onapName) {
453         return getMatchesInstance(configName, onapName, null);
454     }
455
456     private Matches getMatchesInstance(final String configName, final String onapName,
457             final Map<String, String> configAttributes) {
458         final Matches matches = new Matches();
459         matches.setConfigName(configName);
460         matches.setOnapName(onapName);
461         matches.setConfigAttributes(configAttributes);
462         return matches;
463     }
464
465     private StdRemovedPolicy getRemovedPolicy(final String version, final String policyName) {
466         return new StdRemovedPolicy() {
467
468             @Override
469             public String getVersionNo() {
470                 return version;
471             }
472
473             @Override
474             public String getPolicyName() {
475                 return policyName;
476             }
477         };
478     }
479
480     private PDPNotification getPDPNotification(final Collection<StdRemovedPolicy> removedPolicies,
481             final Collection<StdLoadedPolicy> loadedPolicies) {
482         final StdPDPNotification oldNotification = new StdPDPNotification();
483         oldNotification.setLoadedPolicies(loadedPolicies);
484         oldNotification.setRemovedPolicies(removedPolicies);
485         return oldNotification;
486     }
487
488     private StdLoadedPolicy getStdLoadedPolicy(final String version, final String policyName,
489             final Map<String, String> matches) {
490         return new StdLoadedPolicy() {
491
492             @Override
493             public String getVersionNo() {
494                 return version;
495             }
496
497             @Override
498             public String getPolicyName() {
499                 return policyName;
500             }
501
502             @Override
503             public Map<String, String> getMatches() {
504                 return matches;
505             }
506         };
507     }
508
509     /**
510      * Perform post-test clean-up.
511      *
512      * @throws Exception if the clean-up fails for some reason
513      */
514     @After
515     public void tearDown() throws Exception {
516         // Add additional tear down code here
517     }
518
519     /**
520      * Launch the test.
521      *
522      * @param args the command line arguments
523      */
524     public static void main(final String[] args) {
525         new org.junit.runner.JUnitCore().run(MatchStoreTest.class);
526     }
527 }