diff options
Diffstat (limited to 'model/utilities/src/test/java')
10 files changed, 956 insertions, 0 deletions
diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/AssertionsTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/AssertionsTest.java new file mode 100644 index 000000000..db25147c3 --- /dev/null +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/AssertionsTest.java @@ -0,0 +1,99 @@ +/* + * ============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 org.junit.Test; +import org.onap.policy.apex.model.utilities.Assertions; + +/** + * The Class ResourceUtilsTest. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class AssertionsTest { + @Test + public void testAssertions() { + Assertions.argumentNotFalse(true, "it is true"); + + try { + Assertions.argumentNotFalse(false, "it is false"); + } + catch (IllegalArgumentException e) { + assertEquals("it is false", e.getMessage()); + } + + Assertions.argumentNotFalse(true, ArithmeticException.class, "it is true"); + + try { + Assertions.argumentNotFalse(false, ArithmeticException.class, "it is false"); + } + catch (Exception e) { + assertEquals("it is false", e.getMessage()); + } + + Assertions.argumentNotNull("Hello", "it is OK"); + + try { + Assertions.argumentNotNull(null, "it is null"); + } + catch (IllegalArgumentException e) { + assertEquals("it is null", e.getMessage()); + } + + Assertions.argumentNotNull(true, ArithmeticException.class, "it is OK"); + + try { + Assertions.argumentNotNull(null, ArithmeticException.class, "it is null"); + } + catch (Exception e) { + assertEquals("it is null", e.getMessage()); + } + + Assertions.assignableFrom(java.util.TreeMap.class, java.util.Map.class); + + try { + Assertions.assignableFrom(java.util.Map.class, java.util.TreeMap.class); + } + catch (IllegalArgumentException e) { + assertEquals("java.util.Map is not an instance of java.util.TreeMap", e.getMessage()); + } + + Assertions.instanceOf("Hello", String.class); + + try { + Assertions.instanceOf(100, String.class); + } + catch (IllegalArgumentException e) { + assertEquals("java.lang.Integer is not an instance of java.lang.String", e.getMessage()); + } + + Assertions.validateStringParameter("name", "MyName", "^M.*e$"); + + try { + Assertions.validateStringParameter("name", "MyName", "^M.*f$"); + } + catch (IllegalArgumentException e) { + assertEquals("parameter \"name\": value \"MyName\", does not match regular expression \"^M.*f$\"", e.getMessage()); + } + } +} 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 new file mode 100644 index 000000000..4682d985d --- /dev/null +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/CollectionUtilitiesTest.java @@ -0,0 +1,91 @@ +/* + * ============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.*; + +import java.util.ArrayList; +import java.util.List; + +import org.junit.Test; +import org.onap.policy.apex.model.utilities.CollectionUtils; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class CollectionUtilitiesTest { + + @Test + public void testNullLists() { + List<String> leftList = new ArrayList<String>(); + List<String> rightList = new ArrayList<String>(); + + int result = 0; + + result = CollectionUtils.compareLists(null, null); + assertEquals(0, result); + + result = CollectionUtils.compareLists(leftList, null); + assertEquals(-1, result); + + 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 new file mode 100644 index 000000000..84e9733f3 --- /dev/null +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/DirectoryUtilsTest.java @@ -0,0 +1,56 @@ +/* + * ============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 java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.util.Arrays; + +import org.junit.Test; +import org.onap.policy.apex.model.utilities.DirectoryUtils; +import org.onap.policy.apex.model.utilities.TextFileUtils; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class DirectoryUtilsTest { + + @Test + public void test() throws IOException { + DirectoryUtils.emptyDirectory(new File("/i/dont/exist")); + + File tempDir = Files.createTempDirectory("test").toFile(); + + 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/PropertyUtilsTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/PropertyUtilsTest.java new file mode 100644 index 000000000..e25e3ffaa --- /dev/null +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/PropertyUtilsTest.java @@ -0,0 +1,48 @@ +/* + * ============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 static org.junit.Assert.assertNotNull; + +import org.junit.Test; +import org.onap.policy.apex.model.utilities.PropertyUtils; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class PropertyUtilsTest { + + @Test + public void test() { + System.setProperty("boolean.true", "true"); + System.setProperty("boolean.false", "false"); + System.setProperty("boolean.blank", " "); + + assertNotNull(PropertyUtils.getAllProperties()); + + assertEquals(false, PropertyUtils.propertySetOrTrue(null)); + assertEquals(false, PropertyUtils.propertySetOrTrue("ZOOBY")); + assertEquals(true, PropertyUtils.propertySetOrTrue("boolean.true")); + assertEquals(true, PropertyUtils.propertySetOrTrue("boolean.blank")); + assertEquals(false, PropertyUtils.propertySetOrTrue("boolean.false")); + } +} diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/ResourceUtilsTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/ResourceUtilsTest.java new file mode 100644 index 000000000..9f789065e --- /dev/null +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/ResourceUtilsTest.java @@ -0,0 +1,308 @@ +/* + * ============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 static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; + +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.io.InputStream; +import java.net.URL; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; +import org.onap.policy.apex.model.utilities.ResourceUtils; + +/** + * The Class ResourceUtilsTest. + * + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class ResourceUtilsTest { + private File tmpDir = null; + private File tmpEmptyFile = null; + private File tmpUsedFile = null; + + private String jarDirResource = null; + private String jarFileResource = null; + + private final String pathDirResource = "testdir"; + private final String pathFileResource = "testdir/testfile.xml"; + + private final String nonExistantResource = "somewhere/over/the/rainbow"; + private final String invalidResource = "@%%%\\\\_:::DESD"; + + /** + * Setup resource utils test. + * + * @throws IOException Signals that an I/O exception has occurred. + */ + @Before + public void setupResourceUtilsTest() throws IOException { + tmpDir = new File(System.getProperty("java.io.tmpdir")); + tmpEmptyFile = File.createTempFile(this.getClass().getName(), ".tmp"); + tmpUsedFile = File.createTempFile(this.getClass().getName(), ".tmp"); + + jarDirResource = "META-INF"; + jarFileResource = "META-INF/MANIFEST.MF"; + + final FileWriter fileWriter = new FileWriter(tmpUsedFile); + fileWriter.write("Bluebirds fly over the rainbow"); + fileWriter.close(); + } + + /** + * Test get url resource. + */ + @Test + public void testGetURLResource() { + URL theURL = ResourceUtils.getURLResource(tmpDir.getAbsolutePath()); + assertNull(theURL); + + theURL = ResourceUtils.getURLResource(tmpEmptyFile.getAbsolutePath()); + assertNull(theURL); + + theURL = ResourceUtils.getURLResource(tmpUsedFile.getAbsolutePath()); + assertNull(theURL); + + theURL = ResourceUtils.getURLResource(jarDirResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getURLResource(jarFileResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getURLResource(pathDirResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getURLResource(pathFileResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getURLResource("file:///" + pathDirResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getLocalFile("src/test/resources/" + pathDirResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getLocalFile("src/test/resources/" + pathFileResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getURLResource(nonExistantResource); + assertNull(theURL); + + theURL = ResourceUtils.getURLResource(invalidResource); + assertNull(theURL); + + theURL = ResourceUtils.getURLResource(null); + assertNull(theURL); + } + + /** + * Test get local file. + */ + @Test + public void testGetLocalFile() { + URL theURL = ResourceUtils.getLocalFile(tmpDir.getAbsolutePath()); + assertNotNull(theURL); + + theURL = ResourceUtils.getLocalFile(tmpEmptyFile.getAbsolutePath()); + assertNotNull(theURL); + + theURL = ResourceUtils.getLocalFile(tmpUsedFile.getAbsolutePath()); + assertNotNull(theURL); + + theURL = ResourceUtils.getLocalFile(jarDirResource); + assertNull(theURL); + + theURL = ResourceUtils.getLocalFile(jarFileResource); + assertNull(theURL); + + theURL = ResourceUtils.getLocalFile(pathDirResource); + assertNull(theURL); + + theURL = ResourceUtils.getLocalFile(pathFileResource); + assertNull(theURL); + + theURL = ResourceUtils.getLocalFile("src/test/resources/" + pathDirResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getLocalFile("src/test/resources/" + pathFileResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getLocalFile(nonExistantResource); + assertNull(theURL); + + theURL = ResourceUtils.getLocalFile(invalidResource); + assertNull(theURL); + + theURL = ResourceUtils.getLocalFile("file:///"); + assertNotNull(theURL); + + theURL = ResourceUtils.getLocalFile("file:///testdir/testfile.xml"); + assertNull(theURL); + + theURL = ResourceUtils.getLocalFile(null); + assertNull(theURL); + } + + /** + * Test get resource as stream. + */ + @Test + public void testGetResourceAsStream() { + InputStream theStream = ResourceUtils.getResourceAsStream(tmpDir.getAbsolutePath()); + assertNotNull(theStream); + + theStream = ResourceUtils.getResourceAsStream(tmpEmptyFile.getAbsolutePath()); + assertNotNull(theStream); + + theStream = ResourceUtils.getResourceAsStream(tmpUsedFile.getAbsolutePath()); + assertNotNull(theStream); + + theStream = ResourceUtils.getResourceAsStream(jarDirResource); + assertNotNull(theStream); + + theStream = ResourceUtils.getResourceAsStream(jarFileResource); + assertNotNull(theStream); + + theStream = ResourceUtils.getResourceAsStream(pathDirResource); + assertNotNull(theStream); + + theStream = ResourceUtils.getResourceAsStream(pathFileResource); + assertNotNull(theStream); + + theStream = ResourceUtils.getResourceAsStream("src/test/resources/" + pathDirResource); + assertNotNull(theStream); + + theStream = ResourceUtils.getResourceAsStream("src/test/resources/" + pathFileResource); + assertNotNull(theStream); + + theStream = ResourceUtils.getResourceAsStream(nonExistantResource); + assertNull(theStream); + + theStream = ResourceUtils.getResourceAsStream(invalidResource); + assertNull(theStream); + + theStream = ResourceUtils.getResourceAsStream(null); + assertNull(null); + + theStream = ResourceUtils.getResourceAsStream(""); + assertNull(null); + } + + /** + * Test get resource as string. + */ + @Test + public void testGetResourceAsString() { + String theString = ResourceUtils.getResourceAsString(tmpDir.getAbsolutePath()); + assertNotNull(theString); + + theString = ResourceUtils.getResourceAsString(tmpEmptyFile.getAbsolutePath()); + assertTrue(theString.equals("")); + + theString = ResourceUtils.getResourceAsString(tmpUsedFile.getAbsolutePath()); + assertTrue(theString.equals("Bluebirds fly over the rainbow")); + + theString = ResourceUtils.getResourceAsString(jarFileResource); + assertNotNull(theString); + + theString = ResourceUtils.getResourceAsString(pathDirResource); + assertNotNull(theString); + + theString = ResourceUtils.getResourceAsString(pathFileResource); + assertNotNull(theString); + + theString = ResourceUtils.getResourceAsString("src/test/resources/" + pathDirResource); + assertNotNull(theString); + + theString = ResourceUtils.getResourceAsString("src/test/resources/" + pathFileResource); + assertNotNull(theString); + + theString = ResourceUtils.getResourceAsString(nonExistantResource); + assertNull(theString); + + theString = ResourceUtils.getResourceAsString(invalidResource); + assertNull(theString); + + theString = ResourceUtils.getResourceAsString(null); + assertNull(theString); + + theString = ResourceUtils.getResourceAsString(""); + assertEquals("org\ntestdir\n", theString); + } + + @Test + public void testGetURL4Resource() { + URL theURL = ResourceUtils.getURL4Resource(tmpDir.getAbsolutePath()); + assertNotNull(theURL); + + theURL = ResourceUtils.getURL4Resource(tmpEmptyFile.getAbsolutePath()); + assertNotNull(theURL); + + theURL = ResourceUtils.getURL4Resource(tmpUsedFile.getAbsolutePath()); + assertNotNull(theURL); + + theURL = ResourceUtils.getURL4Resource(jarDirResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getURL4Resource(jarFileResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getURL4Resource(pathDirResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getURL4Resource(pathFileResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getURL4Resource("src/test/resources/" + pathDirResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getURL4Resource("src/test/resources/" + pathFileResource); + assertNotNull(theURL); + + theURL = ResourceUtils.getURL4Resource(nonExistantResource); + assertNull(theURL); + + theURL = ResourceUtils.getURL4Resource(invalidResource); + assertNull(theURL); + } + + @Test + public void testGetFilePath4Resource() { + assertNull(ResourceUtils.getFilePath4Resource(null)); + assertEquals("/something/else", ResourceUtils.getFilePath4Resource("/something/else")); + assertTrue(ResourceUtils.getFilePath4Resource("xml/example.xml").endsWith("xml/example.xml")); + } + + /** + * Cleandown resource utils test. + */ + @After + public void cleandownResourceUtilsTest() { + tmpEmptyFile.delete(); + tmpUsedFile.delete(); + } +} 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 new file mode 100644 index 000000000..4e5cba374 --- /dev/null +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TextFileUtilsTest.java @@ -0,0 +1,52 @@ +/* + * ============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.io.File; +import java.io.FileInputStream; +import java.io.IOException; + +import org.junit.Test; +import org.onap.policy.apex.model.utilities.TextFileUtils; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TextFileUtilsTest { + + @Test + public void test() throws IOException { + File tempTextFile = File.createTempFile("Test", "txt"); + + TextFileUtils.putStringAsTextFile("This is the contents of a text file", tempTextFile.getAbsolutePath()); + + String textFileString0 = TextFileUtils.getTextFileAsString(tempTextFile.getAbsolutePath()); + assertEquals("This is the contents of a text file", textFileString0); + + FileInputStream fis = new FileInputStream(tempTextFile); + 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 new file mode 100644 index 000000000..10a31e3b9 --- /dev/null +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/TreeMapUtilsTest.java @@ -0,0 +1,87 @@ +/* + * ============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.List; +import java.util.Map.Entry; +import java.util.TreeMap; + +import org.junit.Test; +import org.onap.policy.apex.model.utilities.TreeMapUtils; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TreeMapUtilsTest { + + @Test + public void test() { + 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()); + } +} diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/ParserTest.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/ParserTest.java new file mode 100644 index 000000000..bf4226102 --- /dev/null +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/ParserTest.java @@ -0,0 +1,92 @@ +/*- + * ============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.typeutils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.fail; + +import java.lang.reflect.Type; + +import org.antlr.v4.runtime.ANTLRInputStream; +import org.antlr.v4.runtime.BailErrorStrategy; +import org.antlr.v4.runtime.CharStream; +import org.antlr.v4.runtime.CommonTokenStream; +import org.antlr.v4.runtime.TokenStream; +import org.junit.Test; + + +/** + */ +public class ParserTest { + @Test + public void testParser() { + final CharStream stream = new ANTLRInputStream( + "java.util.Map<java.util.List<java.lang.Integer>,java.util.Set<java.lang.String>>"); + final TokenStream tokenStream = new CommonTokenStream(new ParametrizedTypeLexer(stream)); + + final ParametrizedTypeParser parser = new ParametrizedTypeParser(tokenStream); + parser.removeErrorListeners(); + parser.setErrorHandler(new BailErrorStrategy()); + parser.setBuildParseTree(true); + assertEquals("java.util.Map<java.util.List<java.lang.Integer>, java.util.Set<java.lang.String>>", + parser.type().value.build().getTypeName()); + + } + + @Test + public void testBuilder() throws IllegalArgumentException { + String t = "java.util.Map<java.util.List<java.lang.Integer>,java.util.Set<java.lang.String>>"; + Type ret = TypeBuilder.build(t); + assertEquals("java.util.Map<java.util.List<java.lang.Integer>, java.util.Set<java.lang.String>>", + ret.getTypeName()); + assertEquals(java.util.Map.class, TypeBuilder.getJavaTypeClass(ret)); + + final Type[] args = TypeBuilder.getJavaTypeParameters(ret); + assertEquals("java.util.List<java.lang.Integer>", args[0].getTypeName()); + assertEquals("java.util.Set<java.lang.String>", args[1].getTypeName()); + t = "java.lang.Integer"; + ret = TypeBuilder.build(t); + assertEquals(java.lang.Integer.class, TypeBuilder.getJavaTypeClass(ret)); + + } + + @Test + public void testBoundaryConditions() { + try { + TypeBuilder.build(null); + fail("Test should throw exception"); + } catch (final IllegalArgumentException e) { + assertEquals( + "Blank type string passed to org.onap.policy.apex.model.utilities.typeutils.TypeBuilder.build(String type)", + e.getMessage()); + } + + try { + TypeBuilder.build("org.zooby.Wooby"); + fail("Test should throw exception"); + } catch (final IllegalArgumentException e) { + assertEquals(e.getMessage(), "Failed to build type 'org.zooby.Wooby': java.lang.IllegalArgumentException: " + + "Class 'org.zooby.Wooby' not found. Also looked for a class called 'java.lang.org.zooby.Wooby'"); + } + + assertEquals(TypeBuilder.getJavaTypeClass("java.lang.String"), String.class); + } +} diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/TestKeyComparer.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/TestKeyComparer.java new file mode 100644 index 000000000..c2e84c6fb --- /dev/null +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/TestKeyComparer.java @@ -0,0 +1,50 @@ +/* + * ============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.typeutils; + +import static org.junit.Assert.assertFalse; +import static org.junit.Assert.assertTrue; + +import org.junit.Test; +import org.onap.policy.apex.model.utilities.comparison.KeyComparer; +import org.onap.policy.apex.model.utilities.comparison.KeyDifference; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestKeyComparer { + + @Test + public void test() { + KeyDifference<String> keyDifference = new KeyComparer<String>().compareKeys("Hello", "Goodbye"); + + assertFalse(keyDifference.isEqual()); + assertTrue("Hello".equals(keyDifference.getLeftKey().toString())); + assertTrue("Goodbye".equals(keyDifference.getRightKey().toString())); + + assertTrue("left key Hello and right key Goodbye differ\n".equals(keyDifference.asString(true))); + assertTrue("left key Hello and right key Goodbye differ\n".equals(keyDifference.asString(false))); + + KeyDifference<String> keyDifference2 = new KeyComparer<String>().compareKeys("Here", "Here"); + assertTrue("".equals(keyDifference2.asString(true))); + assertTrue("left key Here equals right key Here\n".equals(keyDifference2.asString(false))); + } +} diff --git a/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/TestKeyedMapComparer.java b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/TestKeyedMapComparer.java new file mode 100644 index 000000000..33b8101a1 --- /dev/null +++ b/model/utilities/src/test/java/org/onap/policy/apex/model/utilities/typeutils/TestKeyedMapComparer.java @@ -0,0 +1,73 @@ +/* + * ============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.typeutils; + +import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertNotNull; +import static org.junit.Assert.assertTrue; + +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; + +/** + * @author Liam Fallon (liam.fallon@ericsson.com) + */ +public class TestKeyedMapComparer { + + @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); + + assertTrue(kmComparedSame.getIdenticalValues().equals(leftMap)); + 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)); + } +} |