2 * ============LICENSE_START=======================================================
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
13 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
23 package org.onap.policy.std.test;
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;
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;
49 * The class <code>MatchStoreTest</code> contains tests for the class
50 * <code>{@link MatchStore}</code>.
52 * @generatedBy CodePro at 6/1/16 1:41 PM
53 * @version $Revision: 1.0 $
55 public class MatchStoreTest {
57 private static final String ATTRIBUTE_DUMMY_VALUE = "Value";
59 private static final String ATTRIBUTE_DUMMY_KEY = "Key";
61 private static final String POLICY_NAME = "ONAP";
63 private static final String POLICY_VERSION = "1.0.0";
65 private static final String ONAP_NAME_VAL = "ONAPName";
67 private static final String CONFIG_NAME_VAL = "ConfigName";
69 private static final String EMPTY_STRING = "";
72 * Perform pre-test initialization.
74 * @throws Exception if the initialization fails for some reason
77 public void setUp() throws Exception {
78 MatchStore.getMatchStore().clear();
82 public void testCheckMatch_ShouldReturnNullWithNullPDPNotification() throws Exception {
83 final PDPNotification oldNotification = null;
85 final PDPNotification result = MatchStore.checkMatch(oldNotification);
91 public void testCheckMatch_ShouldReturnNullIfMatchStoreCacheIsEmpty() throws Exception {
92 final PDPNotification oldNotification = new StdPDPNotification();
94 final PDPNotification result = MatchStore.checkMatch(oldNotification);
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();
104 final PDPNotification result = MatchStore.checkMatch(oldNotification);
105 assertNotNull(result);
109 public void testGetMatchStore_ShouldNotBeNullOnStartUP() throws Exception {
110 final Set<Matches> result = MatchStore.getMatchStore();
111 assertNotNull(result);
115 public void testStoreMatch_ShouldRetrunEmptyLoadedRemovedPolicesIfNotMatchFoundInMatchStore()
117 final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL);
119 MatchStore.storeMatch(newMatch);
121 assertFalse(MatchStore.getMatchStore().isEmpty());
123 final PDPNotification oldNotification = new StdPDPNotification();
124 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
126 assertTrue(actualPDPNotification.getLoadedPolicies().isEmpty());
127 assertTrue(actualPDPNotification.getRemovedPolicies().isEmpty());
128 assertNull(actualPDPNotification.getNotificationType());
132 public void testStoreMatch_NotificationTypeRemoved_IfRemovedPolicyExistInOldNotification()
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());
139 MatchStore.storeMatch(newMatch);
141 assertFalse(MatchStore.getMatchStore().isEmpty());
143 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
145 assertEquals(NotificationType.REMOVE, actualPDPNotification.getNotificationType());
146 assertFalse(actualPDPNotification.getRemovedPolicies().isEmpty());
151 public void testStoreMatch_NoticficationTypeUpdate_IfStdLoadPolicyExistsWithEmptyMatches()
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));
159 MatchStore.storeMatch(newMatch);
161 assertFalse(MatchStore.getMatchStore().isEmpty());
163 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
165 assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
166 assertFalse(actualPDPNotification.getLoadedPolicies().isEmpty());
170 public void testStoreMatch_NoticficationTypeUpdate_IfStdLoadPolicyExistsWithMatches()
172 final Map<String, String> attribute = getAttributesMap();
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);
178 final StdLoadedPolicy stdLoadedPolicy =
179 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
180 final PDPNotification oldNotification =
181 getPDPNotification(Collections.emptySet(), Arrays.asList(stdLoadedPolicy));
183 MatchStore.storeMatch(newMatch);
185 assertFalse(MatchStore.getMatchStore().isEmpty());
187 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
189 assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
191 final Collection<LoadedPolicy> actualLoadPolicies =
192 actualPDPNotification.getLoadedPolicies();
193 assertFalse(actualLoadPolicies.isEmpty());
195 final LoadedPolicy loadedPolicy = actualLoadPolicies.iterator().next();
196 assertEquals(stdLoadedPolicy.getPolicyName(), loadedPolicy.getPolicyName());
197 assertEquals(stdLoadedPolicy.getVersionNo(), loadedPolicy.getVersionNo());
201 public void testStoreMatch_NoticficationTypeUpdate_IfStdLoadPolicyExistsWithNullMatches()
203 final Map<String, String> attribute = getAttributesMap();
205 final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, attribute);
207 final StdLoadedPolicy stdLoadedPolicy =
208 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, null);
209 final PDPNotification oldNotification =
210 getPDPNotification(Collections.emptySet(), Arrays.asList(stdLoadedPolicy));
212 MatchStore.storeMatch(newMatch);
214 assertFalse(MatchStore.getMatchStore().isEmpty());
216 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
218 assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
220 final Collection<LoadedPolicy> actualLoadPolicies =
221 actualPDPNotification.getLoadedPolicies();
222 assertFalse(actualLoadPolicies.isEmpty());
224 final LoadedPolicy loadedPolicy = actualLoadPolicies.iterator().next();
225 assertEquals(stdLoadedPolicy.getPolicyName(), loadedPolicy.getPolicyName());
226 assertEquals(stdLoadedPolicy.getVersionNo(), loadedPolicy.getVersionNo());
230 public void testStoreMatch_NoticficationTypeNull_IfStdLoadPolicyExistsWithMatchesWithOutMatchingConfigAttribute()
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);
236 final StdLoadedPolicy stdLoadedPolicy =
237 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
238 final PDPNotification oldNotification =
239 getPDPNotification(Collections.emptySet(), Arrays.asList(stdLoadedPolicy));
241 MatchStore.storeMatch(newMatch);
243 assertFalse(MatchStore.getMatchStore().isEmpty());
245 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
247 assertNull(actualPDPNotification.getNotificationType());
249 final Collection<LoadedPolicy> actualLoadPolicies =
250 actualPDPNotification.getLoadedPolicies();
251 assertTrue(actualLoadPolicies.isEmpty());
256 public void testStoreMatch_NoticficationTypeUpdate_IfMatchStoreContainMatchesWithNullConStdLoadPolicyExistsWithMatches()
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);
263 final StdLoadedPolicy stdLoadedPolicy =
264 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
265 final PDPNotification oldNotification =
266 getPDPNotification(Collections.emptySet(), Arrays.asList(stdLoadedPolicy));
268 MatchStore.storeMatch(newMatch);
270 assertFalse(MatchStore.getMatchStore().isEmpty());
272 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
274 assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
276 final Collection<LoadedPolicy> actualLoadPolicies =
277 actualPDPNotification.getLoadedPolicies();
278 assertFalse(actualLoadPolicies.isEmpty());
283 public void testStoreMatch_NoticficationTypeUpdate_IfMatchStoreContainMatchingMatches()
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);
290 final StdLoadedPolicy stdLoadedPolicy =
291 getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
292 final PDPNotification oldNotification =
293 getPDPNotification(Collections.emptySet(), Arrays.asList(stdLoadedPolicy));
295 MatchStore.storeMatch(newMatch);
297 assertFalse(MatchStore.getMatchStore().isEmpty());
299 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
301 assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
303 final Collection<LoadedPolicy> actualLoadPolicies =
304 actualPDPNotification.getLoadedPolicies();
305 assertFalse(actualLoadPolicies.isEmpty());
307 final LoadedPolicy loadedPolicy = actualLoadPolicies.iterator().next();
308 assertEquals(stdLoadedPolicy.getPolicyName(), loadedPolicy.getPolicyName());
309 assertEquals(stdLoadedPolicy.getVersionNo(), loadedPolicy.getVersionNo());
313 public void testStoreMatch_NoticficationTypeUpdate_IfMatchStoreContainMatchingMatches2()
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();
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);
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));
333 MatchStore.storeMatch(firstObj);
334 MatchStore.storeMatch(secondObj);
336 assertFalse(MatchStore.getMatchStore().isEmpty());
338 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
340 assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
342 final Collection<LoadedPolicy> actualLoadPolicies =
343 actualPDPNotification.getLoadedPolicies();
344 assertFalse(actualLoadPolicies.isEmpty());
345 assertEquals(1, actualLoadPolicies.size());
349 public void testStoreMatch_NoticficationTypeBoth_IfOldNotificationContainRemovedAndLoadedPolicies()
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);
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));
362 MatchStore.storeMatch(newMatch);
364 assertFalse(MatchStore.getMatchStore().isEmpty());
366 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
368 assertEquals(NotificationType.BOTH, actualPDPNotification.getNotificationType());
369 assertFalse(actualPDPNotification.getRemovedPolicies().isEmpty());
370 assertFalse(actualPDPNotification.getLoadedPolicies().isEmpty());
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);
381 public void testStoreMatch_MatchesObjectShouldbeAddOnceToMatchStoreAndNoDuplication()
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};
388 for (int i = 0; i < configNames.length; i++) {
390 final Matches matches =
391 getMatchesInstance(configNames[i], onapNames[i], getAttributesMap());
392 MatchStore.storeMatch(matches);
393 MatchStore.storeMatch(matches);
396 assertEquals(configNames.length - 1, MatchStore.getMatchStore().size());
401 public void testStoreMatch_MatchesObjectShouldBeAddedToMatchStore_ConfigAttrValuesAreDifferentThenExistingOne()
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());
408 MatchStore.storeMatch(firstObj);
409 MatchStore.storeMatch(secondObj);
410 MatchStore.storeMatch(firstObj);
412 assertEquals(2, MatchStore.getMatchStore().size());
417 public void testStoreMatch_MatchesObjectShouldBeAddedToMatchStore_ConfigAttrValuesNull()
419 final Matches firstObj = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, null);
420 final Matches secondObj = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, null);
422 MatchStore.storeMatch(firstObj);
423 MatchStore.storeMatch(secondObj);
424 MatchStore.storeMatch(firstObj);
426 assertEquals(1, MatchStore.getMatchStore().size());
431 public void testStoreMatch_MatchesObjectShouldBeAddedToMatchStore_OnapNameIsDifferentThenExistingOne()
433 final Matches firstObj =
434 getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, getAttributesMap());
435 final Matches secondObj =
436 getMatchesInstance(CONFIG_NAME_VAL, "ONAPName1", getAttributesMap());
438 MatchStore.storeMatch(firstObj);
439 MatchStore.storeMatch(secondObj);
440 MatchStore.storeMatch(firstObj);
442 assertEquals(2, MatchStore.getMatchStore().size());
446 private Map<String, String> getAttributesMap() {
447 final Map<String, String> attribute = new HashMap<>();
448 attribute.put(ATTRIBUTE_DUMMY_KEY, ATTRIBUTE_DUMMY_VALUE);
452 private Matches getMatchesInstance(final String configName, final String onapName) {
453 return getMatchesInstance(configName, onapName, null);
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);
465 private StdRemovedPolicy getRemovedPolicy(final String version, final String policyName) {
466 return new StdRemovedPolicy() {
469 public String getVersionNo() {
474 public String getPolicyName() {
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;
488 private StdLoadedPolicy getStdLoadedPolicy(final String version, final String policyName,
489 final Map<String, String> matches) {
490 return new StdLoadedPolicy() {
493 public String getVersionNo() {
498 public String getPolicyName() {
503 public Map<String, String> getMatches() {
510 * Perform post-test clean-up.
512 * @throws Exception if the clean-up fails for some reason
515 public void tearDown() throws Exception {
516 // Add additional tear down code here
522 * @param args the command line arguments
524 public static void main(final String[] args) {
525 new org.junit.runner.JUnitCore().run(MatchStoreTest.class);