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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.vnfmadapter.rest;
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;
56 * @author gareth.roper@est.tech
58 @RunWith(SpringRunner.class)
59 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
60 @ActiveProfiles("test")
61 public class Sol003PackageManagementControllerTest {
63 private static final Logger logger = LoggerFactory.getLogger(Sol003PackageManagementControllerTest.class);
69 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
70 private RestTemplate testRestTemplate;
73 private Sol003PackageManagementController controller;
76 private TestRestTemplate restTemplate;
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";
94 private MockRestServiceServer mockRestServer;
95 private BasicHttpHeadersProvider basicHttpHeadersProvider;
96 private final Gson gson = new Gson();
98 public Sol003PackageManagementControllerTest() {}
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();
110 public void testGetPackageContent_ValidArray_Success() {
111 final byte[] responseArray = buildByteArrayWithRandomData(10);
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));
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);
123 assertEquals(byte[].class, responseEntity.getBody().getClass());
124 assertArrayEquals(responseEntity.getBody(), responseArray);
125 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
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));
133 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
135 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
136 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
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));
144 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
146 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
147 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
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);
159 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
160 assertEquals(HttpStatus.UNAUTHORIZED, responseEntity.getStatusCode());
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));
168 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
170 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
171 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
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));
179 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
181 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
182 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
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));
191 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
193 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
194 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
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());
202 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
204 assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
205 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
209 public void testGetPackageArtifact_ValidArray_Success() {
210 final byte[] responseArray = buildByteArrayWithRandomData(10);
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));
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);
222 assertEquals(byte[].class, responseEntity.getBody().getClass());
223 assertArrayEquals(responseEntity.getBody(), responseArray);
224 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
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));
232 final ResponseEntity<ProblemDetails> responseEntity =
233 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
235 assertNotNull(responseEntity.getBody());
236 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
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));
244 final ResponseEntity<ProblemDetails> responseEntity =
245 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
247 assertNotNull(responseEntity.getBody());
248 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
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);
260 assertNotNull(responseEntity.getBody());
261 assertEquals(HttpStatus.UNAUTHORIZED, responseEntity.getStatusCode());
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));
269 final ResponseEntity<ProblemDetails> responseEntity =
270 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
272 assertNotNull(responseEntity.getBody());
273 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
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));
281 final ResponseEntity<ProblemDetails> responseEntity =
282 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
284 assertNotNull(responseEntity.getBody());
285 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
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));
293 final ResponseEntity<ProblemDetails> responseEntity =
294 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
296 assertNotNull(responseEntity.getBody());
297 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
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());
305 final ResponseEntity<ProblemDetails> responseEntity =
306 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
308 assertNotNull(responseEntity.getBody());
309 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
313 public void testVnfPackagesReceivedAsInlineResponse2001ListIfGetVnfPackagesSuccessful() {
314 final VnfPkgInfo[] responses = createVnfPkgArray();
316 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
317 .andRespond(withSuccess(gson.toJson(responses), MediaType.APPLICATION_JSON));
319 final String testURL = localhostUrl + port + VNFPKGM_BASE_URL;
320 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
322 final ResponseEntity<InlineResponse2001[]> responseEntity = restTemplate.withBasicAuth("test", "test")
323 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001[].class);
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());
342 public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackagesIs400BadRequest() {
343 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
344 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
346 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
347 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
349 assertNotNull(responseEntity.getBody());
350 final ProblemDetails problemDetails = responseEntity.getBody();
351 assertEquals("Error: Bad Request Received", problemDetails.getDetail());
355 public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackagesIs404NotFound() {
356 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
357 .andRespond(withStatus(HttpStatus.NOT_FOUND));
359 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
360 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
362 assertNotNull(responseEntity.getBody());
363 final ProblemDetails problemDetails = responseEntity.getBody();
364 assertEquals("No Vnf Packages found", problemDetails.getDetail());
368 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturns500InternalServerError() {
369 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
370 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
372 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
373 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
375 assertNotNull(responseEntity.getBody());
376 final ProblemDetails problemDetails = responseEntity.getBody();
377 assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
381 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturnsANullPackage() {
382 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
384 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
385 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
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());
395 public void testVnfPackageReceivedAsInlineResponse2001IfGetVnfPackageByIdSuccessful() {
396 final VnfPkgInfo response = createVnfPkgInfo(VNF_PACKAGE_ID);
398 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
399 .andRespond(withSuccess(gson.toJson(response), MediaType.APPLICATION_JSON));
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);
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());
422 public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs400BadRequest() {
423 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
424 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
426 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
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());
435 public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs404NotFound() {
436 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
437 .andRespond(withStatus(HttpStatus.NOT_FOUND));
439 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
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());
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));
452 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
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());
461 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturnsANullPackage() {
462 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
463 .andRespond(withSuccess());
465 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
466 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
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());
475 // The below test method is here to improve code coverage and provide a foundation for writing future tests
477 public void testGetPackageVnfd_ValidArray_Success() {
478 final byte[] responseArray = buildByteArrayWithRandomData(10);
480 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
481 .andExpect(method(HttpMethod.GET))
482 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
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);
490 assertEquals(byte[].class, responseEntity.getBody().getClass());
491 assertArrayEquals(responseEntity.getBody(), responseArray);
492 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
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));
500 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
502 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
503 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
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));
511 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
513 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
514 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
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));
522 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
524 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
525 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
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));
533 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
535 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
536 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
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));
544 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
546 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
547 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
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));
555 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
557 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
558 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
562 public void testGetPackageVnfd_SuccessResponseFromServerWithNullPackage_Fail() {
563 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
564 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
566 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
568 assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
569 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
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
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);
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;
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());
609 private Checksum createVnfPkgChecksum() {
610 final Checksum checksum = new Checksum();
611 checksum.setAlgorithm(ALGORITHM);
612 checksum.setHash(HASH);
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;
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;
638 private VNFPKGMLinkSerializer createVNFPKGMLinkSerializerLinks() {
639 final UriLink uriLink = new UriLink().href(URI_HREF);
640 final VNFPKGMLinkSerializer vnfpkgmLinkSerializer = new VNFPKGMLinkSerializer().self(uriLink);
641 return vnfpkgmLinkSerializer;