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