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 io.reactivex.Observable;
20 import java.util.ArrayList;
21 import java.util.List;
22 import org.junit.Before;
23 import org.junit.Test;
24 import org.mockito.Mock;
25 import org.mockito.Mockito;
26 import org.onap.msb.ApiClient;
27 import org.onap.msb.api.ServiceResourceApi;
28 import org.onap.msb.model.MicroServiceFullInfo;
29 import org.onap.msb.model.NodeInfo;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamTokenProvider;
31 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
32 import org.springframework.core.env.Environment;
34 import static junit.framework.TestCase.assertEquals;
35 import static junit.framework.TestCase.fail;
36 import static org.mockito.Mockito.verify;
37 import static org.mockito.Mockito.when;
38 import static org.springframework.test.util.ReflectionTestUtils.setField;
40 public class TestMsbApiProvider extends TestBase {
42 private Environment environment;
44 private CbamTokenProvider cbamTokenProvider;
45 private MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
46 private List<NodeInfo> nodes = new ArrayList<>();
47 private MsbApiProvider msbApiProvider;
51 setField(MsbApiProvider.class, "logger", logger);
52 msbApiProvider = new MsbApiProvider(environment);
53 microServiceInfo.setNodes(nodes);
57 * test MSB client is created based on driver properties
60 public void testMsbClient() {
61 setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusIp}", "mymessageBusIp");
62 setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusPort}", "123");
64 ApiClient msbClient = msbApiProvider.buildApiClient();
66 assertEquals("http://mymessagebusip:123/api/msdiscover/v1/", msbClient.getAdapterBuilder().build().baseUrl().toString());
70 * error is propagated if no suitable micro service endpoint is found
73 public void testNoSuitableMicroService() throws Exception {
74 NodeInfo dockerAccessPoint = new NodeInfo();
75 dockerAccessPoint.setIp("172.1.2.3");
76 microServiceInfo.setServiceName("serviceName");
77 microServiceInfo.setVersion("v1");
78 microServiceInfo.setUrl("/lead/nslcm/v1");
79 when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("");
80 nodes.add(dockerAccessPoint);
81 msbApiProvider = new MsbApiProvider(environment) {
83 public ServiceResourceApi getMsbApi() {
87 when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
90 msbApiProvider.getMicroServiceUrl("serviceName", "v1");
92 } catch (Exception e) {
93 String msg = "The serviceName service with v1 does not have any valid nodes[class NodeInfo {\n" +
96 " lbServerParams: null\n" +
97 " checkType: null\n" +
99 " checkInterval: null\n" +
100 " checkTimeOut: null\n" +
105 " expiration: null\n" +
106 " createdAt: null\n" +
107 " updatedAt: null\n" +
109 assertEquals(msg, e.getMessage());
110 verify(logger).error(msg);
115 * non Docker endpoint is selected
118 public void testExistingValidEndpoint() throws Exception {
119 NodeInfo nonDocker = new NodeInfo();
120 nonDocker.setIp("173.1.2.3");
121 nonDocker.setPort("234");
122 microServiceInfo.setServiceName("serviceName");
123 microServiceInfo.setVersion("v1");
124 microServiceInfo.setUrl("/lead/nslcm/v1");
125 when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("173.1.2.3->1.2.3.4");
126 nodes.add(nonDocker);
127 msbApiProvider = new MsbApiProvider(environment) {
129 public ServiceResourceApi getMsbApi() {
133 when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
134 msbApiProvider.afterPropertiesSet();
136 assertEquals("http://1.2.3.4:234/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1"));
141 * use HTTPS for known ports
144 public void testMsbServiceOverssl() throws Exception {
145 NodeInfo nonDocker = new NodeInfo();
146 nonDocker.setIp("173.1.2.3");
147 nonDocker.setPort("123");
148 microServiceInfo.setServiceName("serviceName");
149 microServiceInfo.setVersion("v1");
150 microServiceInfo.setUrl("/lead/nslcm/v1");
151 microServiceInfo.setEnableSsl(true);
152 when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("173.1.2.3->1.2.3.4");
153 nodes.add(nonDocker);
154 msbApiProvider = new MsbApiProvider(environment) {
156 public ServiceResourceApi getMsbApi() {
160 when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
161 msbApiProvider.afterPropertiesSet();
163 assertEquals("https://1.2.3.4:123/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1"));
167 * if unable to get micro service info the error is propagated
170 public void testUnableQueryMicroserviInfo() throws Exception {
171 msbApiProvider = new MsbApiProvider(environment) {
173 public ServiceResourceApi getMsbApi() {
177 RuntimeException expectedException = new RuntimeException();
178 when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(Observable.error(expectedException));
181 msbApiProvider.getMicroServiceUrl("serviceName", "v1");
183 } catch (Exception e) {
184 assertEquals("Unable to get micro service URL for serviceName with version v1", e.getMessage());
185 verify(logger).error("Unable to get micro service URL for serviceName with version v1", expectedException);
190 * Test API wrapping for LCM
191 * (questionable benefit [ this is more less ensured by Java type safety) ]
194 public void testOperationExecutionsApiAPiWrapping() {
195 ApiClient c = Mockito.mock(ApiClient.class);
196 class TestClasss extends MsbApiProvider {
198 TestClasss(Environment environment) {
203 ApiClient buildApiClient() {
207 ServiceResourceApi defaultApi = Mockito.mock(ServiceResourceApi.class);
208 when(c.createService(ServiceResourceApi.class)).thenReturn(defaultApi);
210 TestClasss testInstnace = new TestClasss(environment);
211 assertEquals(defaultApi, testInstnace.getMsbApi());