Fix sonar issues
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / test / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / onap / core / TestMsbApiProvider.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16
17 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.onap.core;
18
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;
33
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;
39
40 public class TestMsbApiProvider extends TestBase {
41     @Mock
42     private Environment environment;
43     @Mock
44     private CbamTokenProvider cbamTokenProvider;
45     private MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
46     private List<NodeInfo> nodes = new ArrayList<>();
47     private MsbApiProvider msbApiProvider;
48
49     @Before
50     public void init() {
51         setField(MsbApiProvider.class, "logger", logger);
52         msbApiProvider = new MsbApiProvider(environment);
53         microServiceInfo.setNodes(nodes);
54     }
55
56     /**
57      * test MSB client is created based on driver properties
58      */
59     @Test
60     public void testMsbClient() {
61         setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusIp}", "mymessageBusIp");
62         setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusPort}", "123");
63         //when
64         ApiClient msbClient = msbApiProvider.buildApiClient();
65         //verify
66         assertEquals("http://mymessagebusip:123/api/msdiscover/v1/", msbClient.getAdapterBuilder().build().baseUrl().toString());
67     }
68
69     /**
70      * error is propagated if no suitable micro service endpoint is found
71      */
72     @Test
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) {
82             @Override
83             public ServiceResourceApi getMsbApi() {
84                 return msbClient;
85             }
86         };
87         when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
88         //when
89         try {
90             msbApiProvider.getMicroServiceUrl("serviceName", "v1");
91             fail();
92         } catch (Exception e) {
93             String msg = "The serviceName service with v1 does not have any valid nodes[class NodeInfo {\n" +
94                     "    ip: 172.1.2.3\n" +
95                     "    port: null\n" +
96                     "    lbServerParams: null\n" +
97                     "    checkType: null\n" +
98                     "    checkUrl: null\n" +
99                     "    checkInterval: null\n" +
100                     "    checkTimeOut: null\n" +
101                     "    ttl: null\n" +
102                     "    haRole: null\n" +
103                     "    nodeId: null\n" +
104                     "    status: null\n" +
105                     "    expiration: null\n" +
106                     "    createdAt: null\n" +
107                     "    updatedAt: null\n" +
108                     "}]";
109             assertEquals(msg, e.getMessage());
110             verify(logger).error(msg);
111         }
112     }
113
114     /**
115      * non Docker endpoint is selected
116      */
117     @Test
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) {
128             @Override
129             public ServiceResourceApi getMsbApi() {
130                 return msbClient;
131             }
132         };
133         when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
134         msbApiProvider.afterPropertiesSet();
135         //when
136         assertEquals("http://1.2.3.4:234/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1"));
137     }
138
139
140     /**
141      * use HTTPS for known ports
142      */
143     @Test
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) {
155             @Override
156             public ServiceResourceApi getMsbApi() {
157                 return msbClient;
158             }
159         };
160         when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
161         msbApiProvider.afterPropertiesSet();
162         //when
163         assertEquals("https://1.2.3.4:123/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1"));
164     }
165
166     /**
167      * if unable to get micro service info the error is propagated
168      */
169     @Test
170     public void testUnableQueryMicroserviInfo() throws Exception {
171         msbApiProvider = new MsbApiProvider(environment) {
172             @Override
173             public ServiceResourceApi getMsbApi() {
174                 return msbClient;
175             }
176         };
177         RuntimeException expectedException = new RuntimeException();
178         when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(Observable.error(expectedException));
179         //when
180         try {
181             msbApiProvider.getMicroServiceUrl("serviceName", "v1");
182             fail();
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);
186         }
187     }
188
189     /**
190      * Test API wrapping for LCM
191      * (questionable benefit [ this is more less ensured by Java type safety) ]
192      */
193     @Test
194     public void testOperationExecutionsApiAPiWrapping() {
195         ApiClient c = Mockito.mock(ApiClient.class);
196         class TestClasss extends MsbApiProvider {
197
198             TestClasss(Environment environment) {
199                 super(environment);
200             }
201
202             @Override
203             ApiClient buildApiClient() {
204                 return c;
205             }
206         }
207         ServiceResourceApi defaultApi = Mockito.mock(ServiceResourceApi.class);
208         when(c.createService(ServiceResourceApi.class)).thenReturn(defaultApi);
209         //verify
210         TestClasss testInstnace = new TestClasss(environment);
211         assertEquals(defaultApi, testInstnace.getMsbApi());
212     }
213
214 }