diff options
author | liamfallon <liam.fallon@est.tech> | 2022-02-10 12:06:25 +0000 |
---|---|---|
committer | liamfallon <liam.fallon@est.tech> | 2022-02-10 13:48:15 +0000 |
commit | 8534756d13531ffec9c2d7b2ffe0a53ee1d3aaef (patch) | |
tree | a35f6b3f7766d47900ee6691111acff1418bb747 /model/utilities/src | |
parent | 2f2c5465cd23c8c3300a5c3d185806bb3e7d73c1 (diff) |
Collapse apex-pdp maven model submodules
This review collapses all the code in six podel submodules into a single
model module. There are no code changes, just files moved around.
This change reduces the complexity of the code structure and speeds up
the build.
Issue-ID: POLICY-1820
Change-Id: Ifb644e8ec85ae6d0987378f4616fbc8a8858a9a8
Signed-off-by: liamfallon <liam.fallon@est.tech>
Diffstat (limited to 'model/utilities/src')
20 files changed, 0 insertions, 1501 deletions
diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/CollectionUtils.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/CollectionUtils.java deleted file mode 100644 index 9636ea7ce..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/CollectionUtils.java +++ /dev/null @@ -1,109 +0,0 @@ -/*- - * ============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 java.util.List; -import java.util.ListIterator; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; - -/** - * This is common utility class with static methods for handling collections. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class CollectionUtils { - - /** - * Compare two lists, checks for equality, then for equality on members. - * - * @param <T> The type of the lists being compared - * @param leftList The leftmost List - * @param rightList The rightmost list - * @return an integer indicating how different the lists are - */ - public static <T> int compareLists(final List<? extends Comparable<T>> leftList, - final List<? extends Comparable<T>> rightList) { - // Check for nulls - if (leftList == null && rightList == null) { - return 0; - } - if (leftList != null && rightList == null) { - return -1; - } - if (leftList == null) { - return 1; - } - - // Check for equality - if (leftList.equals(rightList)) { - return 0; - } - - return compareListEntries(leftList, rightList); - } - - /** - * Compare two lists for equality on members. - * - * @param <T> The type of the lists being compared - * @param leftList The leftmost List - * @param rightList The rightmost list - * @return an integer indicating how different the lists are - */ - private static <T> int compareListEntries(final List<? extends Comparable<T>> leftList, - final List<? extends Comparable<T>> rightList) { - - // Iterate down the lists till we find a difference - final ListIterator<?> leftIterator = leftList.listIterator(); - final ListIterator<?> rightIterator = rightList.listIterator(); - - while (true) { - // Check the iterators - if (!leftIterator.hasNext() && !rightIterator.hasNext()) { - return 0; - } - if (leftIterator.hasNext() && !rightIterator.hasNext()) { - return -1; - } - if (!leftIterator.hasNext() && rightIterator.hasNext()) { - return 1; - } - - // Get the next objects - @SuppressWarnings("unchecked") - final var leftObject = (T) leftIterator.next(); - @SuppressWarnings("unchecked") - final var rightObject = (T) rightIterator.next(); - - // Compare the objects - @SuppressWarnings("unchecked") - final int comparisonResult = ((Comparable<T>) leftObject).compareTo(rightObject); - - // Check the comparison result - if (comparisonResult != 0) { - return comparisonResult; - } - } - } -} diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/DirectoryDeleteShutdownHook.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/DirectoryDeleteShutdownHook.java deleted file mode 100644 index 21c417c4d..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/DirectoryDeleteShutdownHook.java +++ /dev/null @@ -1,67 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. - * 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 java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * The Class DirectoryShutdownHook removes the contents of a directory and the directory itself at shutdown. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -final class DirectoryDeleteShutdownHook extends Thread { - - private static final XLogger LOGGER = XLoggerFactory.getXLogger(DirectoryUtils.class); - - // The directory we are acting on - private final File tempDir; - - /** - * Constructor that defines the directory to act on at shutdown. - * - * @param tempDir The temporary directory to delete - */ - DirectoryDeleteShutdownHook(final File tempDir) { - this.tempDir = tempDir; - } - - /** - * {@inheritDoc}. - */ - @Override - public void run() { - if (tempDir.exists()) { - // Empty and delete the directory - DirectoryUtils.emptyDirectory(tempDir); - try { - Files.delete(tempDir.toPath()); - } catch (IOException e) { - LOGGER.warn("Failed to delete directory {}", tempDir, e); - } - } - } -} diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/DirectoryUtils.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/DirectoryUtils.java deleted file mode 100644 index b0e8332b1..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/DirectoryUtils.java +++ /dev/null @@ -1,110 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. - * 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 java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import lombok.AccessLevel; -import lombok.NoArgsConstructor; -import org.slf4j.ext.XLogger; -import org.slf4j.ext.XLoggerFactory; - -/** - * This is common utility class with static methods for handling directories. It is an abstract class to prevent any - * direct instantiation and private constructor to prevent extending this class. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class DirectoryUtils { - // Get a reference to the logger - private static final XLogger LOGGER = XLoggerFactory.getXLogger(DirectoryUtils.class); - - /** - * Method to get an empty temporary directory in the system temporary directory on the local machine that will be - * deleted on (normal) shutdown. - * - * @param nameprefix The prefix of the filename. System.nanoTime() will be appended to the pattern to create a - * unique file pattern - * @return The temporary directory - */ - public static File getLocalTempDirectory(final String nameprefix) { - try { - // Get the name of the temporary directory - final String tempDirName = System.getProperty("java.io.tmpdir") + "/" + nameprefix + System.nanoTime(); - final var tempDir = new File(tempDirName); - - // Delete the directory if it already exists - if (tempDir.exists()) { - return null; - } - - // Make the directory - tempDir.mkdirs(); - - // Add a shutdown hook that deletes the directory contents when the JVM closes - Runtime.getRuntime().addShutdownHook(new DirectoryDeleteShutdownHook(tempDir)); - - LOGGER.trace("creating temp directory\"{}\" : ", tempDir.getAbsolutePath()); - return tempDir; - } catch (final Exception e) { - LOGGER.debug("error creating temp directory\"{}\" : " + e.getMessage(), e); - return null; - } - } - - /** - * Method to recursively delete all the files in a directory. - * - * @param tempDir the directory to empty - * @return true if the operation succeeds, false otherwise - */ - public static boolean emptyDirectory(final File tempDir) { - // Sanity check - if (!tempDir.exists() || !tempDir.isDirectory()) { - return false; - } - - // Walk the directory structure deleting files as we go - final File[] files = tempDir.listFiles(); - if (files != null) { - for (final File directoryFile : files) { - // Check if this is a directory itself - if (directoryFile.isDirectory()) { - // Recurse into the sub directory and empty it - emptyDirectory(directoryFile); - } - - // Delete the directory entry - try { - Files.delete(directoryFile.toPath()); - } catch (IOException e) { - LOGGER.warn("Failed to delete directory file {}", directoryFile, e); - } - } - } - - return true; - } -} 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 deleted file mode 100644 index d8bb469cf..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/TreeMapUtils.java +++ /dev/null @@ -1,126 +0,0 @@ -/*- - * ============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 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; -import lombok.NoArgsConstructor; - -/** - * This class provides utility functions for tree maps. A function to find the nearest match in the tree map to an input - * string is provided. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -@NoArgsConstructor(access = AccessLevel.PRIVATE) -public final class TreeMapUtils { - - /** - * Find the list of entries that matches a given word, for example "p" will match "put", "policy", and "push". - * - * @param <T> the generic type for the value of the tree map - * @param searchMap the map that the method operates on - * @param word the word to search for - * @return the list of entries in the {@code searchMap} that match the {@code word} - */ - public static <T> List<Entry<String, T>> findMatchingEntries(final NavigableMap<String, T> searchMap, - final String word) { - final List<Entry<String, T>> foundNodes = new ArrayList<>(); - - // A straight match check - if (searchMap.containsKey(word)) { - foundNodes.add(new SimpleEntry<>(word, searchMap.get(word))); - return foundNodes; - } - - // Set up the beginning point for our search for a list of near matches - String foundKeyword = searchMap.floorKey(word); - if (foundKeyword == null) { - foundKeyword = searchMap.firstKey(); - } else { - foundKeyword = searchMap.higherKey(foundKeyword); - } - - // Find all the nodes that start with the word we are searching for - while (foundKeyword != null) { - if (foundKeyword.startsWith(word)) { - foundNodes.add(new SimpleEntry<>(foundKeyword, searchMap.get(foundKeyword))); - foundKeyword = searchMap.higherKey(foundKeyword); - } else { - break; - } - } - return foundNodes; - } - - /** - * Compares two maps. - * @param <K> Key type - * @param <V> Value type - * @param leftMap left map - * @param rightMap right map - * @return an integer indicating how different the maps are - */ - @SuppressWarnings("unchecked") - public static <K, V> int compareMaps(Map<? extends Comparable<K>, ? extends Comparable<V>> leftMap, - Map<? extends Comparable<K>, ? extends Comparable<V>> 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(); - - var leftKey = (K) leftEntry.getKey(); - var rightKey = (K) rightEntry.getKey(); - int result = ((Comparable<K>) leftKey).compareTo(rightKey); - if (result != 0) { - return result; - } - - var leftValue = (V) leftEntry.getValue(); - var rightValue = (V) rightEntry.getValue(); - result = ((Comparable<V>) 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/main/java/org/onap/policy/apex/model/utilities/comparison/KeyComparer.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/KeyComparer.java deleted file mode 100644 index f4d628405..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/KeyComparer.java +++ /dev/null @@ -1,42 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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.comparison; - -/** - * This class compares two keys and returns their differences. It is used in bulk comparisons in models where maps of - * keys are being compared. The {@link KeyComparer} that is returned does the actual comparison - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @param <K> the type of key being compared - */ -public class KeyComparer<K> { - - /** - * Compare two keys and return their differences. - * - * @param leftKey The left key of the comparison - * @param rightKey The right key of the comparison - * @return The difference between the keys - */ - public KeyDifference<K> compareKeys(final K leftKey, final K rightKey) { - return new KeyDifference<>(leftKey, rightKey); - } -} diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/KeyDifference.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/KeyDifference.java deleted file mode 100644 index a0395cf16..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/KeyDifference.java +++ /dev/null @@ -1,86 +0,0 @@ -/* - * ============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.comparison; - -import lombok.Getter; - -/** - * This class is used to template key differences for bulk key comparisons in models. It performs a difference check - * between two keys. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @param <K> the generic type - */ -@Getter -public class KeyDifference<K> { - // The keys being compared - private K leftKey; - private K rightKey; - - /** - * Constructor used to set the keys being compared. - * - * @param leftKey the left key that is being compared - * @param rightKey the right key that is being compared - */ - public KeyDifference(final K leftKey, final K rightKey) { - this.leftKey = leftKey; - this.rightKey = rightKey; - } - - /** - * Checks if the left and right keys are equal. - * - * @return true, if checks if is equal - */ - public boolean isEqual() { - return leftKey.equals(rightKey); - } - - /** - * Gets a string representation of the difference between the keys. - * - * @param diffsOnly if set, then a blank string is returned if the keys are equal - * @return the difference between the keys as a string - */ - public String asString(final boolean diffsOnly) { - var builder = new StringBuilder(); - - if (leftKey.equals(rightKey)) { - if (!diffsOnly) { - builder.append("left key "); - builder.append(leftKey); - builder.append(" equals right key "); - builder.append(rightKey); - builder.append('\n'); - } - } else { - builder.append("left key "); - builder.append(leftKey); - builder.append(" and right key "); - builder.append(rightKey); - builder.append(" differ\n"); - } - - return builder.toString(); - } -} diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/KeyedMapComparer.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/KeyedMapComparer.java deleted file mode 100644 index 9943690fa..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/KeyedMapComparer.java +++ /dev/null @@ -1,96 +0,0 @@ -/* - * ============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.comparison; - -import java.util.ArrayList; -import java.util.List; -import java.util.Map; -import java.util.Set; -import java.util.TreeSet; - -/** - * Compare two maps and returns their differences. The types of the keys and the values in the two maps being comapred - * must be the same. The class returns entries that are only in the left map, only in the right map, entries that have - * identical keys and different values and entries that have different keys and different values in a - * {@link KeyedMapDifference} instance. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @param <K> the type of the keys in the maps being compared - * @param <V> the type of the values in the maps being compared - */ -public class KeyedMapComparer<K, V> { - /** - * Compare two maps and return their differences in a {@link KeyedMapDifference} instance. - * - * @param leftMap The left map to be compared - * @param rightMap The right map to be compared - * @return The common, left only, and right only maps in a {@link KeyedMapDifference} instance - */ - public KeyedMapDifference<K, V> compareMaps(final Map<K, V> leftMap, final Map<K, V> rightMap) { - KeyedMapDifference<K, V> result = new KeyedMapDifference<>(); - - // Get the keys that are only in the left map - Set<K> leftOnlyKeys = new TreeSet<>(leftMap.keySet()); - leftOnlyKeys.removeAll(rightMap.keySet()); - - // Get the keys that are only in the right map - Set<K> rightOnlyKeys = new TreeSet<>(rightMap.keySet()); - rightOnlyKeys.removeAll(leftMap.keySet()); - - // Find the keys common across both maps - Set<K> commonKeys = new TreeSet<>(rightMap.keySet()); - commonKeys.addAll(leftMap.keySet()); - commonKeys.removeAll(leftOnlyKeys); - commonKeys.removeAll(rightOnlyKeys); - - // Now save the left values - for (K key : leftOnlyKeys) { - result.getLeftOnly().put(key, leftMap.get(key)); - } - - // Now save the right values - for (K key : rightOnlyKeys) { - result.getRightOnly().put(key, rightMap.get(key)); - } - - // Save the common values to two maps, an identical and different map - for (K key : commonKeys) { - // Check if the values are identical in each map - var leftValue = leftMap.get(key); - var rightValue = rightMap.get(key); - - // Store as appropriate - if (leftValue.equals(rightValue)) { - result.getIdenticalValues().put(key, leftValue); - } else { - // Store the two values - List<V> valueList = new ArrayList<>(); - valueList.add(leftValue); - valueList.add(rightValue); - - result.getDifferentValues().put(key, valueList); - } - } - - return result; - } -} diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/KeyedMapDifference.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/KeyedMapDifference.java deleted file mode 100644 index e10854926..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/KeyedMapDifference.java +++ /dev/null @@ -1,174 +0,0 @@ -/* - * ============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.comparison; - -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; -import java.util.TreeMap; -import lombok.Getter; - -/** - * This class holds the result of a difference check between two keyed maps. Four results are returned in the class. The - * {@code leftOnly} result is the entries that appear only in the left map. the {@code rightOnly} result is the entries - * that appear only in the right map. The {@code differentValues} result are the entries that have the same key but - * different values in the maps being compared. The {@code identicalValues} result are the entries with identical keys - * and values in both maps being compared. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @param <K> the generic type - * @param <V> the generic type - */ -@Getter -public class KeyedMapDifference<K, V> { - private static final String KEY = "key="; - private static final String VALUE = ",value="; - - // Three maps to hold the comparison result - private Map<K, V> leftOnly = new TreeMap<>(); - private Map<K, V> rightOnly = new TreeMap<>(); - private Map<K, V> identicalValues = new TreeMap<>(); - private Map<K, List<V>> differentValues = new TreeMap<>(); - - /** - * Return a string representation of the differences. - * - * @param diffsOnly if set, then a blank string is returned if the maps are equal - * @param keysOnly if set, then a terse string that prints only the keys is returned, otherwise both keys and values - * are printed - * @return the string - */ - public String asString(final boolean diffsOnly, final boolean keysOnly) { - var builder = new StringBuilder(); - - if (leftOnly.isEmpty()) { - if (!diffsOnly) { - builder.append("*** all left keys in right\n"); - } - } else { - builder.append(getInOneSideOnlyAsString(leftOnly, "left", keysOnly)); - } - - if (rightOnly.isEmpty()) { - if (!diffsOnly) { - builder.append("*** all right keys in left\n"); - } - } else { - builder.append(getInOneSideOnlyAsString(rightOnly, "right", keysOnly)); - } - - if (differentValues.isEmpty()) { - if (!diffsOnly) { - builder.append("*** all values in left and right are identical\n"); - } - } else { - builder.append(getDifferencesAsString(keysOnly)); - } - - if (!diffsOnly) { - builder.append(getIdenticalsAsString(keysOnly)); - } - - return builder.toString(); - } - - /** - * Output the entries in a map with entries that are in one side only as a string. - * - * @param sideMap the map for the side being checked - * @param sideMapString the string that represents the map in output strings - * @param keysOnly if true, just add key information and not entries - * @return the entries as a string - */ - private Object getInOneSideOnlyAsString(final Map<K, V> sideMap, final String sideMapString, - final boolean keysOnly) { - var builder = new StringBuilder(); - - builder.append("*** list of keys on " + sideMapString + " only\n"); - for (Entry<K, V> leftEntry : sideMap.entrySet()) { - builder.append(KEY); - builder.append(leftEntry.getKey()); - if (!keysOnly) { - builder.append(VALUE); - builder.append(leftEntry.getValue()); - } - builder.append('\n'); - } - - return builder.toString(); - } - - /** - * Output the differences between two the maps as a string. - * - * @param keysOnly if true, just add key information and not entries - * @return the differences as a string - */ - private String getDifferencesAsString(final boolean keysOnly) { - var builder = new StringBuilder(); - - builder.append("*** list of differing entries between left and right\n"); - for (Entry<K, List<V>> differentEntry : differentValues.entrySet()) { - builder.append(KEY); - builder.append(differentEntry.getKey()); - if (!keysOnly) { - builder.append(",values={"); - var first = true; - for (V differentEntryValue : differentEntry.getValue()) { - builder.append(differentEntryValue); - if (first) { - first = false; - } else { - builder.append(','); - } - } - builder.append("}"); - } - builder.append('\n'); - } - - return builder.toString(); - } - - /** - * Output the identical entries in the maps as a string. - * - * @param keysOnly if true, just add key information and not entries - * @return the identical entries as a string - */ - private String getIdenticalsAsString(final boolean keysOnly) { - var builder = new StringBuilder(); - - builder.append("*** list of identical entries in left and right\n"); - for (Entry<K, V> identicalEntry : identicalValues.entrySet()) { - builder.append(KEY); - builder.append(identicalEntry.getKey()); - if (!keysOnly) { - builder.append(VALUE); - builder.append(identicalEntry.getValue()); - } - builder.append('\n'); - } - - return builder.toString(); - } -} diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/package-info.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/package-info.java deleted file mode 100644 index f0488eacd..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/comparison/package-info.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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========================================================= - */ - -/** - * Provides utility template classes that compare keys and maps of any type. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -package org.onap.policy.apex.model.utilities.comparison; diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/json/JsonHandler.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/json/JsonHandler.java deleted file mode 100644 index 66e389fa2..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/json/JsonHandler.java +++ /dev/null @@ -1,50 +0,0 @@ -/*- - * ============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.json; - -import com.google.gson.Gson; -import com.google.gson.GsonBuilder; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.io.Reader; - -/** - * This class reads objects of the given class from an input stream. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - * @param <P> the generic type - */ -public class JsonHandler<P> { - private static final Gson GSON = new GsonBuilder().serializeNulls().create(); - - /** - * This method reads objects of a given class from an input stream. - * - * @param inputClass The class to read - * @param inputStream the input stream to read from - * @return the object read - */ - public P read(final Class<P> inputClass, final InputStream inputStream) { - final Reader jsonResourceReader = new InputStreamReader(inputStream); - return GSON.fromJson(jsonResourceReader, inputClass); - } -} diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/json/package-info.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/json/package-info.java deleted file mode 100755 index f9d1304d7..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/json/package-info.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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========================================================= - */ - -/** - * Provides a utility class for reading JSON streams. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -package org.onap.policy.apex.model.utilities.json; diff --git a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/package-info.java b/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/package-info.java deleted file mode 100644 index 446d009ba..000000000 --- a/model/utilities/src/main/java/org/onap/policy/apex/model/utilities/package-info.java +++ /dev/null @@ -1,26 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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========================================================= - */ - -/** - * Provides utility classes that are used in APEX models and indeed in other packages that use APEX models. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -package org.onap.policy.apex.model.utilities; diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/CollectionUtilitiesTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/CollectionUtilitiesTest.java deleted file mode 100644 index 2512133e7..000000000 --- a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/CollectionUtilitiesTest.java +++ /dev/null @@ -1,87 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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.junit.Assert.assertEquals; - -import java.util.ArrayList; -import java.util.List; -import org.junit.Test; - -public class CollectionUtilitiesTest { - - @Test - public void testNullLists() { - int result = 0; - - result = CollectionUtils.compareLists(null, null); - assertEquals(0, result); - - List<String> leftList = new ArrayList<String>(); - - result = CollectionUtils.compareLists(leftList, null); - assertEquals(-1, result); - - List<String> rightList = new ArrayList<String>(); - - result = CollectionUtils.compareLists(null, rightList); - assertEquals(1, result); - - result = CollectionUtils.compareLists(leftList, rightList); - assertEquals(0, result); - - leftList.add("AAA"); - result = CollectionUtils.compareLists(leftList, rightList); - assertEquals(-1, result); - - rightList.add("AAA"); - result = CollectionUtils.compareLists(leftList, rightList); - assertEquals(0, result); - - rightList.add("BBB"); - result = CollectionUtils.compareLists(leftList, rightList); - assertEquals(1, result); - - leftList.add("BBB"); - result = CollectionUtils.compareLists(leftList, rightList); - assertEquals(0, result); - - leftList.add("CCA"); - rightList.add("CCB"); - result = CollectionUtils.compareLists(leftList, rightList); - assertEquals(-1, result); - - leftList.remove(leftList.size() - 1); - rightList.remove(rightList.size() - 1); - result = CollectionUtils.compareLists(leftList, rightList); - assertEquals(0, result); - - leftList.add("CCB"); - rightList.add("CCA"); - result = CollectionUtils.compareLists(leftList, rightList); - assertEquals(1, result); - - leftList.remove(leftList.size() - 1); - rightList.remove(rightList.size() - 1); - result = CollectionUtils.compareLists(leftList, rightList); - assertEquals(0, result); - } -} diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/DirectoryUtilsTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/DirectoryUtilsTest.java deleted file mode 100644 index 69c2d4763..000000000 --- a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/DirectoryUtilsTest.java +++ /dev/null @@ -1,55 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. - * ================================================================================ - * 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.junit.Assert.assertNotNull; - -import java.io.File; -import java.io.IOException; -import java.nio.file.Files; -import java.util.Arrays; -import org.junit.Test; -import org.onap.policy.common.utils.resources.TextFileUtils; - -public class DirectoryUtilsTest { - - @Test - public void test() throws IOException { - DirectoryUtils.emptyDirectory(new File("/i/dont/exist")); - - File tempDir = Files.createTempDirectory("test").toFile(); - assertNotNull(tempDir); - - Files.createTempDirectory(tempDir.toPath(), "testsubprefix"); - - TextFileUtils.putStringAsTextFile("Temp File 0 contents", tempDir.getAbsolutePath() + "/tempFile0.tmp"); - TextFileUtils.putStringAsTextFile("Temp File 1 contents", tempDir.getAbsolutePath() + "/tempFile1.tmp"); - - DirectoryUtils.emptyDirectory(tempDir); - - DirectoryUtils.getLocalTempDirectory(null); - - byte[] byteArray = new byte[] {0, 0, 0}; - DirectoryUtils.getLocalTempDirectory(Arrays.toString(byteArray)); - } - -} diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/KeyComparerTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/KeyComparerTest.java deleted file mode 100644 index 20780b271..000000000 --- a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/KeyComparerTest.java +++ /dev/null @@ -1,53 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. - * ================================================================================ - * 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.junit.Assert.assertEquals; -import static org.junit.Assert.assertFalse; - -import org.junit.Test; -import org.onap.policy.apex.model.utilities.comparison.KeyComparer; -import org.onap.policy.apex.model.utilities.comparison.KeyDifference; - -/** - * Test key comparisons. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class KeyComparerTest { - - @Test - public void test() { - KeyDifference<String> keyDifference = new KeyComparer<String>().compareKeys("Hello", "Goodbye"); - - assertFalse(keyDifference.isEqual()); - assertEquals("Hello", keyDifference.getLeftKey().toString()); - assertEquals("Goodbye", keyDifference.getRightKey().toString()); - - assertEquals("left key Hello and right key Goodbye differ\n", keyDifference.asString(true)); - assertEquals("left key Hello and right key Goodbye differ\n", keyDifference.asString(false)); - - KeyDifference<String> keyDifference2 = new KeyComparer<String>().compareKeys("Here", "Here"); - assertEquals("", keyDifference2.asString(true)); - assertEquals("left key Here equals right key Here\n", keyDifference2.asString(false)); - } -} diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/KeyedMapComparerTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/KeyedMapComparerTest.java deleted file mode 100644 index b4e88e20d..000000000 --- a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/KeyedMapComparerTest.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. - * ================================================================================ - * 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.junit.Assert.assertEquals; -import static org.junit.Assert.assertNotNull; - -import java.util.TreeMap; -import org.junit.Test; -import org.onap.policy.apex.model.utilities.comparison.KeyedMapComparer; -import org.onap.policy.apex.model.utilities.comparison.KeyedMapDifference; - -/** - * Test key map comparisons. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class KeyedMapComparerTest { - - @Test - public void test() { - TreeMap<String, String> leftMap = new TreeMap<String, String>(); - leftMap.put("B", "BBBBB"); - leftMap.put("C", "CCCCC"); - leftMap.put("E", "EEEEE"); - leftMap.put("G", "GGGGG"); - - TreeMap<String, String> rightMap = new TreeMap<String, String>(); - rightMap.put("A", "AAAAA"); - rightMap.put("B", "B"); - rightMap.put("D", "DDDDD"); - rightMap.put("E", "EEEEE"); - rightMap.put("F", "FFFFF"); - rightMap.put("G", "G"); - - KeyedMapDifference<String, String> kmComparedSame = - new KeyedMapComparer<String, String>().compareMaps(leftMap, leftMap); - KeyedMapDifference<String, String> kmComparedDiff = - new KeyedMapComparer<String, String>().compareMaps(leftMap, rightMap); - - assertEquals(leftMap, kmComparedSame.getIdenticalValues()); - assertEquals(1, kmComparedDiff.getLeftOnly().size()); - assertEquals(3, kmComparedDiff.getRightOnly().size()); - assertEquals(2, kmComparedDiff.getDifferentValues().size()); - assertEquals(1, kmComparedDiff.getIdenticalValues().size()); - - assertNotNull(kmComparedSame.asString(true, true)); - assertNotNull(kmComparedSame.asString(true, false)); - assertNotNull(kmComparedSame.asString(false, false)); - assertNotNull(kmComparedSame.asString(false, true)); - - assertNotNull(kmComparedDiff.asString(true, true)); - assertNotNull(kmComparedDiff.asString(true, false)); - assertNotNull(kmComparedDiff.asString(false, false)); - assertNotNull(kmComparedDiff.asString(false, true)); - } -} diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TextFileUtilsTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TextFileUtilsTest.java deleted file mode 100644 index 1d6f29fe5..000000000 --- a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TextFileUtilsTest.java +++ /dev/null @@ -1,56 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. All rights reserved. - * Modifications Copyright (C) 2020 Nordix Foundation. - * ================================================================================ - * 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.junit.Assert.assertEquals; - -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import org.junit.Test; -import org.onap.policy.common.utils.resources.TextFileUtils; - -/** - * Test text file utilities. - * - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -public class TextFileUtilsTest { - - private static final String FILE_CONTENT = "This is the contents of a text file"; - - @Test - public void test() throws IOException { - final File tempTextFile = File.createTempFile("Test", "txt"); - - TextFileUtils.putStringAsTextFile(FILE_CONTENT, tempTextFile.getAbsolutePath()); - - final String textFileString0 = TextFileUtils.getTextFileAsString(tempTextFile.getAbsolutePath()); - assertEquals(FILE_CONTENT, textFileString0); - - final FileInputStream fis = new FileInputStream(tempTextFile); - final String textFileString1 = TextFileUtils.getStreamAsString(fis); - assertEquals(textFileString0, textFileString1); - - } - -} 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 deleted file mode 100644 index c3a36b7b9..000000000 --- a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TreeMapUtilsTest.java +++ /dev/null @@ -1,157 +0,0 @@ -/* - * ============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; - -/** - * Test the tree map utilities. - * @author Liam Fallon (liam.fallon@ericsson.com) - */ -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 testFindMatchingEntries() { - TreeMap<String, String> testTreeMap = new TreeMap<String, String>(); - testTreeMap.put("G", "G"); - testTreeMap.put("H", "H"); - testTreeMap.put("JA", "JA"); - testTreeMap.put("JAM", "JAM"); - testTreeMap.put("JOE", "JOE"); - testTreeMap.put("JOSH", "JOSH"); - testTreeMap.put("K", "K"); - - List<Entry<String, String>> foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "F"); - assertEquals(0, foundKeyList.size()); - - foundKeyList = TreeMapUtils.findMatchingEntries(testTreeMap, "G"); - assertEquals("G", foundKeyList.get(0).getKey()); - - 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<Integer, String> map1 = Map.of(); - Map<Integer, String> 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(); - } -} diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/json/JsonHandlerTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/json/JsonHandlerTest.java deleted file mode 100644 index ae5d53ec3..000000000 --- a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/json/JsonHandlerTest.java +++ /dev/null @@ -1,59 +0,0 @@ -/*- - * ============LICENSE_START======================================================= - * Copyright (C) 2016-2018 Ericsson. 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.json; - -import static org.junit.Assert.assertEquals; - -import com.google.gson.GsonBuilder; -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.nio.charset.StandardCharsets; -import org.junit.Test; - -public class JsonHandlerTest { - - private static final String VALUE = "value"; - - @Test - public void testAssertions() throws IOException { - final OverTheMoonObject jsonObject = new OverTheMoonObject(VALUE); - final String jsonString = new GsonBuilder().create().toJson(jsonObject); - - final byte[] bytes = jsonString.getBytes(StandardCharsets.UTF_8); - try (final InputStream inputStream = new ByteArrayInputStream(bytes);) { - - final JsonHandler<OverTheMoonObject> objUnderTest = new JsonHandler<>(); - - final OverTheMoonObject actualObject = objUnderTest.read(OverTheMoonObject.class, inputStream); - assertEquals(VALUE, actualObject.name); - } - - } - - private class OverTheMoonObject { - private final String name; - - public OverTheMoonObject(final String name) { - this.name = name; - } - } -} diff --git a/model/utilities/src/test/resources/testdir/testfile.xml b/model/utilities/src/test/resources/testdir/testfile.xml deleted file mode 100644 index ddffc5822..000000000 --- a/model/utilities/src/test/resources/testdir/testfile.xml +++ /dev/null @@ -1,20 +0,0 @@ -<?xml version="1.0" encoding="UTF-8"?> -<!-- - ============LICENSE_START======================================================= - Copyright (C) 2016-2018 Ericsson. 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========================================================= ---> |