2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.onap.policy.std.test;
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;
29 import java.util.Arrays;
30 import java.util.Collection;
31 import java.util.Collections;
32 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.
75 * if the initialization fails for some reason
78 public void setUp() throws Exception {
79 MatchStore.getMatchStore().clear();
83 public void testCheckMatch_ShouldReturnNullWithNullPDPNotification() throws Exception {
84 final PDPNotification oldNotification = null;
86 final PDPNotification result = MatchStore.checkMatch(oldNotification);
92 public void testCheckMatch_ShouldReturnNullIfMatchStoreCacheIsEmpty() throws Exception {
93 final PDPNotification oldNotification = new StdPDPNotification();
95 final PDPNotification result = MatchStore.checkMatch(oldNotification);
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();
105 final PDPNotification result = MatchStore.checkMatch(oldNotification);
106 assertNotNull(result);
110 public void testGetMatchStore_ShouldNotBeNullOnStartUP() throws Exception {
111 final Set<Matches> result = MatchStore.getMatchStore();
112 assertNotNull(result);
116 public void testStoreMatch_ShouldRetrunEmptyLoadedRemovedPolicesIfNotMatchFoundInMatchStore() throws Exception {
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() 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());
138 MatchStore.storeMatch(newMatch);
140 assertFalse(MatchStore.getMatchStore().isEmpty());
142 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
144 assertEquals(NotificationType.REMOVE, actualPDPNotification.getNotificationType());
145 assertFalse(actualPDPNotification.getRemovedPolicies().isEmpty());
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));
156 MatchStore.storeMatch(newMatch);
158 assertFalse(MatchStore.getMatchStore().isEmpty());
160 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
162 assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
163 assertFalse(actualPDPNotification.getLoadedPolicies().isEmpty());
167 public void testStoreMatch_NoticficationTypeUpdate_IfStdLoadPolicyExistsWithMatches() throws Exception {
168 final Map<String, String> attribute = getAttributesMap();
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);
174 final StdLoadedPolicy stdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
175 final PDPNotification oldNotification = getPDPNotification(Collections.emptySet(),
176 Arrays.asList(stdLoadedPolicy));
178 MatchStore.storeMatch(newMatch);
180 assertFalse(MatchStore.getMatchStore().isEmpty());
182 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
184 assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
186 final Collection<LoadedPolicy> actualLoadPolicies = actualPDPNotification.getLoadedPolicies();
187 assertFalse(actualLoadPolicies.isEmpty());
189 final LoadedPolicy loadedPolicy = actualLoadPolicies.iterator().next();
190 assertEquals(stdLoadedPolicy.getPolicyName(), loadedPolicy.getPolicyName());
191 assertEquals(stdLoadedPolicy.getVersionNo(), loadedPolicy.getVersionNo());
195 public void testStoreMatch_NoticficationTypeUpdate_IfStdLoadPolicyExistsWithNullMatches() throws Exception {
196 final Map<String, String> attribute = getAttributesMap();
198 final Matches newMatch = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, attribute);
200 final StdLoadedPolicy stdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, null);
201 final PDPNotification oldNotification = getPDPNotification(Collections.emptySet(),
202 Arrays.asList(stdLoadedPolicy));
204 MatchStore.storeMatch(newMatch);
206 assertFalse(MatchStore.getMatchStore().isEmpty());
208 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
210 assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
212 final Collection<LoadedPolicy> actualLoadPolicies = actualPDPNotification.getLoadedPolicies();
213 assertFalse(actualLoadPolicies.isEmpty());
215 final LoadedPolicy loadedPolicy = actualLoadPolicies.iterator().next();
216 assertEquals(stdLoadedPolicy.getPolicyName(), loadedPolicy.getPolicyName());
217 assertEquals(stdLoadedPolicy.getVersionNo(), loadedPolicy.getVersionNo());
221 public void testStoreMatch_NoticficationTypeNull_IfStdLoadPolicyExistsWithMatchesWithOutMatchingConfigAttribute()
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);
226 final StdLoadedPolicy stdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
227 final PDPNotification oldNotification = getPDPNotification(Collections.emptySet(),
228 Arrays.asList(stdLoadedPolicy));
230 MatchStore.storeMatch(newMatch);
232 assertFalse(MatchStore.getMatchStore().isEmpty());
234 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
236 assertNull(actualPDPNotification.getNotificationType());
238 final Collection<LoadedPolicy> actualLoadPolicies = actualPDPNotification.getLoadedPolicies();
239 assertTrue(actualLoadPolicies.isEmpty());
244 public void testStoreMatch_NoticficationTypeUpdate_IfMatchStoreContainMatchesWithNullConStdLoadPolicyExistsWithMatches()
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);
251 final StdLoadedPolicy stdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
252 final PDPNotification oldNotification = getPDPNotification(Collections.emptySet(),
253 Arrays.asList(stdLoadedPolicy));
255 MatchStore.storeMatch(newMatch);
257 assertFalse(MatchStore.getMatchStore().isEmpty());
259 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
261 assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
263 final Collection<LoadedPolicy> actualLoadPolicies = actualPDPNotification.getLoadedPolicies();
264 assertFalse(actualLoadPolicies.isEmpty());
269 public void testStoreMatch_NoticficationTypeUpdate_IfMatchStoreContainMatchingMatches() throws Exception {
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);
274 final StdLoadedPolicy stdLoadedPolicy = getStdLoadedPolicy(POLICY_VERSION, POLICY_NAME, matches);
275 final PDPNotification oldNotification = getPDPNotification(Collections.emptySet(),
276 Arrays.asList(stdLoadedPolicy));
278 MatchStore.storeMatch(newMatch);
280 assertFalse(MatchStore.getMatchStore().isEmpty());
282 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
284 assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
286 final Collection<LoadedPolicy> actualLoadPolicies = actualPDPNotification.getLoadedPolicies();
287 assertFalse(actualLoadPolicies.isEmpty());
289 final LoadedPolicy loadedPolicy = actualLoadPolicies.iterator().next();
290 assertEquals(stdLoadedPolicy.getPolicyName(), loadedPolicy.getPolicyName());
291 assertEquals(stdLoadedPolicy.getVersionNo(), loadedPolicy.getVersionNo());
295 public void testStoreMatch_NoticficationTypeUpdate_IfMatchStoreContainMatchingMatches2() throws Exception {
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();
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);
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));
310 MatchStore.storeMatch(firstObj);
311 MatchStore.storeMatch(secondObj);
313 assertFalse(MatchStore.getMatchStore().isEmpty());
315 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
317 assertEquals(NotificationType.UPDATE, actualPDPNotification.getNotificationType());
319 final Collection<LoadedPolicy> actualLoadPolicies = actualPDPNotification.getLoadedPolicies();
320 assertFalse(actualLoadPolicies.isEmpty());
321 assertEquals(1, actualLoadPolicies.size());
325 public void testStoreMatch_NoticficationTypeBoth_IfOldNotificationContainRemovedAndLoadedPolicies()
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);
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));
336 MatchStore.storeMatch(newMatch);
338 assertFalse(MatchStore.getMatchStore().isEmpty());
340 final PDPNotification actualPDPNotification = MatchStore.checkMatch(oldNotification);
342 assertEquals(NotificationType.BOTH, actualPDPNotification.getNotificationType());
343 assertFalse(actualPDPNotification.getRemovedPolicies().isEmpty());
344 assertFalse(actualPDPNotification.getLoadedPolicies().isEmpty());
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);
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,
361 for (int i = 0; i < configNames.length; i++) {
363 final Matches matches = getMatchesInstance(configNames[i], onapNames[i], getAttributesMap());
364 MatchStore.storeMatch(matches);
365 MatchStore.storeMatch(matches);
368 assertEquals(configNames.length - 1, MatchStore.getMatchStore().size());
373 public void testStoreMatch_MatchesObjectShouldBeAddedToMatchStore_ConfigAttrValuesAreDifferentThenExistingOne()
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());
378 MatchStore.storeMatch(firstObj);
379 MatchStore.storeMatch(secondObj);
380 MatchStore.storeMatch(firstObj);
382 assertEquals(2, MatchStore.getMatchStore().size());
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);
391 MatchStore.storeMatch(firstObj);
392 MatchStore.storeMatch(secondObj);
393 MatchStore.storeMatch(firstObj);
395 assertEquals(1, MatchStore.getMatchStore().size());
400 public void testStoreMatch_MatchesObjectShouldBeAddedToMatchStore_OnapNameIsDifferentThenExistingOne()
402 final Matches firstObj = getMatchesInstance(CONFIG_NAME_VAL, ONAP_NAME_VAL, getAttributesMap());
403 final Matches secondObj = getMatchesInstance(CONFIG_NAME_VAL, "ONAPName1", getAttributesMap());
405 MatchStore.storeMatch(firstObj);
406 MatchStore.storeMatch(secondObj);
407 MatchStore.storeMatch(firstObj);
409 assertEquals(2, MatchStore.getMatchStore().size());
413 private Map<String, String> getAttributesMap() {
414 final Map<String, String> attribute = new HashMap<>();
415 attribute.put(ATTRIBUTE_DUMMY_KEY, ATTRIBUTE_DUMMY_VALUE);
419 private Matches getMatchesInstance(final String configName, final String onapName) {
420 return getMatchesInstance(configName, onapName, null);
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);
432 private StdRemovedPolicy getRemovedPolicy(final String version, final String policyName) {
433 return new StdRemovedPolicy() {
436 public String getVersionNo() {
441 public String getPolicyName() {
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;
455 private StdLoadedPolicy getStdLoadedPolicy(final String version, final String policyName,
456 final Map<String, String> matches) {
457 return new StdLoadedPolicy() {
460 public String getVersionNo() {
465 public String getPolicyName() {
470 public Map<String, String> getMatches() {
477 * Perform post-test clean-up.
480 * if the clean-up fails for some reason
483 public void tearDown() throws Exception {
484 // Add additional tear down code here
491 * the command line arguments
493 public static void main(final String[] args) {
494 new org.junit.runner.JUnitCore().run(MatchStoreTest.class);