Fix technical debt/JUnit on MSB 31/30431/3
authoreeimmis <michael.morris@ericsson.com>
Tue, 6 Feb 2018 09:43:40 +0000 (09:43 +0000)
committermmis <michael.morris@ericsson.com>
Wed, 7 Feb 2018 15:27:54 +0000 (15:27 +0000)
Unit tests added and technical debt removed

Issue-ID: POLICY-455
Change-Id: Ib10613ff7cac64bb576046e038b2acbd66152648
Signed-off-by: mmis <michael.morris@ericsson.com>
controlloop/common/msb/pom.xml
controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceFactory.java
controlloop/common/msb/src/main/java/org/onap/policy/msb/client/MSBServiceManager.java
controlloop/common/msb/src/test/java/org/onap/policy/msb/client/MSBServiceExceptionTest.java [new file with mode: 0644]
controlloop/common/msb/src/test/java/org/onap/policy/msb/client/MSBServiceManagerTest.java
controlloop/common/msb/src/test/java/org/onap/policy/msb/client/NodeTest.java [new file with mode: 0644]
controlloop/common/msb/src/test/resources/msbPropertyFile.properties [new file with mode: 0644]

index 54e68da..a24f333 100644 (file)
             <version>1.2.3</version>
             <scope>test</scope>
         </dependency>
+        <dependency>
+            <groupId>org.onap.policy.common</groupId>
+            <artifactId>utils-test</artifactId>
+            <version>${project.version}</version>
+            <scope>test</scope>
+        </dependency>
     </dependencies>
 
 </project>
index 5332c0d..7ebb5d6 100644 (file)
@@ -20,7 +20,6 @@ import java.io.Serializable;
 import java.nio.file.Files;\r
 import java.nio.file.Path;\r
 import java.nio.file.Paths;\r
-import java.util.Iterator;\r
 import java.util.Properties;\r
 \r
 import org.onap.msb.sdk.discovery.common.RouteException;\r
