Removing jackson to mitigate cve-2017-4995
[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.onap.msb.ApiClient;
26 import org.onap.msb.api.ServiceResourceApi;
27 import org.onap.msb.model.MicroServiceFullInfo;
28 import org.onap.msb.model.NodeInfo;
29 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.CbamTokenProvider;
30 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm.TestBase;
31 import org.springframework.core.env.Environment;
32
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;
38
39 public class TestMsbApiProvider extends TestBase {
40     @Mock
41     private Environment environment;
42     @Mock
43     private CbamTokenProvider cbamTokenProvider;
44     private MicroServiceFullInfo microServiceInfo = new MicroServiceFullInfo();
45     private List<NodeInfo> nodes = new ArrayList<>();
46     private MsbApiProvider msbApiProvider;
47
48     @Before
49     public void init() {
50         setField(MsbApiProvider.class, "logger", logger);
51         msbApiProvider = new MsbApiProvider(environment);
52         microServiceInfo.setNodes(nodes);
53     }
54
55     /**
56      * test MSB client is created based on driver properties
57      */
58     @Test
59     public void testMsbClient() {
60         setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusIp}", "mymessageBusIp");
61         setFieldWithPropertyAnnotation(msbApiProvider, "${messageBusPort}", "123");
62         //when
63         ApiClient msbClient = msbApiProvider.buildApiClient();
64         //verify
65         assertEquals("http://mymessagebusip:123/api/msdiscover/v1/", msbClient.getAdapterBuilder().build().baseUrl().toString());
66     }
67
68     /**
69      * error is propagated if no suitable micro service endpoint is found
70      */
71     @Test
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) {
81             @Override
82             public ServiceResourceApi getMsbApi() {
83                 return msbClient;
84             }
85         };
86         when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
87         //when
88         try {
89             msbApiProvider.getMicroServiceUrl("serviceName", "v1");
90             fail();
91         } catch (Exception e) {
92             String msg = "The serviceName service with v1 does not have any valid nodes[class NodeInfo {\n" +
93                     "    ip: 172.1.2.3\n" +
94                     "    port: null\n" +
95                     "    lbServerParams: null\n" +
96                     "    checkType: null\n" +
97                     "    checkUrl: null\n" +
98                     "    checkInterval: null\n" +
99                     "    checkTimeOut: null\n" +
100                     "    ttl: null\n" +
101                     "    haRole: null\n" +
102                     "    nodeId: null\n" +
103                     "    status: null\n" +
104                     "    expiration: null\n" +
105                     "    createdAt: null\n" +
106                     "    updatedAt: null\n" +
107                     "}]";
108             assertEquals(msg, e.getMessage());
109             verify(logger).error(msg);
110         }
111     }
112
113     /**
114      * non Docker endpoint is selected
115      */
116     @Test
117     public void testExistingValidEndpoint() throws Exception {
118         NodeInfo nonDocker = new NodeInfo();
119         nonDocker.setIp("173.1.2.3");
120         nonDocker.setPort("234");
121         microServiceInfo.setServiceName("serviceName");
122         microServiceInfo.setVersion("v1");
123         microServiceInfo.setUrl("/lead/nslcm/v1");
124         when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("173.1.2.3->1.2.3.4");
125         nodes.add(nonDocker);
126         msbApiProvider = new MsbApiProvider(environment) {
127             @Override
128             public ServiceResourceApi getMsbApi() {
129                 return msbClient;
130             }
131         };
132         when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
133         msbApiProvider.afterPropertiesSet();
134         //when
135         assertEquals("http://1.2.3.4:234/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1"));
136     }
137
138
139     /**
140      * use HTTPS for known ports
141      */
142     @Test
143     public void testMsbServiceOverssl() throws Exception {
144         NodeInfo nonDocker = new NodeInfo();
145         nonDocker.setIp("173.1.2.3");
146         nonDocker.setPort("123");
147         microServiceInfo.setServiceName("serviceName");
148         microServiceInfo.setVersion("v1");
149         microServiceInfo.setUrl("/lead/nslcm/v1");
150         microServiceInfo.setEnableSsl(true);
151         when(environment.getProperty(IpMappingProvider.IP_MAP, String.class, "")).thenReturn("173.1.2.3->1.2.3.4");
152         nodes.add(nonDocker);
153         msbApiProvider = new MsbApiProvider(environment) {
154             @Override
155             public ServiceResourceApi getMsbApi() {
156                 return msbClient;
157             }
158         };
159         when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(buildObservable(microServiceInfo));
160         msbApiProvider.afterPropertiesSet();
161         //when
162         assertEquals("https://1.2.3.4:123/lead/nslcm/v1", msbApiProvider.getMicroServiceUrl("serviceName", "v1"));
163     }
164
165     /**
166      * if unable to get micro service info the error is propagated
167      */
168     @Test
169     public void testUnableQueryMicroserviInfo() throws Exception {
170         msbApiProvider = new MsbApiProvider(environment) {
171             @Override
172             public ServiceResourceApi getMsbApi() {
173                 return msbClient;
174             }
175         };
176         RuntimeException expectedException = new RuntimeException();
177         when(msbClient.getMicroService_0("serviceName", "v1", null, null, null, null, null)).thenReturn(Observable.error(expectedException));
178         //when
179         try {
180             msbApiProvider.getMicroServiceUrl("serviceName", "v1");
181             fail();
182         } catch (Exception e) {
183             assertEquals("Unable to get micro service URL for serviceName with version v1", e.getMessage());
184             verify(logger).error("Unable to get micro service URL for serviceName with version v1", expectedException);
185         }
186     }
187
188 }