2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;
19 import org.junit.Before;
20 import org.junit.Test;
21 import org.mockito.Mock;
22 import org.onap.msb.sdk.discovery.common.RouteException;
23 import org.onap.msb.sdk.discovery.entity.MicroServiceFullInfo;
24 import org.onap.msb.sdk.discovery.entity.NodeInfo;
25 import org.onap.msb.sdk.httpclient.msb.MSBServiceClient;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamTokenProvider;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
28 import org.springframework.core.env.Environment;
30 import java.util.HashSet;
33 import static junit.framework.TestCase.assertEquals;
34 import static junit.framework.TestCase.fail;
35 import static org.mockito.Mockito.verify;
36 import static org.mockito.Mockito.when;
37 import static org.springframework.test.util.ReflectionTestUtils.setField;
39 public class TestMsbApiProvider extends TestBase {
41 private Environment environment;
43 private CbamTokenProvider cbamTokenProvider;
44 private MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
45 private Set<NodeInfo> nodes = new HashSet<>();
46 private MsbApiProvider msbApiProvider;
50 setField(MsbApiProvider.class, "logger", logger);
51 msbApiProvider = new MsbApiProvider(environment);
52 microServiceInfo.setNodes(nodes);
56 * test MSB client is created based on driver properties
59 public void testMsbClient() {
60 setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusIp}", "mymessageBusIp");
61 setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusPort}", "123");
63 MSBServiceClient msbClient = msbApiProvider.getMsbClient();
65 assertEquals("mymessageBusIp:123", msbClient.getMsbSvrAddress());
69 * error is propagated if no suitable micro service endpoint is found
72 public void testNoSuitableMicroService() throws Exception {
73 NodeInfo dockerAccessPoint = new NodeInfo();
74 dockerAccessPoint.setIp("172.1.2.3");
75 microServiceInfo.setServiceName("serviceName");
76 microServiceInfo.setVersion("v1");
77 microServiceInfo.setUrl("/lead/nslcm/v1");
78 when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("");
79 nodes.add(dockerAccessPoint);
80 msbApiProvider = new MsbApiProvider(environment) {
82 public MSBServiceClient getMsbClient() {
86 when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenReturn(microServiceInfo);
89 msbApiProvider.getMicroServiceUrl("serviceName", "v1");
91 } catch (Exception e) {
92 assertEquals("The serviceName service with v1 does not have any valid nodes[172.1.2.3:null ttl:]", e.getMessage());
93 verify(logger).error("The serviceName service with v1 does not have any valid nodes[172.1.2.3:null ttl:]");
98 * non Docker endpoint is selected
101 public void testExistingValidEndpoint() throws Exception {
102 NodeInfo nonDocker = new NodeInfo();
103 nonDocker.setIp("173.1.2.3");
104 nonDocker.setPort("234");
105 microServiceInfo.setServiceName("serviceName");
106 microServiceInfo.setVersion("v1");
107 microServiceInfo.setUrl("/lead/nslcm/v1");
108 when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("173.1.2.3->1.2.3.4");
109 nodes.add(nonDocker);
110 msbApiProvider = new MsbApiProvider(environment) {
112 public MSBServiceClient getMsbClient() {
116 when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenReturn(microServiceInfo);
117 msbApiProvider.afterPropertiesSet();
119 assertEquals("http://1.2.3.4:234/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1"));
124 * use HTTPS for known ports (443) should be removed if https://jira.onap.org/browse/MSB-151 is solved
127 public void testMsb151IssueHack() throws Exception {
128 NodeInfo nonDocker = new NodeInfo();
129 nonDocker.setIp("173.1.2.3");
130 nonDocker.setPort("443");
131 microServiceInfo.setServiceName("serviceName");
132 microServiceInfo.setVersion("v1");
133 microServiceInfo.setUrl("/lead/nslcm/v1");
134 when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("173.1.2.3->1.2.3.4");
135 nodes.add(nonDocker);
136 msbApiProvider = new MsbApiProvider(environment) {
138 public MSBServiceClient getMsbClient() {
142 when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenReturn(microServiceInfo);
143 msbApiProvider.afterPropertiesSet();
145 assertEquals("https://1.2.3.4:443/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1"));
149 * use HTTPS for known ports (443) should be removed if https://jira.onap.org/browse/MSB-151 is solved
152 public void testMsb151IssueHack2() throws Exception {
153 NodeInfo nonDocker = new NodeInfo();
154 nonDocker.setIp("173.1.2.3");
155 nonDocker.setPort("8443");
156 microServiceInfo.setServiceName("serviceName");
157 microServiceInfo.setVersion("v1");
158 microServiceInfo.setUrl("/lead/nslcm/v1");
159 when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("173.1.2.3->1.2.3.4");
160 nodes.add(nonDocker);
161 msbApiProvider = new MsbApiProvider(environment) {
163 public MSBServiceClient getMsbClient() {
167 when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenReturn(microServiceInfo);
168 msbApiProvider.afterPropertiesSet();
170 assertEquals("https://1.2.3.4:8443/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1"));
174 * if unable to get micro service info the error is propagated
177 public void testUnableQueryMicroserviInfo() throws Exception {
178 msbApiProvider = new MsbApiProvider(environment) {
180 public MSBServiceClient getMsbClient() {
184 RouteException expectedException = new RouteException();
185 when(msbClient.queryMicroServiceInfo("serviceName", "v1")).thenThrow(expectedException);
189 msbApiProvider.getMicroServiceUrl("serviceName", "v1");
191 } catch (Exception e) {
192 assertEquals("Unable to get micro service URL for serviceName with version v1", e.getMessage());
193 verify(logger).error("Unable to get micro service URL for serviceName with version v1", expectedException);