From ea317372413753320db681efb9a1e469c3776f42 Mon Sep 17 00:00:00 2001 From: RossC Date: Thu, 7 May 2020 13:25:34 +0100 Subject: Unit tests for various classes Issue-ID: POLICY-1916 Change-Id: Ie7cafa16ce12ca542a4e76307caddb36b7753990 Signed-off-by: RossC --- core/core-infrastructure/pom.xml | 5 ++ .../java/classes/ClassUtilsTest.java | 68 ++++++++++++++++++++++ .../messaging/MessagingUtilsTest.java | 67 +++++++++++++++++++++ 3 files changed, 140 insertions(+) create mode 100644 core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/java/classes/ClassUtilsTest.java create mode 100644 core/core-infrastructure/src/test/java/org/onap/policy/apex/core/infrastructure/messaging/MessagingUtilsTest.java (limited to 'core/core-infrastructure') 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 @@ com.google.code.gson gson + + org.mockito + mockito-all + test + 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)); + } +} -- cgit 1.2.3-korg