From 53fe02c107eae2f45abfee02e5b56a8ab3c09523 Mon Sep 17 00:00:00 2001 From: Jim Hahn Date: Mon, 23 Aug 2021 17:10:00 -0400 Subject: More lombok for apex-pdp Added lombok to auth thru context-management, excluding basic-model and context-model. Issue-ID: POLICY-3391 Change-Id: I1c3a69d52d3bc65a99126ad44126e5a97424c66f Signed-off-by: Jim Hahn --- .../policy/apex/model/utilities/TreeMapUtils.java | 48 +++++++++++ .../apex/model/utilities/TreeMapUtilsTest.java | 99 +++++++++++++++++++--- 2 files changed, 133 insertions(+), 14 deletions(-) (limited to 'model/utilities/src') diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/TreeMapUtils.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/TreeMapUtils.java index 6daa6864a..3f369c0e0 100644 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/TreeMapUtils.java +++ b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/TreeMapUtils.java @@ -23,7 +23,9 @@ package org.onap.policy.apex.model.utilities; import java.util.AbstractMap.SimpleEntry; import java.util.ArrayList; +import java.util.Iterator; import java.util.List; +import java.util.Map; import java.util.Map.Entry; import java.util.NavigableMap; import lombok.AccessLevel; @@ -75,4 +77,50 @@ public final class TreeMapUtils { } return foundNodes; } + + /** + * Compares two maps. + * @param Key type + * @param Value type + * @param leftMap left map + * @param rightMap right map + * @return an integer indicating how different the maps are + */ + @SuppressWarnings("unchecked") + public static int compareMaps(Map, ? extends Comparable> leftMap, + Map, ? extends Comparable> rightMap) { + if (leftMap == rightMap) { + return 0; + } + + Iterator leftIt = leftMap.entrySet().iterator(); + Iterator rightIt = rightMap.entrySet().iterator(); + + while (leftIt.hasNext() && rightIt.hasNext()) { + Map.Entry leftEntry = (Entry) leftIt.next(); + Map.Entry rightEntry = (Entry) rightIt.next(); + + K leftKey = (K) leftEntry.getKey(); + K rightKey = (K) rightEntry.getKey(); + int result = ((Comparable) leftKey).compareTo(rightKey); + if (result != 0) { + return result; + } + + V leftValue = (V) leftEntry.getValue(); + V rightValue = (V) rightEntry.getValue(); + result = ((Comparable) leftValue).compareTo(rightValue); + if (result != 0) { + return result; + } + } + + if (leftIt.hasNext()) { + return 1; + } else if (rightIt.hasNext()) { + return -1; + } else { + return 0; + } + } } diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TreeMapUtilsTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TreeMapUtilsTest.java index 7da3f9c47..c3a36b7b9 100644 --- a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TreeMapUtilsTest.java +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TreeMapUtilsTest.java @@ -1,28 +1,31 @@ /* * ============LICENSE_START======================================================= * Copyright (C) 2016-2018 Ericsson. All rights reserved. + * Modifications Copyright (C) 2021 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. - * + * * SPDX-License-Identifier: Apache-2.0 * ============LICENSE_END========================================================= */ package org.onap.policy.apex.model.utilities; +import static org.assertj.core.api.Assertions.assertThat; import static org.junit.Assert.assertEquals; import java.util.List; +import java.util.Map; import java.util.Map.Entry; import java.util.TreeMap; import org.junit.Test; @@ -33,8 +36,16 @@ import org.junit.Test; */ public class TreeMapUtilsTest { + private static final int KEY1 = 10; + private static final int KEY2 = 20; + private static final int KEY3 = 30; + private static final String VALUE1 = "a-one"; + private static final String VALUE2 = "b-two"; + private static final String VALUE3 = "c-three"; + private static final String VALUE4 = "d-four"; + @Test - public void test() { + public void testFindMatchingEntries() { TreeMap testTreeMap = new TreeMap(); testTreeMap.put("G", "G"); testTreeMap.put("H", "H"); @@ -43,7 +54,7 @@ public class TreeMapUtilsTest { testTreeMap.put("JOE", "JOE"); testTreeMap.put("JOSH", "JOSH"); testTreeMap.put("K", "K"); - + List> foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "F"); assertEquals(0, foundKeyList.size()); @@ -52,35 +63,95 @@ public class TreeMapUtilsTest { foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "H"); assertEquals("H", foundKeyList.get(0).getKey()); - + foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "I"); assertEquals(0, foundKeyList.size()); - + foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "J"); assertEquals("JA", foundKeyList.get(0).getKey()); - + foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "JA"); assertEquals("JA", foundKeyList.get(0).getKey()); - + foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "JB"); assertEquals(0, foundKeyList.size()); foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "JO"); assertEquals("JOE", foundKeyList.get(0).getKey()); - + foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "JOE"); assertEquals("JOE", foundKeyList.get(0).getKey()); - + foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "JOS"); assertEquals("JOSH", foundKeyList.get(0).getKey()); - + foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "JOSH"); assertEquals("JOSH", foundKeyList.get(0).getKey()); - + foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "K"); assertEquals("K", foundKeyList.get(0).getKey()); - + foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "L"); assertEquals(0, foundKeyList.size()); } + + @Test + public void testCompareMaps() { + Map map1 = Map.of(); + Map map2 = Map.of(); + + // note: using TreeMap so we can control the ordering of the entries + + // compare with self + assertThat(TreeMapUtils.compareMaps(map1, map1)).isZero(); + + // two empty maps + assertThat(TreeMapUtils.compareMaps(map1, map2)).isZero(); + + // same content + map1 = new TreeMap<>(Map.of(KEY1, VALUE1, KEY2, VALUE2, KEY3, VALUE3)); + map2 = new TreeMap<>(Map.of(KEY1, VALUE1, KEY2, VALUE2, KEY3, VALUE3)); + assertThat(TreeMapUtils.compareMaps(map1, map1)).isZero(); + assertThat(TreeMapUtils.compareMaps(map1, map2)).isZero(); + + // one is shorter than the other + map1 = new TreeMap<>(Map.of(KEY1, VALUE1, KEY2, VALUE2, KEY3, VALUE3)); + map2 = new TreeMap<>(Map.of(KEY1, VALUE1)); + assertThat(TreeMapUtils.compareMaps(map1, map2)).isPositive(); + assertThat(TreeMapUtils.compareMaps(map2, map1)).isNegative(); + + map2 = new TreeMap<>(Map.of(KEY1, VALUE1, KEY2, VALUE2)); + assertThat(TreeMapUtils.compareMaps(map1, map2)).isPositive(); + assertThat(TreeMapUtils.compareMaps(map2, map1)).isNegative(); + + // first key is different + map1 = new TreeMap<>(Map.of(KEY3, VALUE1, KEY2, VALUE2)); + map2 = new TreeMap<>(Map.of(KEY1, VALUE1, KEY2, VALUE2)); + assertThat(TreeMapUtils.compareMaps(map1, map2)).isPositive(); + assertThat(TreeMapUtils.compareMaps(map2, map1)).isNegative(); + + // second key is different + map1 = new TreeMap<>(Map.of(KEY1, VALUE1, KEY3, VALUE2)); + map2 = new TreeMap<>(Map.of(KEY1, VALUE1, KEY2, VALUE2)); + assertThat(TreeMapUtils.compareMaps(map1, map2)).isPositive(); + assertThat(TreeMapUtils.compareMaps(map2, map1)).isNegative(); + + // first value is different + map1 = new TreeMap<>(Map.of(KEY1, VALUE4, KEY2, VALUE2, KEY3, VALUE3)); + map2 = new TreeMap<>(Map.of(KEY1, VALUE1, KEY2, VALUE2, KEY3, VALUE3)); + assertThat(TreeMapUtils.compareMaps(map1, map2)).isPositive(); + assertThat(TreeMapUtils.compareMaps(map2, map1)).isNegative(); + + // second value is different + map1 = new TreeMap<>(Map.of(KEY1, VALUE1, KEY2, VALUE4, KEY3, VALUE3)); + map2 = new TreeMap<>(Map.of(KEY1, VALUE1, KEY2, VALUE2, KEY3, VALUE3)); + assertThat(TreeMapUtils.compareMaps(map1, map2)).isPositive(); + assertThat(TreeMapUtils.compareMaps(map2, map1)).isNegative(); + + // third value is different + map1 = new TreeMap<>(Map.of(KEY1, VALUE1, KEY2, VALUE2, KEY3, VALUE4)); + map2 = new TreeMap<>(Map.of(KEY1, VALUE1, KEY2, VALUE2, KEY3, VALUE3)); + assertThat(TreeMapUtils.compareMaps(map1, map2)).isPositive(); + assertThat(TreeMapUtils.compareMaps(map2, map1)).isNegative(); + } } -- cgit 1.2.3-korg