diff options
8 files changed, 490 insertions, 6 deletions
diff --git a/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineCommandTest.java b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineCommandTest.java new file mode 100644 index 000000000..020e272dd --- /dev/null +++ b/auth/cli-editor/src/test/java/org/onap/policy/apex/auth/clieditor/CommandLineCommandTest.java @@ -0,0 +1,134 @@ +/*- + * ============LICENSE_START======================================================= + * 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.auth.clieditor; + +import static org.junit.Assert.*; + +import java.util.List; + +import org.junit.Before; +import org.junit.Test; + +public class CommandLineCommandTest { + + CommandLineCommand commandLineCommand = null; + + @Before + public void initializeCommandLineCommand() { + commandLineCommand = new CommandLineCommand(); + } + + @Test + public void testCommandLine() { + commandLineCommand.setName("TestName"); + commandLineCommand.setDescription("testDescription"); + commandLineCommand.setSystemCommand(true); + assertTrue(commandLineCommand.isSystemCommand()); + assertEquals("testDescription", commandLineCommand.getDescription()); + assertEquals("TestName", commandLineCommand.getName()); + assertEquals("CLICommand [name=TestName,keywordlist=[], argumentList=[], apiMethod=, systemCommand=true, description=testDescription]",commandLineCommand.toString()); + } + + @Test(expected = CommandLineException.class) + public void testInvalidApiClassName() { + commandLineCommand.getApiClassName(); + } + + @Test + public void testGetValidApiClassName() { + commandLineCommand.setApiMethod("Java.Get"); + assertEquals("Java", commandLineCommand.getApiClassName()); + } + + @Test(expected = CommandLineException.class) + public void testInvalidApiMethodName() { + commandLineCommand.getApiMethodName(); + } + + @Test(expected = CommandLineException.class) + public void testInvalidApiMethod() { + commandLineCommand.setApiMethod("fail."); + assertEquals("fail.", commandLineCommand.getApiMethod()); + commandLineCommand.getApiMethodName(); + } + + @Test + public void testValidApiMethodName() { + commandLineCommand.setApiMethod("Java.Get"); + assertEquals("Get",commandLineCommand.getApiMethodName()); + } + + @Test + public void testGetHelp() { + List<String> keywordList = commandLineCommand.getKeywordlist(); + List<CommandLineArgument> argumentList = commandLineCommand.getArgumentList(); + assertEquals("{}: ",commandLineCommand.getHelp()); + keywordList.add("TestKeyword"); + argumentList.add(new CommandLineArgument("TestArgument")); + argumentList.add(null); + assertEquals("TestKeyword {}: \n" + + " TestArgument: (M) ",commandLineCommand.getHelp()); + } + + @Test + public void testCompareTo() { + assertEquals(0,commandLineCommand.compareTo(commandLineCommand)); + CommandLineCommand otherCommand = new CommandLineCommand(); + otherCommand.setSystemCommand(true); + assertEquals(6,commandLineCommand.compareTo(otherCommand)); + otherCommand.getArgumentList().add(new CommandLineArgument("testArgument")); + assertEquals(-609496833, commandLineCommand.compareTo(otherCommand)); + } + + @Test + public void testCompareKeywordList() { + CommandLineCommand otherCommand = new CommandLineCommand(); + otherCommand.getKeywordlist().add("test"); + assertEquals(-1, commandLineCommand.compareTo(otherCommand)); + commandLineCommand.getKeywordlist().add("test"); + assertEquals(0, commandLineCommand.compareTo(otherCommand)); + commandLineCommand.getKeywordlist().add("test2"); + assertEquals(1, commandLineCommand.compareTo(otherCommand)); + otherCommand.getKeywordlist().add("test3"); + assertEquals(-1, commandLineCommand.compareTo(otherCommand)); + } + + @Test + public void testHashCode() { + CommandLineCommand otherCommand = new CommandLineCommand(); + assertEquals(commandLineCommand.hashCode(), otherCommand.hashCode()); + commandLineCommand.getKeywordlist().add("Test"); + otherCommand.setDescription(null); + otherCommand.setApiMethod(null); + otherCommand.setName(null); + assertNotEquals(commandLineCommand.hashCode(), otherCommand.hashCode()); + } + + @Test + public void testEquals() { + CommandLineCommand otherCommand = new CommandLineCommand(); + assertFalse(commandLineCommand.equals(new Object())); + assertTrue(commandLineCommand.equals(commandLineCommand)); + assertFalse(commandLineCommand.equals(null)); + assertTrue(commandLineCommand.equals(otherCommand)); + otherCommand.getKeywordlist().add("TestKeyword"); + assertFalse(commandLineCommand.equals(otherCommand)); + } +} diff --git a/core/core-infrastructure/pom.xml b/core/core-infrastructure/pom.xml index fcae62e04..956d89400 100644 --- a/core/core-infrastructure/pom.xml +++ b/core/core-infrastructure/pom.xml @@ -42,6 +42,11 @@ <groupId>com.google.code.gson</groupId> <artifactId>gson</artifactId> </dependency> + <dependency> + <groupId>org.mockito</groupId> + <artifactId>mockito-all</artifactId> + <scope>test</scope> + </dependency> </dependencies> <profiles> diff --git a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/java/classes/ClassUtilsTest.java b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/java/classes/ClassUtilsTest.java new file mode 100644 index 000000000..4e69f44ee --- /dev/null +++ b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/java/classes/ClassUtilsTest.java @@ -0,0 +1,68 @@ +/*- + * ============LICENSE_START======================================================= + * 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.core.infrastructure.java.classes; + +import static org.junit.Assert.*; + +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.util.Set; +import java.util.TreeSet; + +import org.junit.Test; +import org.mockito.Mockito; + +public class ClassUtilsTest { + + @Test + public void testGetClassNames() throws IOException { + InputStream input = null; + ClassUtils.getClassNames(); + assertEquals(new TreeSet<>(), ClassUtils.processJar(input)); + } + + @Test + public void testProcessFileName() { + assertEquals("testing.txt",ClassUtils.processFileName("testing.txt")); + assertNull(ClassUtils.processFileName(null)); + assertEquals("",ClassUtils.processFileName("/classes/")); + } + + @Test + public void testProcessDir() throws Exception { + File mockFile = Mockito.mock(File.class); + File mockChildFile = Mockito.mock(File.class); + Mockito.when(mockFile.isDirectory()).thenReturn(false); + assertEquals(new TreeSet<>(),ClassUtils.processDir(mockFile, "Here")); + assertEquals(new TreeSet<>(),ClassUtils.processDir(null, "Test")); + Mockito.when(mockFile.isDirectory()).thenReturn(true); + File[] files = {mockChildFile}; + Mockito.when(mockFile.listFiles()).thenReturn(files); + Mockito.when(mockChildFile.getName()).thenReturn("test.class"); + Mockito.when(mockChildFile.getAbsolutePath()).thenReturn("/test/"); + assertEquals(Set.of(".test."),ClassUtils.processDir(mockFile, "Here")); + Mockito.when(mockChildFile.getName()).thenReturn("test.class"); + assertEquals(Set.of(".test."),ClassUtils.processDir(mockFile, "Here")); + Mockito.when(mockChildFile.getName()).thenReturn("$test.class"); + assertEquals(new TreeSet<>(),ClassUtils.processDir(mockFile, "Here")); + } + +} diff --git a/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/MessagingUtilsTest.java b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/MessagingUtilsTest.java new file mode 100644 index 000000000..91b34e20c --- /dev/null +++ b/core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/MessagingUtilsTest.java @@ -0,0 +1,67 @@ +/*- + * ============LICENSE_START======================================================= + * 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.core.infrastructure.messaging; + +import static org.junit.Assert.*; + +import java.io.IOException; +import java.net.InetAddress; +import java.net.UnknownHostException; + +import org.junit.Test; +import org.onap.policy.apex.core.infrastructure.messaging.util.MessagingUtils; + +public class MessagingUtilsTest { + + @Test + public void testCheckPort() throws UnknownHostException, IOException { + assertEquals(1,MessagingUtils.checkPort(1)); + assertEquals(1,MessagingUtils.findPort(1)); + } + + @Test(expected = IllegalArgumentException.class) + public void testIllegalArgumentException() { + assertEquals(1,MessagingUtils.findPort(65536)); + } + + @Test + public void testGetHost() throws UnknownHostException { + InetAddress host = InetAddress.getLocalHost(); + assertEquals(host,MessagingUtils.getHost()); + } + + @Test + public void testValidAllocateAddress() throws UnknownHostException { + assertNotNull(MessagingUtils.getLocalHostLanAddress()); + assertEquals(3306,MessagingUtils.allocateAddress(3306)); + } + + @Test(expected = IllegalArgumentException.class) + public void testInvalidAllocateAddress() { + assertEquals(1,MessagingUtils.allocateAddress(1)); + } + + @Test + public void testSerializeObject() { + String testString = "Test"; + MessagingUtils.serializeObject(new Object()); + assertNotNull(MessagingUtils.serializeObject(testString)); + } +} diff --git a/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionConceptTest.java b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionConceptTest.java new file mode 100644 index 000000000..623588267 --- /dev/null +++ b/examples/examples-adaptive/src/test/java/org/onap/policy/apex/examples/adaptive/AnomalyDetectionConceptTest.java @@ -0,0 +1,124 @@ +/*- + * ============LICENSE_START======================================================= + * 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.examples.adaptive; + +import static org.junit.Assert.*; + +import java.util.LinkedList; +import java.util.List; + +import org.junit.Test; +import org.onap.policy.apex.examples.adaptive.concepts.AnomalyDetection; + +public class AnomalyDetectionConceptTest { + + @Test + public void testToString(){ + AnomalyDetection anomalyDetection = new AnomalyDetection(); + List<Double> newAnomalyScores = new LinkedList<>(); + newAnomalyScores.add((double) 55); + anomalyDetection.setAnomalyScores(newAnomalyScores); + anomalyDetection.setFrequency(55); + assertEquals(newAnomalyScores, anomalyDetection.getAnomalyScores()); + assertTrue(anomalyDetection.checkSetAnomalyScores()); + assertEquals(55,anomalyDetection.getFrequency()); + assertEquals(true,anomalyDetection.getFirstRound()); + assertEquals("AnomalyDetection [firstRound=true, frequency=55, anomalyScores=[55.0], frequencyForecasted=null]", anomalyDetection.toString()); + } + + @Test + public void testHashCode(){ + AnomalyDetection detection = new AnomalyDetection(); + AnomalyDetection compareDetection = new AnomalyDetection(); + assertEquals(detection.hashCode(), compareDetection.hashCode()); + detection.init(1); + assertTrue(detection.isInitialized()); + assertFalse(compareDetection.isInitialized()); + compareDetection.setAnomalyScores(null); + compareDetection.setFirstRound(false); + compareDetection.setFrequencyForecasted(new LinkedList<>()); + assertNotEquals(detection.hashCode(), compareDetection.hashCode()); + } + + @Test + public void testEquals() { + AnomalyDetection anomalyDetection = new AnomalyDetection(); + AnomalyDetection comparisonDetection = new AnomalyDetection(); + assertTrue(anomalyDetection.equals(comparisonDetection)); + //Compare object to itself + assertTrue(anomalyDetection.equals(anomalyDetection)); + //Compare object to null + assertFalse(anomalyDetection.equals(null)); + //compare object to string + assertFalse(anomalyDetection.equals("test")); + // Anomaly Scores comparison + anomalyDetection.setAnomalyScores(null); + assertFalse(anomalyDetection.equals(comparisonDetection)); + comparisonDetection.setAnomalyScores(null); + assertTrue(anomalyDetection.equals(comparisonDetection)); + List<Double> anomalyScores = new LinkedList<>(); + anomalyScores.add((double) 20); + anomalyDetection.setAnomalyScores(anomalyScores); + assertFalse(anomalyDetection.equals(comparisonDetection)); + comparisonDetection.setAnomalyScores(anomalyScores); + assertTrue(anomalyDetection.checkSetAnomalyScores()); + //First Round Checks + anomalyDetection.setFirstRound(false); + assertFalse(anomalyDetection.equals(comparisonDetection)); + anomalyDetection.setFirstRound(true); + //Frequency Checks + anomalyDetection.setFrequency(55); + assertFalse(anomalyDetection.equals(comparisonDetection)); + anomalyDetection.setFrequency(0); + //FrequencyForecasted Checks + List<Double> comparisonFrequency = new LinkedList<>(); + comparisonDetection.setFrequencyForecasted(comparisonFrequency); + assertFalse(anomalyDetection.equals(comparisonDetection)); + anomalyDetection.setFrequencyForecasted(anomalyScores); + assertFalse(anomalyDetection.equals(comparisonDetection)); + anomalyDetection.setFrequencyForecasted(comparisonFrequency); + assertTrue(anomalyDetection.equals(comparisonDetection)); + } + + @Test + public void testCheckSets(){ + AnomalyDetection anomalyDetection = new AnomalyDetection(); + assertFalse(anomalyDetection.checkSetAnomalyScores()); + List<Double> anomalyScores = new LinkedList<>(); + anomalyDetection.setAnomalyScores(anomalyScores); + assertFalse(anomalyDetection.checkSetAnomalyScores()); + anomalyScores.add((double)2); + anomalyDetection.setAnomalyScores(anomalyScores); + assertTrue(anomalyDetection.checkSetAnomalyScores()); + anomalyDetection.unsetAnomalyScores(); + assertFalse(anomalyDetection.checkSetAnomalyScores()); + assertEquals(null, anomalyDetection.getFrequencyForecasted()); + assertFalse(anomalyDetection.checkSetFrequencyForecasted()); + List<Double> frequencyForecasted = new LinkedList<>(); + anomalyDetection.setFrequencyForecasted(frequencyForecasted); + assertFalse(anomalyDetection.checkSetFrequencyForecasted()); + frequencyForecasted.add((double)2); + anomalyDetection.setFrequencyForecasted(frequencyForecasted); + assertTrue(anomalyDetection.checkSetFrequencyForecasted()); + anomalyDetection.unsetFrequencyForecasted(); + assertFalse(anomalyDetection.checkSetFrequencyForecasted()); + } +} diff --git a/model/basic-model/pom.xml b/model/basic-model/pom.xml index 3e11381a5..53c1adba3 100644 --- a/model/basic-model/pom.xml +++ b/model/basic-model/pom.xml @@ -1,7 +1,6 @@ <!-- ============LICENSE_START======================================================= - Copyright (C) 2018 Ericsson. All rights reserved. - Modifications Copyright (C) 2020 Nordix Foundation. + 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. @@ -34,6 +33,13 @@ <artifactId>basic-model</artifactId> <name>${project.artifactId}</name> <description>Basic Models used and model handling in Apex</description> + <properties> + <sonar.exclusions> + **/package-info.java, + **/TestApexModel.java, + **/TestApexModelCreator.java + </sonar.exclusions> + </properties> <dependencies> <dependency> diff --git a/tools/tools-common/src/main/java/org/onap/policy/apex/tools/common/OutputFile.java b/tools/tools-common/src/main/java/org/onap/policy/apex/tools/common/OutputFile.java index 59d15d19d..5b386861a 100644 --- a/tools/tools-common/src/main/java/org/onap/policy/apex/tools/common/OutputFile.java +++ b/tools/tools-common/src/main/java/org/onap/policy/apex/tools/common/OutputFile.java @@ -1,6 +1,7 @@ /*- * ============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. @@ -116,10 +117,6 @@ public class OutputFile { * @return null on success, an error message on error */ public String validate() { - if (StringUtils.isBlank(fileName)) { - return "file name was blank"; - } - final File file = toFile(); if (file.exists()) { if (!overwrite) { diff --git a/tools/tools-common/src/test/java/org/onap/policy/apex/tools/common/OutputFileTest.java b/tools/tools-common/src/test/java/org/onap/policy/apex/tools/common/OutputFileTest.java new file mode 100644 index 000000000..36c64e517 --- /dev/null +++ b/tools/tools-common/src/test/java/org/onap/policy/apex/tools/common/OutputFileTest.java @@ -0,0 +1,83 @@ +/*- + * ============LICENSE_START======================================================= + * 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.tools.common; + +import static org.junit.Assert.*; + +import java.io.File; +import java.nio.file.FileSystems; +import java.nio.file.Path; + +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + +public class OutputFileTest { + + final String testFileName = "testing.txt"; + final Path fp = FileSystems.getDefault().getPath(testFileName); + File file = fp.toFile(); + + @Before + public void setUp() { + if (file.exists()){ + file.delete(); + } + } + + @Test + public void testToWriter() { + OutputFile testFile = new OutputFile(testFileName,false); + testFile.validate(); + file.setReadable(false); + file.setWritable(false); + assertNull(testFile.toWriter()); + file.setWritable(true); + assertNotNull(testFile.toWriter()); + } + + @Test + public void testValidate() { + OutputFile testFile = new OutputFile(testFileName,true); + assertNull(testFile.validate()); + file.setReadable(false); + file.setWritable(false); + assertNotNull(testFile.validate()); + OutputFile testFile2 = new OutputFile(testFileName); + assertNotNull(testFile2.validate()); + assertEquals("file already exists",testFile2.validate()); + } + + @Test + public void testToOutputStream() { + OutputFile testFile = new OutputFile(testFileName,true); + assertNotNull(testFile.toOutputStream()); + file.setReadable(false); + file.setWritable(false); + assertNull(testFile.toOutputStream()); + } + + @After + public void testDown() { + if (file.exists()){ + file.delete(); + } + } +} |