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