@@ -34,7 +33,7 @@ import org.slf4j.LoggerFactory;
 public class MSBServiceFactory implements Serializable {\r
        private static final long serialVersionUID = 4638414146278012425L;\r
        private static final Logger logger = LoggerFactory.getLogger(MSBServiceFactory.class);\r
-    private static final String msbPropertyFile = "msb.policy.properties";\r
+    private static final String MSB_PROPERTY_FILE = "msb.policy.properties";\r
     private static final String MSB_IP = "msb.ip";\r
     private static final String MSB_PORT = "msb.port";\r
     private transient MSBServiceClient msbClient;\r
@@ -50,15 +49,16 @@ public class MSBServiceFactory implements Serializable {
 \r
     private void init() throws MSBServiceException,IOException  {\r
         properties = new Properties();\r
-        Path file = Paths.get(System.getProperty(msbPropertyFile));\r
-        if (file == null) {\r
+        String propertyFilePath = System.getProperty(MSB_PROPERTY_FILE);\r
+        if (propertyFilePath == null) {\r
             throw new MSBServiceException("No msb.policy.properties specified.");\r
         }\r
-        if (Files.notExists(file)) {\r
+        Path file = Paths.get(propertyFilePath);\r
+        if (!file.toFile().exists()) {\r
             throw new MSBServiceException("No msb.policy.properties specified.");\r
         }\r
 \r
-        if (Files.isReadable(file) == false) {\r
+        if (!Files.isReadable(file)) {\r
             throw new MSBServiceException ("Repository is NOT readable: " + file.toAbsolutePath());\r
         }\r
         try(InputStream is = new FileInputStream(file.toFile())){\r
@@ -94,9 +94,7 @@ public class MSBServiceFactory implements Serializable {
         node.setName(serviceName);\r
         try {\r
             MicroServiceFullInfo serviceInfo = msbClient.queryMicroServiceInfo(serviceName,version);\r
-            Iterator iterator = serviceInfo.getNodes().iterator();\r
-            while(iterator.hasNext()) {\r
-                NodeInfo nodeInfo = (NodeInfo)iterator.next();\r
+            for (NodeInfo nodeInfo: serviceInfo.getNodes()){\r
                 node.setIp(nodeInfo.getIp());\r
                 node.setPort(nodeInfo.getPort());\r
             }\r
index cbff8d8..ccb13ec 100644 (file)
 package org.onap.policy.msb.client;\r
 \r
 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;\r
-import org.slf4j.Logger;\r
-import org.slf4j.LoggerFactory;\r
 \r
 import java.io.IOException;\r
 import java.io.Serializable;\r
 \r
 public class MSBServiceManager implements Serializable {\r
-    private static final Logger logger = LoggerFactory.getLogger(MSBServiceManager.class);\r
     private static final long serialVersionUID = -2517971308551895215L;\r
     private MSBServiceFactory factory;\r
 \r
diff --git a/controlloop/common/msb/src/test/java/org/onap/policy/msb/client/MSBServiceExceptionTest.java b/controlloop/common/msb/src/test/java/org/onap/policy/msb/client/MSBServiceExceptionTest.java
new file mode 100644 (file)
index 0000000..80c453e
--- /dev/null
@@ -0,0 +1,30 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 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.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.policy.msb.client;
+
+import org.junit.Test;
+import org.onap.policy.common.utils.test.ExceptionsTester;
+
+public class MSBServiceExceptionTest extends ExceptionsTester{
+       
+       @Test
+       public void test() throws Exception {
+               test(MSBServiceException.class);
+       }
+
+}
index 9ab54f7..771bb49 100644 (file)
@@ -14,6 +14,7 @@
 package org.onap.policy.msb.client;\r
 \r
 import org.junit.*;\r
+import org.junit.rules.ExpectedException;\r
 import org.mockito.Mock;\r
 import org.mockito.MockitoAnnotations;\r
 import org.onap.msb.sdk.discovery.common.RouteException;\r
@@ -23,11 +24,14 @@ import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
 import org.onap.policy.msb.client.MSBServiceManager;\r
 import org.onap.policy.msb.client.Node;\r
 \r
+import java.io.IOException;\r
+import java.lang.reflect.Field;\r
 import java.net.InetAddress;\r
 import java.net.UnknownHostException;\r
 import java.util.HashSet;\r
 import java.util.Set;\r
 \r
+import static org.junit.Assert.assertEquals;\r
 import static org.junit.Assert.assertNotNull;\r
 import static org.junit.Assert.assertNull;\r
 import static org.junit.Assert.assertTrue;\r
@@ -36,6 +40,9 @@ import static org.mockito.Mockito.when;
 public class MSBServiceManagerTest {\r
     @Mock\r
     private MSBServiceClient msbClient;\r
+    \r
+    @Rule\r
+       public ExpectedException expectedException = ExpectedException.none();\r
 \r
     private MSBServiceManager msbManager;\r
 \r
@@ -101,6 +108,39 @@ public class MSBServiceManagerTest {
         assertTrue(node.getIp() == null);\r
         assertTrue(node.getPort() == null);\r
     }\r
+    \r
+    @Test\r
+    public void testReadMsbPolicyProperites_noPropertyFileSpecifed_throwsException() throws MSBServiceException, IOException {\r
+         expectedException.expect(MSBServiceException.class);\r
+         expectedException.expectMessage("No msb.policy.properties specified.");\r
+         System.clearProperty("msb.policy.properties");\r
+        msbManager = new MSBServiceManager();\r
+    }\r
+    \r
+    @Test \r
+    public void testReadMsbPolicyProperites_propertyFileDoesNotExist_throwsException() throws MSBServiceException, IOException {\r
+         expectedException.expect(MSBServiceException.class);\r
+         expectedException.expectMessage("No msb.policy.properties specified.");\r
+         System.setProperty("msb.policy.properties", "nonExistingPropertyFile.txt");\r
+        msbManager = new MSBServiceManager();\r
+        System.clearProperty("msb.policy.properties");\r
+    }\r
+    \r
+    @Test \r
+    public void testReadMsbPolicyProperites_propertyFileExists() throws MSBServiceException, IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {\r
+         System.setProperty("msb.policy.properties", "src/test/resources/msbPropertyFile.properties");\r
+      msbManager = new MSBServiceManager();\r
+      System.clearProperty("msb.policy.properties");\r
+      \r
+      Field factoryField = msbManager.getClass().getDeclaredField("factory");\r
+      factoryField.setAccessible(true);\r
+      MSBServiceFactory msbServiceFactory = (MSBServiceFactory) factoryField.get(msbManager);\r
+      \r
+      Field msbClientField = msbServiceFactory.getClass().getDeclaredField("msbClient");\r
+      msbClientField.setAccessible(true);\r
+      MSBServiceClient msbClient = (MSBServiceClient) msbClientField.get(msbServiceFactory);\r
+      assertEquals("127.0.0.1:20", msbClient.getMsbSvrAddress());\r
+    }\r
 \r
     public static MicroServiceFullInfo build(String ip,String port){\r
         MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo();\r
diff --git a/controlloop/common/msb/src/test/java/org/onap/policy/msb/client/NodeTest.java b/controlloop/common/msb/src/test/java/org/onap/policy/msb/client/NodeTest.java
new file mode 100644 (file)
index 0000000..ed1d55f
--- /dev/null
@@ -0,0 +1,62 @@
+/*-
+ * ============LICENSE_START=======================================================
+ * Copyright (C) 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.
+ * ============LICENSE_END=========================================================
+ */
+package org.onap.policy.msb.client;
+
+import static org.junit.Assert.*;
+
+import org.junit.Test;
+
+public class NodeTest {
+
+       @Test
+       public void testSetAndGetName() {
+               Node node = new Node();
+               final String name = "myName";
+               node.setName(name);
+               assertEquals(name, node.getName());
+       }
+
+       @Test
+       public void testSetAndGetIp() {
+               Node node = new Node();
+               final String ip = "127.0.0.1";
+               node.setIp(ip);
+               assertEquals(ip, node.getIp());
+       }
+
+       @Test
+       public void testSetAndGetPort() {
+               Node node = new Node();
+               final String port = "1001";
+               node.setPort(port);
+               assertEquals(port, node.getPort());
+       }
+
+       @Test
+       public void testToString() {
+               Node node = new Node();
+               final String name = "myName";
+               final String ip = "127.0.0.1";
+               final String port = "1001";
+               node.setName(name);
+               node.setIp(ip);
+               node.setPort(port);
+               assertEquals("Node{name='myName', ip='127.0.0.1', port='1001'}", node.toString());
+       }
+
+}
diff --git a/controlloop/common/msb/src/test/resources/msbPropertyFile.properties b/controlloop/common/msb/src/test/resources/msbPropertyFile.properties
new file mode 100644 (file)
index 0000000..98666e2
--- /dev/null
@@ -0,0 +1,2 @@
+msb.ip=127.0.0.1
+msb.port=20
\ No newline at end of file