Fix for SO-2678
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / test / java / org / onap / so / adapters / vnfmadapter / rest / Sol003PackageManagementControllerTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2019 Nordix Foundation.
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  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vnfmadapter.rest;
22
23 import static org.junit.Assert.assertArrayEquals;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.onap.so.adapters.vnfmadapter.Constants.PACKAGE_MANAGEMENT_BASE_URL;
28 import static org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.EtsiCatalogServiceProviderConfiguration.ETSI_CATALOG_REST_TEMPLATE_BEAN;
29 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
30 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
31 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
32 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Random;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
40 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.Checksum;
41 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.ProblemDetails;
42 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.UriLink;
43 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VNFPKGMLinkSerializer;
44 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfPackageArtifactInfo;
45 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfPackageSoftwareImageInfo;
46 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfPkgInfo;
47 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2001;
48 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.VnfPackagesLinks;
49 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.beans.factory.annotation.Qualifier;
52 import org.springframework.boot.test.context.SpringBootTest;
53 import org.springframework.boot.test.web.client.TestRestTemplate;
54 import org.springframework.boot.web.server.LocalServerPort;
55 import org.springframework.http.HttpEntity;
56 import org.springframework.http.HttpMethod;
57 import org.springframework.http.HttpStatus;
58 import org.springframework.http.MediaType;
59 import org.springframework.http.ResponseEntity;
60 import org.springframework.test.context.ActiveProfiles;
61 import org.springframework.test.context.junit4.SpringRunner;
62 import org.springframework.test.web.client.MockRestServiceServer;
63 import org.springframework.web.client.RestTemplate;
64 import com.google.gson.Gson;
65
66 /**
67  * @author gareth.roper@est.tech
68  */
69 @RunWith(SpringRunner.class)
70 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
71 @ActiveProfiles("test")
72 public class Sol003PackageManagementControllerTest {
73
74     @LocalServerPort
75     private int port;
76
77     @Autowired
78     @Qualifier(ETSI_CATALOG_REST_TEMPLATE_BEAN)
79     private RestTemplate restTemplate;
80
81     @Autowired
82     private TestRestTemplate testRestTemplate;
83
84     private static final String VNF_PACKAGE_ID = "myVnfPackageId";
85     private static final String ARTIFACT_PATH = "myArtifactPath";
86     private static final String MSB_BASE_URL = "http://msb-iag.onap:80/api/vnfpkgm/v1/vnf_packages";
87     private static final String VNFPKGM_BASE_URL = PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages";
88     private static final String localhostUrl = "http://localhost:";
89     private static final String GET_VNF_PACKAGES_URL = "";
90     private static final String GET_VNF_PACKAGE_BY_ID_URL = "/" + VNF_PACKAGE_ID;
91     private static final String VNFD_ID = "vnfdId";
92     private static final String VNF_PROVIDER = "vnfProvider";
93     private static final String VNF_PRODUCT_NAME = "vnfProductName";
94     private static final String VNF_SOFTWARE_VERSION = "vnfSoftwareVersion";
95     private static final String VNFD_VERSION = "vnfdVersion";
96     private static final String ALGORITHM = "algorithm";
97     private static final String HASH = "hash";
98     private static final String EXPECTED_BASE_URL =
99             "https://so-vnfm-adapter.onap:30406/so/vnfm-adapter/v1/vnfpkgm/v1/vnf_packages/";
100     private static final String EXPECTED_SELF_HREF = EXPECTED_BASE_URL + VNF_PACKAGE_ID;
101     private static final String EXPECTED_VNFD_HREF = EXPECTED_BASE_URL + VNF_PACKAGE_ID + "/vnfd";
102     private static final String EXPECTED_PACKAGE_CONTENT_HREF = EXPECTED_BASE_URL + VNF_PACKAGE_ID + "/package_content";
103
104     private MockRestServiceServer mockRestServer;
105     private BasicHttpHeadersProvider basicHttpHeadersProvider;
106     private final Gson gson = new Gson();
107
108     public Sol003PackageManagementControllerTest() {}
109
110     @Before
111     public void setUp() {
112         final MockRestServiceServer.MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(restTemplate);
113         builder.ignoreExpectOrder(true);
114         mockRestServer = builder.build();
115         basicHttpHeadersProvider = new BasicHttpHeadersProvider();
116     }
117
118     @Test
119     public void testGetPackageContent_ValidArray_Success() {
120         final byte[] responseArray = buildByteArrayWithRandomData(10);
121
122         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
123                 .andExpect(method(HttpMethod.GET))
124                 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
125
126         final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
127                 + VNF_PACKAGE_ID + "/package_content";
128         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
129         final ResponseEntity<byte[]> responseEntity =
130                 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
131
132         assertEquals(byte[].class, responseEntity.getBody().getClass());
133         assertArrayEquals(responseEntity.getBody(), responseArray);
134         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
135     }
136
137     @Test
138     public void testOnGetPackageContent_Conflict_Fail() {
139         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
140                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
141
142         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
143
144         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
145         assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
146     }
147
148     @Test
149     public void testOnGetPackageContent_NotFound_Fail() {
150         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
151                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
152
153         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
154
155         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
156         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
157     }
158
159     @Test
160     public void testOnGetPackageContent_UnauthorizedClient_Fail() {
161         final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
162                 + VNF_PACKAGE_ID + "/package_content";
163
164         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
165                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
166
167         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
168
169
170         final ResponseEntity<ProblemDetails> responseEntity =
171                 testRestTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
172
173         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
174         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
175     }
176
177     @Test
178     public void testOnGetPackageContent_InternalServerError_Fail() {
179         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
180                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
181
182         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
183
184         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
185         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
186     }
187
188     @Test
189     public void testOnGetPackageContent_BadRequest_Fail() {
190         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
191                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
192
193         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
194
195         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
196         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
197     }
198
199     @Test
200     public void testOnGetPackageContent_UnauthorizedServer_InternalError_Fail() {
201         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
202                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
203
204         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
205
206         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
207         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
208     }
209
210     @Test
211     public void testGetPackageContent_SuccessResponseFromServerWithNullPackage_Fail() {
212         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
213                 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
214
215         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
216
217         assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
218         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
219     }
220
221     @Test
222     public void testGetPackageArtifact_ValidArray_Success() {
223         final byte[] responseArray = buildByteArrayWithRandomData(10);
224
225         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
226                 .andExpect(method(HttpMethod.GET))
227                 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
228
229         final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
230                 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
231         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
232         final ResponseEntity<byte[]> responseEntity =
233                 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
234
235         assertEquals(byte[].class, responseEntity.getBody().getClass());
236         assertArrayEquals(responseEntity.getBody(), responseArray);
237         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
238     }
239
240     @Test
241     public void testOnGetPackageArtifact_Conflict_Fail() {
242         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
243                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
244
245         final ResponseEntity<ProblemDetails> responseEntity =
246                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
247
248         assertNotNull(responseEntity.getBody());
249         assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
250     }
251
252     @Test
253     public void testOnGetPackageArtifact_NotFound_Fail() {
254         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
255                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
256
257         final ResponseEntity<ProblemDetails> responseEntity =
258                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
259
260         assertNotNull(responseEntity.getBody());
261         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
262     }
263
264     @Test
265     public void testOnGetPackageArtifact_UnauthorizedClient_Fail() {
266         final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
267                 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
268
269         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
270                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
271
272         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
273         final ResponseEntity<ProblemDetails> responseEntity =
274                 testRestTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
275
276         assertNotNull(responseEntity.getBody());
277         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
278     }
279
280     @Test
281     public void testOnGetPackageArtifact_InternalServerError_Fail() {
282         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
283                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
284
285         final ResponseEntity<ProblemDetails> responseEntity =
286                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
287
288         assertNotNull(responseEntity.getBody());
289         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
290     }
291
292     @Test
293     public void testOnGetPackageArtifact_BadRequest_Fail() {
294         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
295                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
296
297         final ResponseEntity<ProblemDetails> responseEntity =
298                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
299
300         assertNotNull(responseEntity.getBody());
301         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
302     }
303
304     @Test
305     public void testOnGetPackageArtifact_UnauthorizedServer_InternalError_Fail() {
306         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
307                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
308
309         final ResponseEntity<ProblemDetails> responseEntity =
310                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
311
312         assertNotNull(responseEntity.getBody());
313         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
314     }
315
316     @Test
317     public void testGetPackageArtifact_SuccessResponseFromServerWithNullPackage_Fail() {
318         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
319                 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
320
321         final ResponseEntity<ProblemDetails> responseEntity =
322                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
323
324         assertNotNull(responseEntity.getBody());
325         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
326     }
327
328     @Test
329     public void testVnfPackagesReceivedAsInlineResponse2001ListIfGetVnfPackagesSuccessful() {
330         final VnfPkgInfo[] responses = createVnfPkgArray();
331
332         mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
333                 .andRespond(withSuccess(gson.toJson(responses), MediaType.APPLICATION_JSON));
334
335         final String testURL = localhostUrl + port + VNFPKGM_BASE_URL;
336         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
337
338         final ResponseEntity<InlineResponse2001[]> responseEntity = testRestTemplate.withBasicAuth("test", "test")
339                 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001[].class);
340
341         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
342         assertNotNull(responseEntity.getBody());
343         final InlineResponse2001[] inlineResponse2001array = responseEntity.getBody();
344         final InlineResponse2001 inlineResponse2001 = inlineResponse2001array[0];
345         assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
346         assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
347         assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
348         assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
349         assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
350         assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
351         assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
352         assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
353         assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
354         final VnfPackagesLinks links = inlineResponse2001.getLinks();
355         assertNotNull(links);
356         assertEquals(EXPECTED_SELF_HREF, links.getSelf().getHref());
357         assertEquals(EXPECTED_VNFD_HREF, links.getVnfd().getHref());
358         assertEquals(EXPECTED_PACKAGE_CONTENT_HREF, links.getPackageContent().getHref());
359     }
360
361     @Test
362     public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackagesIs400BadRequest() {
363         mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
364                 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
365
366         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
367         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
368
369         assertNotNull(responseEntity.getBody());
370         final ProblemDetails problemDetails = responseEntity.getBody();
371         assertEquals("Error: Bad Request Received", problemDetails.getDetail());
372     }
373
374     @Test
375     public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackagesIs404NotFound() {
376         mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
377                 .andRespond(withStatus(HttpStatus.NOT_FOUND));
378
379         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
380         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
381
382         assertNotNull(responseEntity.getBody());
383         final ProblemDetails problemDetails = responseEntity.getBody();
384         assertEquals("No Vnf Packages found", problemDetails.getDetail());
385     }
386
387     @Test
388     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturns500InternalServerError() {
389         mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
390                 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
391
392         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
393         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
394
395         assertNotNull(responseEntity.getBody());
396         final ProblemDetails problemDetails = responseEntity.getBody();
397         assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
398     }
399
400     @Test
401     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturnsANullPackage() {
402         mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
403
404         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
405         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
406
407         assertNotNull(responseEntity.getBody());
408         final ProblemDetails problemDetails = responseEntity.getBody();
409         assertEquals("An error occurred, a null response was received by the\n"
410                 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" \n"
411                 + "endpoint.", problemDetails.getDetail());
412     }
413
414     @Test
415     public void testVnfPackageReceivedAsInlineResponse2001IfGetVnfPackageByIdSuccessful() {
416         final VnfPkgInfo response = createVnfPkgInfo(VNF_PACKAGE_ID);
417
418         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
419                 .andRespond(withSuccess(gson.toJson(response), MediaType.APPLICATION_JSON));
420
421         final String testURL = localhostUrl + port + VNFPKGM_BASE_URL + "/" + VNF_PACKAGE_ID;
422         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
423         final ResponseEntity<InlineResponse2001> responseEntity = testRestTemplate.withBasicAuth("test", "test")
424                 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001.class);
425
426         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
427         assertNotNull(responseEntity.getBody());
428         final InlineResponse2001 inlineResponse2001 = responseEntity.getBody();
429         assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
430         assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
431         assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
432         assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
433         assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
434         assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
435         assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
436         assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
437         assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
438         final VnfPackagesLinks links = inlineResponse2001.getLinks();
439         assertNotNull(links);
440         assertEquals(EXPECTED_SELF_HREF, links.getSelf().getHref());
441         assertEquals(EXPECTED_VNFD_HREF, links.getVnfd().getHref());
442         assertEquals(EXPECTED_PACKAGE_CONTENT_HREF, links.getPackageContent().getHref());
443
444     }
445
446     @Test
447     public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs400BadRequest() {
448         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
449                 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
450
451         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
452
453         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
454         assertNotNull(responseEntity.getBody());
455         final ProblemDetails problemDetails = responseEntity.getBody();
456         assertEquals("Error: Bad Request Received", problemDetails.getDetail());
457     }
458
459     @Test
460     public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs404NotFound() {
461         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
462                 .andRespond(withStatus(HttpStatus.NOT_FOUND));
463
464         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
465
466         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
467         assertNotNull(responseEntity.getBody());
468         final ProblemDetails problemDetails = responseEntity.getBody();
469         assertEquals("No Vnf Package found with vnfPkgId: " + VNF_PACKAGE_ID, problemDetails.getDetail());
470     }
471
472     @Test
473     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturns500InternalServerError() {
474         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
475                 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
476
477         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
478
479         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
480         assertNotNull(responseEntity.getBody());
481         final ProblemDetails problemDetails = responseEntity.getBody();
482         assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
483     }
484
485     @Test
486     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturnsANullPackage() {
487         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
488                 .andRespond(withSuccess());
489
490         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
491         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
492
493         assertNotNull(responseEntity.getBody());
494         final ProblemDetails problemDetails = responseEntity.getBody();
495         assertEquals("An error occurred, a null response was received by the\n"
496                 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" by vnfPkgId: \""
497                 + VNF_PACKAGE_ID + "\" \n" + "endpoint.", problemDetails.getDetail());
498     }
499
500     // The below test method is here to improve code coverage and provide a foundation for writing
501     // future tests
502     @Test
503     public void testGetPackageVnfd_ValidArray_Success() {
504         final byte[] responseArray = buildByteArrayWithRandomData(10);
505
506         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
507                 .andExpect(method(HttpMethod.GET))
508                 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
509
510         final String testURL =
511                 "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/" + VNF_PACKAGE_ID + "/vnfd";
512         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
513         final ResponseEntity<byte[]> responseEntity =
514                 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
515
516         assertEquals(byte[].class, responseEntity.getBody().getClass());
517         assertArrayEquals(responseEntity.getBody(), responseArray);
518         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
519     }
520
521     @Test
522     public void testOnGetPackageVnfd_Conflict_Fail() {
523         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
524                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
525
526         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
527
528         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
529         assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
530     }
531
532     @Test
533     public void testOnGetPackageVnfd_NotFound_Fail() {
534         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
535                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
536
537         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
538
539         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
540         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
541     }
542
543     @Test
544     public void testOnGetPackageVnfd_UnauthorizedClient_Fail() {
545         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
546                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
547
548         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
549
550         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
551         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
552     }
553
554     @Test
555     public void testOnGetPackageVnfd_InternalServerError_Fail() {
556         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
557                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
558
559         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
560
561         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
562         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
563     }
564
565     @Test
566     public void testOnGetPackageVnfd_BadRequest_Fail() {
567         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
568                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
569
570         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
571
572         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
573         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
574     }
575
576     @Test
577     public void testOnGetPackageVnfd_UnauthorizedServer_InternalError_Fail() {
578         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
579                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
580
581         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
582
583         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
584         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
585     }
586
587     @Test
588     public void testGetPackageVnfd_SuccessResponseFromServerWithNullPackage_Fail() {
589         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
590                 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
591
592         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
593
594         assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
595         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
596     }
597
598     // Simply returns a byte array filled with random data, for use in the tests.
599     private byte[] buildByteArrayWithRandomData(final int sizeInKb) {
600         final Random rnd = new Random();
601         final byte[] b = new byte[sizeInKb * 1024]; // converting kb to byte
602         rnd.nextBytes(b);
603         return b;
604     }
605
606     private ResponseEntity<ProblemDetails> sendHttpRequest(final String url) {
607         final String testURL = localhostUrl + port + VNFPKGM_BASE_URL + "/" + url;
608         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
609         return testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request,
610                 ProblemDetails.class);
611     }
612
613     private VnfPkgInfo[] createVnfPkgArray() {
614         final VnfPkgInfo[] vnfPkgInfoArray = new VnfPkgInfo[1];
615         final VnfPkgInfo vnfPkgInfo = createVnfPkgInfo(VNF_PACKAGE_ID);
616         vnfPkgInfoArray[0] = vnfPkgInfo;
617         return vnfPkgInfoArray;
618     }
619
620     private VnfPkgInfo createVnfPkgInfo(final String vnfPackageId) {
621         final VnfPkgInfo vnfPkgInfo = new VnfPkgInfo();
622         vnfPkgInfo.setId(vnfPackageId);
623         vnfPkgInfo.setVnfdId(VNFD_ID);
624         vnfPkgInfo.setVnfProvider(VNF_PROVIDER);
625         vnfPkgInfo.setVnfProductName(VNF_PRODUCT_NAME);
626         vnfPkgInfo.setVnfSoftwareVersion(VNF_SOFTWARE_VERSION);
627         vnfPkgInfo.setVnfdVersion(VNFD_VERSION);
628         vnfPkgInfo.setChecksum(createVnfPkgChecksum());
629         vnfPkgInfo.setSoftwareImages(createSoftwareImages());
630         vnfPkgInfo.setAdditionalArtifacts(createAdditionalArtifacts());
631         vnfPkgInfo.setLinks(createVNFPKGMLinkSerializerLinks());
632         return vnfPkgInfo;
633     }
634
635     private Checksum createVnfPkgChecksum() {
636         final Checksum checksum = new Checksum();
637         checksum.setAlgorithm(ALGORITHM);
638         checksum.setHash(HASH);
639         return checksum;
640     }
641
642     private List<VnfPackageSoftwareImageInfo> createSoftwareImages() {
643         final List<VnfPackageSoftwareImageInfo> softwareImages = new ArrayList<>();
644         final VnfPackageSoftwareImageInfo vnfPackageSoftwareImageInfo = new VnfPackageSoftwareImageInfo();
645         vnfPackageSoftwareImageInfo.setId(VNFD_ID);
646         vnfPackageSoftwareImageInfo.setName(VNF_PRODUCT_NAME);
647         vnfPackageSoftwareImageInfo.setProvider("");
648         vnfPackageSoftwareImageInfo.setVersion("");
649         vnfPackageSoftwareImageInfo.setChecksum(createVnfPkgChecksum());
650         vnfPackageSoftwareImageInfo
651                 .setContainerFormat(VnfPackageSoftwareImageInfo.ContainerFormatEnum.fromValue("AKI"));
652         softwareImages.add(vnfPackageSoftwareImageInfo);
653         return softwareImages;
654     }
655
656     private List<VnfPackageArtifactInfo> createAdditionalArtifacts() {
657         final List<VnfPackageArtifactInfo> vnfPackageArtifactInfos = new ArrayList<>();
658         final VnfPackageArtifactInfo vnfPackageArtifactInfo =
659                 new VnfPackageArtifactInfo().artifactPath(ARTIFACT_PATH).checksum(createVnfPkgChecksum());
660         vnfPackageArtifactInfos.add(vnfPackageArtifactInfo);
661         return vnfPackageArtifactInfos;
662     }
663
664     private VNFPKGMLinkSerializer createVNFPKGMLinkSerializerLinks() {
665         final String baseUrl = "http://msb-iag:443/api/vnfpkgm/v1/vnf_packages";
666         return new VNFPKGMLinkSerializer().self(new UriLink().href(baseUrl + "/myVnfPackageId"))
667                 .vnfd(new UriLink().href(baseUrl + "/myVnfPackageId/vnfd"))
668                 .packageContent(new UriLink().href(baseUrl + "/myVnfPackageId/package_content"));
669     }
670
671 }