e3ea735635fc1af2e73cb83db3bdb6e93e5b4b10
[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  * 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.std.test;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertFalse;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Collections;
32 import java.util.HashMap;
33 import java.util.Map;
34 import java.util.Set;
35
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
75      *             if the initialization fails for some reason
76      */
77     @Before
78     public void setUp() throws Exception {
79         MatchStore.getMatchStore().clear();
80     }
81
82     @Test
83     public void testCheckMatch_ShouldReturnNullWithNullPDPNotification() throws Exception {
84         final PDPNotification oldNotification = null;
85
86         final PDPNotification result = MatchStore.checkMatch(oldNotification);
87
88         assertNull(result);
89     }
90
91     @Test
92     public void testCheckMatch_ShouldReturnNullIfMatchStoreCacheIsEmpty() throws Exception {
93         final PDPNotification oldNotification = new StdPDPNotification();
94
95         final PDPNotification result = MatchStore.checkMatch(oldNotification);
96         assertNull(result);
97     }
98
99     @Test
100     public void testCheckMatch_ShouldNotReturnNullIfMatchStoreCacheIsNotEmpty() throws Exception {
101         final Matches newMatch = getMatchesInstance(EMPTY_STRING, EMPTY_STRING);
102         MatchStore.storeMatch(newMatch);
103         final PDPNotification oldNotification = new StdPDPNotification();
104
105         final PDPNotification result = MatchStore.checkMatch(oldNotification);
106         assertNotNull(result);
107     }
108
109     @Test
110     public void testGetMatchStore_ShouldNotBeNullOnStartUP() throws Exception {
111         final Set<Matches> result = MatchStore.getMatchStore();
112         assertNotNull(result);
113     }
114
115     @Test
116     public void testStoreMatch_ShouldRetrunEmptyLoadedRemovedPolicesIfNotMatchFoundInMatchStore() 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() throws Exception {
133         final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL);
134         final StdRemovedPolicy removedPolicy = getRemovedPolicy(POLICY_VERSION, POLICY_NAME);
135         final PDPNotification oldNotification = getPDPNotification(Arrays.asList(removedPolicy),
136                 Collections.emptySet());
137
138         MatchStore.storeMatch(newMatch);
139
140         assertFalse(MatchStore.getMatchStore().isEmpty());
141
142         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
143
144         assertEquals(NotificationType.REMOVE, actualPDPNotification.getNotificationType());
145         assertFalse(actualPDPNotification.getRemovedPolicies().isEmpty());
146
147     }
148
149     @Test
150     public void testStoreMatch_NoticficationTypeUpdate_IfStdLoadPolicyExistsWithEmptyMatches() throws Exception {
151         final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL);
152         final StdLoadedPolicy stdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, Collections.emptyMap());
153         final PDPNotification oldNotification = getPDPNotification(Collections.emptySet(),
154                 Arrays.asList(stdLoadedPolicy));
155
156         MatchStore.storeMatch(newMatch);
157
158         assertFalse(MatchStore.getMatchStore().isEmpty());
159
160         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
161
162         assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
163         assertFalse(actualPDPNotification.getLoadedPolicies().isEmpty());
164     }
165
166     @Test
167     public void testStoreMatch_NoticficationTypeUpdate_IfStdLoadPolicyExistsWithMatches() throws Exception {
168         final Map<String, String> attribute = getAttributesMap();
169
170         final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, attribute);
171         final Map<String, String> matches = getPolicyMatches(ONAP_NAME_VAL, CONFIG_NAME_VAL);
172         matches.putAll(attribute);
173
174         final StdLoadedPolicy stdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
175         final PDPNotification oldNotification = getPDPNotification(Collections.emptySet(),
176                 Arrays.asList(stdLoadedPolicy));
177
178         MatchStore.storeMatch(newMatch);
179
180         assertFalse(MatchStore.getMatchStore().isEmpty());
181
182         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
183
184         assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
185
186         final Collection<LoadedPolicy> actualLoadPolicies = actualPDPNotification.getLoadedPolicies();
187         assertFalse(actualLoadPolicies.isEmpty());
188
189         final LoadedPolicy loadedPolicy = actualLoadPolicies.iterator().next();
190         assertEquals(stdLoadedPolicy.getPolicyName(), loadedPolicy.getPolicyName());
191         assertEquals(stdLoadedPolicy.getVersionNo(), loadedPolicy.getVersionNo());
192     }
193
194     @Test
195     public void testStoreMatch_NoticficationTypeUpdate_IfStdLoadPolicyExistsWithNullMatches() throws Exception {
196         final Map<String, String> attribute = getAttributesMap();
197
198         final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, attribute);
199
200         final StdLoadedPolicy stdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, null);
201         final PDPNotification oldNotification = getPDPNotification(Collections.emptySet(),
202                 Arrays.asList(stdLoadedPolicy));
203
204         MatchStore.storeMatch(newMatch);
205
206         assertFalse(MatchStore.getMatchStore().isEmpty());
207
208         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
209
210         assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
211
212         final Collection<LoadedPolicy> actualLoadPolicies = actualPDPNotification.getLoadedPolicies();
213         assertFalse(actualLoadPolicies.isEmpty());
214
215         final LoadedPolicy loadedPolicy = actualLoadPolicies.iterator().next();
216         assertEquals(stdLoadedPolicy.getPolicyName(), loadedPolicy.getPolicyName());
217         assertEquals(stdLoadedPolicy.getVersionNo(), loadedPolicy.getVersionNo());
218     }
219
220     @Test
221     public void testStoreMatch_NoticficationTypeNull_IfStdLoadPolicyExistsWithMatchesWithOutMatchingConfigAttribute()
222             throws Exception {
223         final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, getAttributesMap());
224         final Map<String, String> matches = getPolicyMatches(ONAP_NAME_VAL, CONFIG_NAME_VAL);
225
226         final StdLoadedPolicy stdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
227         final PDPNotification oldNotification = getPDPNotification(Collections.emptySet(),
228                 Arrays.asList(stdLoadedPolicy));
229
230         MatchStore.storeMatch(newMatch);
231
232         assertFalse(MatchStore.getMatchStore().isEmpty());
233
234         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
235
236         assertNull(actualPDPNotification.getNotificationType());
237
238         final Collection<LoadedPolicy> actualLoadPolicies = actualPDPNotification.getLoadedPolicies();
239         assertTrue(actualLoadPolicies.isEmpty());
240
241     }
242
243     @Test
244     public void testStoreMatch_NoticficationTypeUpdate_IfMatchStoreContainMatchesWithNullConStdLoadPolicyExistsWithMatches()
245             throws Exception {
246
247         final Matches newMatch = getMatchesInstance(null, ONAP_NAME_VAL, Collections.emptyMap());
248         final Map<String, String> matches = new HashMap<>();
249         matches.put(ONAP_NAME_VAL, ONAP_NAME_VAL);
250
251         final StdLoadedPolicy stdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
252         final PDPNotification oldNotification = getPDPNotification(Collections.emptySet(),
253                 Arrays.asList(stdLoadedPolicy));
254
255         MatchStore.storeMatch(newMatch);
256
257         assertFalse(MatchStore.getMatchStore().isEmpty());
258
259         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
260
261         assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
262
263         final Collection<LoadedPolicy> actualLoadPolicies = actualPDPNotification.getLoadedPolicies();
264         assertFalse(actualLoadPolicies.isEmpty());
265
266     }
267
268     @Test
269     public void testStoreMatch_NoticficationTypeUpdate_IfMatchStoreContainMatchingMatches() throws Exception {
270
271         final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, Collections.emptyMap());
272         final Map<String, String> matches = getPolicyMatches(ONAP_NAME_VAL, CONFIG_NAME_VAL);
273
274         final StdLoadedPolicy stdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
275         final PDPNotification oldNotification = getPDPNotification(Collections.emptySet(),
276                 Arrays.asList(stdLoadedPolicy));
277
278         MatchStore.storeMatch(newMatch);
279
280         assertFalse(MatchStore.getMatchStore().isEmpty());
281
282         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
283
284         assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
285
286         final Collection<LoadedPolicy> actualLoadPolicies = actualPDPNotification.getLoadedPolicies();
287         assertFalse(actualLoadPolicies.isEmpty());
288
289         final LoadedPolicy loadedPolicy = actualLoadPolicies.iterator().next();
290         assertEquals(stdLoadedPolicy.getPolicyName(), loadedPolicy.getPolicyName());
291         assertEquals(stdLoadedPolicy.getVersionNo(), loadedPolicy.getVersionNo());
292     }
293
294     @Test
295     public void testStoreMatch_NoticficationTypeUpdate_IfMatchStoreContainMatchingMatches2() throws Exception {
296
297         final Matches firstObj = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, Collections.emptyMap());
298         final Map<String, String> firstPolicyObj = getPolicyMatches(ONAP_NAME_VAL, CONFIG_NAME_VAL);
299         final Map<String, String> attributesMap = getAttributesMap();
300
301         final Matches secondObj = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, attributesMap);
302         final Map<String, String> secondPolicyObj = getPolicyMatches(ONAP_NAME_VAL, CONFIG_NAME_VAL);
303         secondPolicyObj.putAll(attributesMap);
304
305         final StdLoadedPolicy stdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, firstPolicyObj);
306         final StdLoadedPolicy secondStdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, secondPolicyObj);
307         final PDPNotification oldNotification = getPDPNotification(Collections.emptySet(),
308                 Arrays.asList(stdLoadedPolicy, secondStdLoadedPolicy));
309
310         MatchStore.storeMatch(firstObj);
311         MatchStore.storeMatch(secondObj);
312
313         assertFalse(MatchStore.getMatchStore().isEmpty());
314
315         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
316
317         assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
318
319         final Collection<LoadedPolicy> actualLoadPolicies = actualPDPNotification.getLoadedPolicies();
320         assertFalse(actualLoadPolicies.isEmpty());
321         assertEquals(1, actualLoadPolicies.size());
322     }
323
324     @Test
325     public void testStoreMatch_NoticficationTypeBoth_IfOldNotificationContainRemovedAndLoadedPolicies()
326             throws Exception {
327
328         final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, Collections.emptyMap());
329         final Map<String, String> matches = getPolicyMatches(ONAP_NAME_VAL, CONFIG_NAME_VAL);
330
331         final StdRemovedPolicy removedPolicy = getRemovedPolicy(POLICY_VERSION, POLICY_NAME);
332         final StdLoadedPolicy stdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
333         final PDPNotification oldNotification = getPDPNotification(Arrays.asList(removedPolicy),
334                 Arrays.asList(stdLoadedPolicy));
335
336         MatchStore.storeMatch(newMatch);
337
338         assertFalse(MatchStore.getMatchStore().isEmpty());
339
340         final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
341
342         assertEquals(NotificationType.BOTH, actualPDPNotification.getNotificationType());
343         assertFalse(actualPDPNotification.getRemovedPolicies().isEmpty());
344         assertFalse(actualPDPNotification.getLoadedPolicies().isEmpty());
345     }
346
347     private Map<String, String> getPolicyMatches(final String onapName, final String configName) {
348         final Map<String, String> matches = new HashMap<>();
349         matches.put(ONAP_NAME_VAL, onapName);
350         matches.put(CONFIG_NAME_VAL, configName);
351         return matches;
352     }
353
354     @Test
355     public void testStoreMatch_MatchesObjectShouldbeAddOnceToMatchStoreAndNoDuplication() throws Exception {
356         final String[] configNames = new String[] { CONFIG_NAME_VAL, CONFIG_NAME_VAL, "ConfigName1", CONFIG_NAME_VAL,
357                 "ConfigName1", null };
358         final String[] onapNames = new String[] { ONAP_NAME_VAL, ONAP_NAME_VAL, "ONAPName1", "ONAPName1", ONAP_NAME_VAL,
359                 ONAP_NAME_VAL };
360
361         for (int i = 0; i < configNames.length; i++) {
362
363             final Matches matches = getMatchesInstance(configNames[i], onapNames[i], getAttributesMap());
364             MatchStore.storeMatch(matches);
365             MatchStore.storeMatch(matches);
366         }
367
368         assertEquals(configNames.length - 1, MatchStore.getMatchStore().size());
369
370     }
371
372     @Test
373     public void testStoreMatch_MatchesObjectShouldBeAddedToMatchStore_ConfigAttrValuesAreDifferentThenExistingOne()
374             throws Exception {
375         final Matches firstObj = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, getAttributesMap());
376         final Matches secondObj = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, Collections.emptyMap());
377
378         MatchStore.storeMatch(firstObj);
379         MatchStore.storeMatch(secondObj);
380         MatchStore.storeMatch(firstObj);
381
382         assertEquals(2, MatchStore.getMatchStore().size());
383
384     }
385
386     @Test
387     public void testStoreMatch_MatchesObjectShouldBeAddedToMatchStore_ConfigAttrValuesNull() throws Exception {
388         final Matches firstObj = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, null);
389         final Matches secondObj = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, null);
390
391         MatchStore.storeMatch(firstObj);
392         MatchStore.storeMatch(secondObj);
393         MatchStore.storeMatch(firstObj);
394
395         assertEquals(1, MatchStore.getMatchStore().size());
396
397     }
398
399     @Test
400     public void testStoreMatch_MatchesObjectShouldBeAddedToMatchStore_OnapNameIsDifferentThenExistingOne()
401             throws Exception {
402         final Matches firstObj = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, getAttributesMap());
403         final Matches secondObj = getMatchesInstance(CONFIG_NAME_VAL, "ONAPName1", getAttributesMap());
404
405         MatchStore.storeMatch(firstObj);
406         MatchStore.storeMatch(secondObj);
407         MatchStore.storeMatch(firstObj);
408
409         assertEquals(2, MatchStore.getMatchStore().size());
410
411     }
412
413     private Map<String, String> getAttributesMap() {
414         final Map<String, String> attribute = new HashMap<>();
415         attribute.put(ATTRIBUTE_DUMMY_KEY, ATTRIBUTE_DUMMY_VALUE);
416         return attribute;
417     }
418
419     private Matches getMatchesInstance(final String configName, final String onapName) {
420         return getMatchesInstance(configName, onapName, null);
421     }
422
423     private Matches getMatchesInstance(final String configName, final String onapName,
424             final Map<String, String> configAttributes) {
425         final Matches matches = new Matches();
426         matches.setConfigName(configName);
427         matches.setOnapName(onapName);
428         matches.setConfigAttributes(configAttributes);
429         return matches;
430     }
431
432     private StdRemovedPolicy getRemovedPolicy(final String version, final String policyName) {
433         return new StdRemovedPolicy() {
434
435             @Override
436             public String getVersionNo() {
437                 return version;
438             }
439
440             @Override
441             public String getPolicyName() {
442                 return policyName;
443             }
444         };
445     }
446
447     private PDPNotification getPDPNotification(final Collection<StdRemovedPolicy> removedPolicies,
448             final Collection<StdLoadedPolicy> loadedPolicies) {
449         final StdPDPNotification oldNotification = new StdPDPNotification();
450         oldNotification.setLoadedPolicies(loadedPolicies);
451         oldNotification.setRemovedPolicies(removedPolicies);
452         return oldNotification;
453     }
454
455     private StdLoadedPolicy getStdLoadedPolicy(final String version, final String policyName,
456             final Map<String, String> matches) {
457         return new StdLoadedPolicy() {
458
459             @Override
460             public String getVersionNo() {
461                 return version;
462             }
463
464             @Override
465             public String getPolicyName() {
466                 return policyName;
467             }
468
469             @Override
470             public Map<String, String> getMatches() {
471                 return matches;
472             }
473         };
474     }
475
476     /**
477      * Perform post-test clean-up.
478      *
479      * @throws Exception
480      *             if the clean-up fails for some reason
481      */
482     @After
483     public void tearDown() throws Exception {
484         // Add additional tear down code here
485     }
486
487     /**
488      * Launch the test.
489      *
490      * @param args
491      *            the command line arguments
492      */
493     public static void main(final String[] args) {
494         new org.junit.runner.JUnitCore().run(MatchStoreTest.class);
495     }
496 }