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