<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>
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
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
\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
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
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
--- /dev/null
+/*-
+ * ============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);
+ }
+
+}
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
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
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
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
--- /dev/null
+/*-
+ * ============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());
+ }
+
+}
--- /dev/null
+msb.ip=127.0.0.1
+msb.port=20
\ No newline at end of file