1 /*******************************************************************************
\r
2 * Copyright 2017 ZTE, Inc. and others.
\r
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
\r
5 * in compliance with the License. You may obtain a copy of the License at
\r
7 * http://www.apache.org/licenses/LICENSE-2.0
\r
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
\r
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
\r
11 * or implied. See the License for the specific language governing permissions and limitations under
\r
13 ******************************************************************************/
\r
14 package org.onap.policy.msb.client;
\r
17 import org.junit.rules.ExpectedException;
\r
18 import org.mockito.Mock;
\r
19 import org.mockito.MockitoAnnotations;
\r
20 import org.onap.msb.sdk.discovery.common.RouteException;
\r
21 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
\r
22 import org.onap.msb.sdk.discovery.entity.NodeInfo;
\r
23 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
\r
24 import org.onap.policy.msb.client.MSBServiceManager;
\r
25 import org.onap.policy.msb.client.Node;
\r
27 import java.io.IOException;
\r
28 import java.lang.reflect.Field;
\r
29 import java.net.InetAddress;
\r
30 import java.net.UnknownHostException;
\r
31 import java.util.HashSet;
\r
32 import java.util.Set;
\r
34 import static org.junit.Assert.assertEquals;
\r
35 import static org.junit.Assert.assertNotNull;
\r
36 import static org.junit.Assert.assertNull;
\r
37 import static org.junit.Assert.assertTrue;
\r
38 import static org.mockito.Mockito.when;
\r
40 public class MSBServiceManagerTest {
\r
42 private MSBServiceClient msbClient;
\r
45 public ExpectedException expectedException = ExpectedException.none();
\r
47 private MSBServiceManager msbManager;
\r
49 public MSBServiceManagerTest(){}
\r
52 public static void setUpClass(){}
\r
55 public static void tearDownClass(){}
\r
58 public void setUp() throws Exception {
\r
59 MockitoAnnotations.initMocks(this);
\r
60 msbManager = new MSBServiceManager(msbClient);
\r
64 public void tearDown() {
\r
68 public void testByActor () throws RouteException,UnknownHostException {
\r
69 MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");
\r
70 when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenReturn(serviceInfo);
\r
71 Node node = msbManager.getNode("AAI");
\r
72 assertNotNull(node);
\r
73 serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8840");
\r
74 when(msbClient.queryMicroServiceInfo("so","v2")).thenReturn(serviceInfo);
\r
75 node = msbManager.getNode("SO");
\r
76 assertNotNull(node);
\r
78 serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8082");
\r
79 when(msbClient.queryMicroServiceInfo("nfvo-nslcm","v1")).thenReturn(serviceInfo);
\r
80 node = msbManager.getNode("VFC");
\r
81 assertNotNull(node);
\r
86 public void testByActor_when_actorNotExist_returnNull () throws RouteException,UnknownHostException {
\r
87 MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");
\r
88 when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenReturn(serviceInfo);
\r
89 Node node = msbManager.getNode("DDD");
\r
94 public void testByServiceNameAndVersion () throws RouteException,UnknownHostException {
\r
95 MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");
\r
96 when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenReturn(serviceInfo);
\r
97 Node node = msbManager.getNode("aai-search","v11");
\r
98 assertNotNull(node);
\r
102 public void testByServiceNameAndVersion_when_serice_notRegistedToMSB () throws RouteException,UnknownHostException {
\r
103 MicroServiceFullInfo serviceInfo = this.build(InetAddress.getLocalHost().getHostAddress(),"8843");
\r
104 when(msbClient.queryMicroServiceInfo("aai-search","v11")).thenThrow(new RouteException());
\r
105 Node node = msbManager.getNode("aai-search","v11");
\r
106 assertNotNull(node);
\r
107 assertTrue(node.getName() == "aai-search");
\r
108 assertTrue(node.getIp() == null);
\r
109 assertTrue(node.getPort() == null);
\r
113 public void testReadMsbPolicyProperites_noPropertyFileSpecifed_throwsException() throws MSBServiceException, IOException {
\r
114 expectedException.expect(MSBServiceException.class);
\r
115 expectedException.expectMessage("No msb.policy.properties specified.");
\r
116 System.clearProperty("msb.policy.properties");
\r
117 msbManager = new MSBServiceManager();
\r
121 public void testReadMsbPolicyProperites_propertyFileDoesNotExist_throwsException() throws MSBServiceException, IOException {
\r
122 expectedException.expect(MSBServiceException.class);
\r
123 expectedException.expectMessage("No msb.policy.properties specified.");
\r
124 System.setProperty("msb.policy.properties", "nonExistingPropertyFile.txt");
\r
125 msbManager = new MSBServiceManager();
\r
126 System.clearProperty("msb.policy.properties");
\r
130 public void testReadMsbPolicyProperites_propertyFileExists() throws MSBServiceException, IOException, NoSuchFieldException, SecurityException, IllegalArgumentException, IllegalAccessException {
\r
131 System.setProperty("msb.policy.properties", "src/test/resources/msbPropertyFile.properties");
\r
132 msbManager = new MSBServiceManager();
\r
133 System.clearProperty("msb.policy.properties");
\r
135 Field factoryField = msbManager.getClass().getDeclaredField("factory");
\r
136 factoryField.setAccessible(true);
\r
137 MSBServiceFactory msbServiceFactory = (MSBServiceFactory) factoryField.get(msbManager);
\r
139 Field msbClientField = msbServiceFactory.getClass().getDeclaredField("msbClient");
\r
140 msbClientField.setAccessible(true);
\r
141 MSBServiceClient msbClient = (MSBServiceClient) msbClientField.get(msbServiceFactory);
\r
142 assertEquals("127.0.0.1:20", msbClient.getMsbSvrAddress());
\r
145 public static MicroServiceFullInfo build(String ip,String port){
\r
146 MicroServiceFullInfo serviceInfo = new MicroServiceFullInfo();
\r
147 Set<NodeInfo> nodes = new HashSet<NodeInfo>();
\r
148 NodeInfo node= new NodeInfo();
\r
149 node.setPort(port);
\r
152 serviceInfo.setNodes(nodes);
\r
153 return serviceInfo;
\r