0680842972d2f38ab1e426f95e5e68909413eeae
[sdnc/apps.git] /
1 /*
2  *  ============LICENSE_START===================================================
3  * Copyright (c) 2018 Amdocs
4  * ============================================================================
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  * ============LICENSE_END=====================================================
17  */
18
19 package org.onap.sdnc.apps.pomba.servicedecomposition.test;
20
21 import static com.github.tomakehurst.wiremock.client.WireMock.get;
22 import static com.github.tomakehurst.wiremock.client.WireMock.notFound;
23 import static com.github.tomakehurst.wiremock.client.WireMock.okJson;
24 import static com.github.tomakehurst.wiremock.core.WireMockConfiguration.wireMockConfig;
25 import static org.junit.Assert.assertEquals;
26 import static org.junit.Assert.assertNull;
27 import static org.junit.Assert.assertTrue;
28 import static org.junit.Assert.fail;
29
30 import com.github.tomakehurst.wiremock.junit.WireMockRule;
31 import java.io.IOException;
32 import java.io.InputStream;
33 import java.io.InputStreamReader;
34 import java.util.Base64;
35 import javax.servlet.http.HttpServletRequest;
36 import javax.ws.rs.core.HttpHeaders;
37 import javax.ws.rs.core.Response;
38 import javax.ws.rs.core.Response.Status;
39 import org.eclipse.jetty.util.security.Password;
40 import org.json.JSONArray;
41 import org.json.JSONObject;
42 import org.junit.Rule;
43 import org.junit.Test;
44 import org.junit.runner.RunWith;
45 import org.mockito.Mockito;
46 import org.onap.logging.ref.slf4j.ONAPLogConstants;
47 import org.onap.sdnc.apps.pomba.servicedecomposition.service.rs.RestService;
48 import org.springframework.beans.factory.annotation.Autowired;
49 import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
50 import org.springframework.boot.autoconfigure.jdbc.DataSourceAutoConfiguration;
51 import org.springframework.boot.autoconfigure.orm.jpa.HibernateJpaAutoConfiguration;
52 import org.springframework.boot.test.context.SpringBootTest;
53 import org.springframework.test.context.TestPropertySource;
54 import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
55 import org.springframework.test.context.web.WebAppConfiguration;
56
57 @RunWith(SpringJUnit4ClassRunner.class)
58 @EnableAutoConfiguration(exclude = { DataSourceAutoConfiguration.class, HibernateJpaAutoConfiguration.class })
59 @WebAppConfiguration
60 @SpringBootTest
61 @TestPropertySource(properties = {
62         "aai.httpProtocol=http",
63         "aai.serviceName=localhost",
64         "aai.servicePort=8081",
65         "basicAuth.username=admin",
66         "basicAuth.password=OBF:1u2a1toa1w8v1tok1u30"
67     })
68
69 public class ServiceDecompositionTest {
70
71     private static final String AUTH = "Basic " + Base64.getEncoder().encodeToString((
72             "admin:" + Password.deobfuscate("OBF:1u2a1toa1w8v1tok1u30")).getBytes());
73
74     private HttpServletRequest httpRequest = Mockito.mock(HttpServletRequest.class);
75
76     // TODO missing code coverage for VNFC resources
77
78     @Rule
79     public WireMockRule aai = new WireMockRule(wireMockConfig().port(8081));
80
81     @Autowired
82     private RestService service;
83
84     /**
85      * Simulation of the firewall demo service, using json captured from real A&AI queries.
86      * The resource relationship looks something like.
87      * <pre>
88      * service instance
89      * +- generic-vnf 1
90      * |  +- vserver
91      * +- generic-vnf 2
92      *    +- network 1
93      *    +- network 2
94      * </pre>
95      */
96     @Test
97     public void testDemoFirewallService() throws Exception {
98         // setup A&AI responses
99         addResponse(
100                 "/aai/v13/nodes/service-instance/c6456519-6acf-4adb-997c-3c363dd4caaf",
101                 "junit/aai-service-instance.json");
102         addResponse(
103                 "/aai/v13/network/generic-vnfs/generic-vnf/6700c313-fbb7-4cf9-ac70-0293ec56df68?depth=2",
104                 "junit/aai-generic-vnf1.json");
105         addResponse(
106                 "/aai/v13/network/generic-vnfs/generic-vnf/8a9ddb25-2e79-449c-a40d-5011bac0da39?depth=2",
107                 "junit/aai-generic-vnf2.json");
108         addResponse(
109                 "/aai/v13/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/RegionOne/tenants/tenant"
110                         + "/b49b830686654191bb1e952a74b014ad/vservers/vserver/b494cd6e-b9f3-45e0-afe7-e1d1a5f5d74a",
111                 "junit/aai-vserver.json");
112         addResponse(
113                 "/aai/v13/network/l3-networks/l3-network/HNP1d77c-1094-41ec-b7f3-94bb30951870",
114                 "junit/aai-l3-network1.json");
115         addResponse(
116                 "/aai/v13/network/l3-networks/l3-network/HNP1d77c-1094-41ec-b7f3-94bb30951872",
117                 "junit/aai-l3-network2.json");
118
119         final String serviceInstanceId = "c6456519-6acf-4adb-997c-3c363dd4caaf";
120         final String vnfId1 = "6700c313-fbb7-4cf9-ac70-0293ec56df68";
121         final String vnfId2 = "8a9ddb25-2e79-449c-a40d-5011bac0da39";
122         final String vserverId = "b494cd6e-b9f3-45e0-afe7-e1d1a5f5d74a";
123         final String networkId1 = "HNP1d77c-1094-41ec-b7f3-94bb30951870";
124         final String networkId2 = "HNP1d77c-1094-41ec-b7f3-94bb30951872";
125
126         Response response = this.service.getContext(httpRequest, AUTH, "network-discovery-context-builder", null,
127                 serviceInstanceId);
128         assertEquals(200, response.getStatus());
129
130         JSONObject serviceInstance = new JSONObject((String)response.getEntity());
131
132         // verify two generic-vnfs added to service instance data
133         verifyResource(serviceInstance, "generic-vnfs", "vnf-id", vnfId1);
134         verifyResource(serviceInstance, "generic-vnfs", "vnf-id", vnfId2);
135
136         JSONArray vnfs = serviceInstance.getJSONArray("generic-vnfs");
137         for (int i = 0; i < vnfs.length(); i++) {
138             JSONObject vnf = vnfs.getJSONObject(i);
139             String vnfId = vnf.getString("vnf-id");
140             switch (vnfId) {
141                 case vnfId1:
142                     // verify vserver resource
143                     verifyResource(vnf, "vservers", "vserver-id", vserverId);
144                     break;
145                 case vnfId2:
146                     // verify network resources
147                     verifyResource(vnf, "l3-networks", "network-id", networkId1);
148                     verifyResource(vnf, "l3-networks", "network-id", networkId2);
149                     break;
150                 default:
151                     fail("Unexpected generic-vnf " + vnfId);
152             }
153         }
154     }
155
156     @Test
157     public void testDemoFirewallServiceWithL3Networks() throws Exception {
158         // setup A&AI responses
159         addResponse(
160                 "/aai/v13/nodes/service-instance/c6456519-6acf-4adb-997c-3c363dd4caaf",
161                 "junit/aai-service-instance2.json");
162         addResponse(
163                 "/aai/v13/network/generic-vnfs/generic-vnf/6700c313-fbb7-4cf9-ac70-0293ec56df68?depth=2",
164                 "junit/aai-generic-vnf1.json");
165         addResponse(
166                 "/aai/v13/network/generic-vnfs/generic-vnf/8a9ddb25-2e79-449c-a40d-5011bac0da39?depth=2",
167                 "junit/aai-generic-vnf2.json");
168         addResponse(
169                 "/aai/v13/cloud-infrastructure/cloud-regions/cloud-region/CloudOwner/RegionOne/tenants/tenant"
170                         + "/b49b830686654191bb1e952a74b014ad/vservers/vserver/b494cd6e-b9f3-45e0-afe7-e1d1a5f5d74a",
171                 "junit/aai-vserver.json");
172         addResponse(
173                 "/aai/v13/network/l3-networks/l3-network/HNP1d77c-1094-41ec-b7f3-94bb30951870",
174                 "junit/aai-l3-network1.json");
175         addResponse(
176                 "/aai/v13/network/l3-networks/l3-network/HNP1d77c-1094-41ec-b7f3-94bb30951872",
177                 "junit/aai-l3-network2.json");
178
179         final String serviceInstanceId = "c6456519-6acf-4adb-997c-3c363dd4caaf";
180         final String vnfId1 = "6700c313-fbb7-4cf9-ac70-0293ec56df68";
181         final String vnfId2 = "8a9ddb25-2e79-449c-a40d-5011bac0da39";
182         final String vserverId = "b494cd6e-b9f3-45e0-afe7-e1d1a5f5d74a";
183         final String networkId1 = "HNP1d77c-1094-41ec-b7f3-94bb30951870";
184         final String networkId2 = "HNP1d77c-1094-41ec-b7f3-94bb30951872";
185
186         Response response = this.service.getContext(httpRequest, AUTH, "network-discovery-context-builder", null,
187                 serviceInstanceId);
188         assertEquals(200, response.getStatus());
189
190         JSONObject serviceInstance = new JSONObject((String)response.getEntity());
191
192         // verify two generic-vnfs added to service instance data
193         verifyResource(serviceInstance, "generic-vnfs", "vnf-id", vnfId1);
194         verifyResource(serviceInstance, "generic-vnfs", "vnf-id", vnfId2);
195         verifyResource(serviceInstance, "l3-networks", "network-id", networkId1);
196
197         JSONArray vnfs = serviceInstance.getJSONArray("generic-vnfs");
198         for (int i = 0; i < vnfs.length(); i++) {
199             JSONObject vnf = vnfs.getJSONObject(i);
200             String vnfId = vnf.getString("vnf-id");
201             switch (vnfId) {
202                 case vnfId1:
203                     // verify vserver resource
204                     verifyResource(vnf, "vservers", "vserver-id", vserverId);
205                     break;
206                 case vnfId2:
207                     // verify network resources
208                     verifyResource(vnf, "l3-networks", "network-id", networkId1);
209                     verifyResource(vnf, "l3-networks", "network-id", networkId2);
210                     JSONObject vfmodules = vnf.getJSONObject("vf-modules");
211                     JSONArray vfmoduleList = vfmodules.getJSONArray("vf-module");
212                     for (int j = 0; j < vfmoduleList.length(); j++) {
213                         JSONObject vfmodule = vfmoduleList.getJSONObject(j);
214                         verifyResource(vfmodule, "l3-networks", "network-id", networkId1);
215                     }
216                     break;
217                 default:
218                     fail("Unexpected generic-vnf " + vnfId);
219             }
220         }
221     }
222
223     @Test
224     public void testNoAuthHeader() throws Exception {
225         String fromAppId = "junit";
226         String transactionId = null;
227         String serviceInstanceId = "aServiceInstanceId";
228         // no Authorization header
229         Response response = this.service.getContext(httpRequest, null, fromAppId, transactionId,
230                 serviceInstanceId);
231         assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
232         // should get WWW-Authenticate header in response
233         assertTrue(response.getHeaderString(HttpHeaders.WWW_AUTHENTICATE).startsWith("Basic realm"));
234     }
235
236     @Test
237     public void testUnauthorized() throws Exception {
238         // bad credentials
239         String authorization = "Basic " + Base64.getEncoder().encodeToString("aaa:bbb".getBytes());
240         String fromAppId = "junit";
241         String transactionId = null;
242         String serviceInstanceId = "aServiceInstanceId";
243
244         Response response = this.service.getContext(httpRequest, authorization, fromAppId,
245                 transactionId, serviceInstanceId);
246         assertEquals(Status.UNAUTHORIZED.getStatusCode(), response.getStatus());
247         // should not get WWW-Authenticate header in response
248         assertNull(response.getHeaderString(HttpHeaders.WWW_AUTHENTICATE));
249     }
250
251     /** Fail if calling app name is missing. */
252     @Test
253     public void verifyFromAppId() throws Exception {
254         String fromAppId = null;
255         String transactionId = null;
256         String serviceInstanceId = "someValue";
257
258         Response response = this.service.getContext(httpRequest, AUTH, fromAppId, transactionId,
259                 serviceInstanceId);
260         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
261         assertTrue(((String)response.getEntity()).contains(ONAPLogConstants.Headers.PARTNER_NAME));
262     }
263
264     /** Fail if service instance id is missing. */
265     @Test
266     public void verifyServiceInstanceId() throws Exception {
267         String fromAppId = "junit";
268         String transactionId = null;
269         String serviceInstanceId = null;
270
271         Response response = this.service.getContext(httpRequest, AUTH, fromAppId, transactionId,
272                 serviceInstanceId);
273         assertEquals(Status.BAD_REQUEST.getStatusCode(), response.getStatus());
274         assertTrue(((String)response.getEntity()).contains("service-instance-id"));
275     }
276
277     /** Unknown service-instance-id return HTTP 404. */
278     @Test
279     public void testInvalidServiceId() throws Exception {
280         aai.stubFor(get("/aai/v13/nodes/service-instance/noSuchServiceId").willReturn(notFound()));
281
282         Response response =
283                 this.service.getContext(httpRequest, AUTH, "junit", null, "noSuchServiceId");
284
285         assertEquals(Status.NOT_FOUND.getStatusCode(), response.getStatus());
286     }
287
288     private void verifyResource(JSONObject parent, String arrayName, String field, String value) {
289         JSONArray array = parent.getJSONArray(arrayName);
290         for (int i = 0; i < array.length(); i++) {
291             JSONObject item = array.getJSONObject(i);
292             if (value.equals(item.getString(field))) {
293                 return;
294             }
295         }
296         fail("Did not find " + field + "=" + value + " in " + arrayName + " array");
297     }
298
299     private void addResponse(String path, String classpathResource) throws IOException {
300         String payload = readFully(ClassLoader.getSystemResourceAsStream(classpathResource));
301         aai.stubFor(get(path).willReturn(okJson(payload)));
302     }
303
304     private String readFully(InputStream in) throws IOException {
305         char[] cbuf = new char[1024];
306         StringBuilder content = new StringBuilder();
307         try (InputStreamReader reader = new InputStreamReader(in, "UTF-8")) {
308             int count;
309             while ((count = reader.read(cbuf)) >= 0) {
310                 content.append(cbuf, 0, count);
311             }
312         }
313         return content.toString();
314     }
315 }