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.assertArrayEquals;
24 import static org.junit.Assert.assertEquals;
25 import static org.junit.Assert.assertNotNull;
26 import static org.junit.Assert.assertTrue;
27 import static org.onap.so.adapters.vnfmadapter.Constants.PACKAGE_MANAGEMENT_BASE_URL;
28 import static org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.EtsiCatalogServiceProviderConfiguration.ETSI_CATALOG_REST_TEMPLATE_BEAN;
29 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
30 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
31 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
32 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Random;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
40 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.Checksum;
41 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.ProblemDetails;
42 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.UriLink;
43 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VNFPKGMLinkSerializer;
44 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfPackageArtifactInfo;
45 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfPackageSoftwareImageInfo;
46 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfPkgInfo;
47 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2001;
48 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.VnfPackagesLinks;
49 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
50 import org.springframework.beans.factory.annotation.Autowired;
51 import org.springframework.beans.factory.annotation.Qualifier;
52 import org.springframework.boot.test.context.SpringBootTest;
53 import org.springframework.boot.test.web.client.TestRestTemplate;
54 import org.springframework.boot.web.server.LocalServerPort;
55 import org.springframework.http.HttpEntity;
56 import org.springframework.http.HttpMethod;
57 import org.springframework.http.HttpStatus;
58 import org.springframework.http.MediaType;
59 import org.springframework.http.ResponseEntity;
60 import org.springframework.test.context.ActiveProfiles;
61 import org.springframework.test.context.junit4.SpringRunner;
62 import org.springframework.test.web.client.MockRestServiceServer;
63 import org.springframework.web.client.RestTemplate;
64 import com.google.gson.Gson;
67 * @author gareth.roper@est.tech
69 @RunWith(SpringRunner.class)
70 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
71 @ActiveProfiles("test")
72 public class Sol003PackageManagementControllerTest {
78 @Qualifier(ETSI_CATALOG_REST_TEMPLATE_BEAN)
79 private RestTemplate restTemplate;
82 private TestRestTemplate testRestTemplate;
84 private static final String VNF_PACKAGE_ID = "myVnfPackageId";
85 private static final String ARTIFACT_PATH = "myArtifactPath";
86 private static final String MSB_BASE_URL = "http://msb-iag.onap:80/api/vnfpkgm/v1/vnf_packages";
87 private static final String VNFPKGM_BASE_URL = PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages";
88 private static final String localhostUrl = "http://localhost:";
89 private static final String GET_VNF_PACKAGES_URL = "";
90 private static final String GET_VNF_PACKAGE_BY_ID_URL = "/" + VNF_PACKAGE_ID;
91 private static final String VNFD_ID = "vnfdId";
92 private static final String VNF_PROVIDER = "vnfProvider";
93 private static final String VNF_PRODUCT_NAME = "vnfProductName";
94 private static final String VNF_SOFTWARE_VERSION = "vnfSoftwareVersion";
95 private static final String VNFD_VERSION = "vnfdVersion";
96 private static final String ALGORITHM = "algorithm";
97 private static final String HASH = "hash";
98 private static final String EXPECTED_BASE_URL =
99 "https://so-vnfm-adapter.onap:30406/so/vnfm-adapter/v1/vnfpkgm/v1/vnf_packages/";
100 private static final String EXPECTED_SELF_HREF = EXPECTED_BASE_URL + VNF_PACKAGE_ID;
101 private static final String EXPECTED_VNFD_HREF = EXPECTED_BASE_URL + VNF_PACKAGE_ID + "/vnfd";
102 private static final String EXPECTED_PACKAGE_CONTENT_HREF = EXPECTED_BASE_URL + VNF_PACKAGE_ID + "/package_content";
104 private MockRestServiceServer mockRestServer;
105 private BasicHttpHeadersProvider basicHttpHeadersProvider;
106 private final Gson gson = new Gson();
108 public Sol003PackageManagementControllerTest() {}
111 public void setUp() {
112 final MockRestServiceServer.MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(restTemplate);
113 builder.ignoreExpectOrder(true);
114 mockRestServer = builder.build();
115 basicHttpHeadersProvider = new BasicHttpHeadersProvider();
119 public void testGetPackageContent_ValidArray_Success() {
120 final byte[] responseArray = buildByteArrayWithRandomData(10);
122 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
123 .andExpect(method(HttpMethod.GET))
124 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
126 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
127 + VNF_PACKAGE_ID + "/package_content";
128 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
129 final ResponseEntity<byte[]> responseEntity =
130 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
132 assertEquals(byte[].class, responseEntity.getBody().getClass());
133 assertArrayEquals(responseEntity.getBody(), responseArray);
134 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
138 public void testOnGetPackageContent_Conflict_Fail() {
139 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
140 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
142 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
144 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
145 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
149 public void testOnGetPackageContent_NotFound_Fail() {
150 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
151 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
153 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
155 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
156 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
160 public void testOnGetPackageContent_UnauthorizedClient_Fail() {
161 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
162 + VNF_PACKAGE_ID + "/package_content";
164 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
165 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
167 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
170 final ResponseEntity<ProblemDetails> responseEntity =
171 testRestTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
173 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
174 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
178 public void testOnGetPackageContent_InternalServerError_Fail() {
179 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
180 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
182 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
184 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
185 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
189 public void testOnGetPackageContent_BadRequest_Fail() {
190 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
191 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
193 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
195 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
196 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
200 public void testOnGetPackageContent_UnauthorizedServer_InternalError_Fail() {
201 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
202 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
204 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
206 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
207 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
211 public void testGetPackageContent_SuccessResponseFromServerWithNullPackage_Fail() {
212 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
213 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
215 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
217 assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
218 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
222 public void testGetPackageArtifact_ValidArray_Success() {
223 final byte[] responseArray = buildByteArrayWithRandomData(10);
225 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
226 .andExpect(method(HttpMethod.GET))
227 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
229 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
230 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
231 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
232 final ResponseEntity<byte[]> responseEntity =
233 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
235 assertEquals(byte[].class, responseEntity.getBody().getClass());
236 assertArrayEquals(responseEntity.getBody(), responseArray);
237 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
241 public void testOnGetPackageArtifact_Conflict_Fail() {
242 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
243 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
245 final ResponseEntity<ProblemDetails> responseEntity =
246 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
248 assertNotNull(responseEntity.getBody());
249 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
253 public void testOnGetPackageArtifact_NotFound_Fail() {
254 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
255 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
257 final ResponseEntity<ProblemDetails> responseEntity =
258 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
260 assertNotNull(responseEntity.getBody());
261 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
265 public void testOnGetPackageArtifact_UnauthorizedClient_Fail() {
266 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
267 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
269 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
270 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
272 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
273 final ResponseEntity<ProblemDetails> responseEntity =
274 testRestTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
276 assertNotNull(responseEntity.getBody());
277 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
281 public void testOnGetPackageArtifact_InternalServerError_Fail() {
282 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
283 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
285 final ResponseEntity<ProblemDetails> responseEntity =
286 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
288 assertNotNull(responseEntity.getBody());
289 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
293 public void testOnGetPackageArtifact_BadRequest_Fail() {
294 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
295 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
297 final ResponseEntity<ProblemDetails> responseEntity =
298 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
300 assertNotNull(responseEntity.getBody());
301 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
305 public void testOnGetPackageArtifact_UnauthorizedServer_InternalError_Fail() {
306 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
307 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
309 final ResponseEntity<ProblemDetails> responseEntity =
310 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
312 assertNotNull(responseEntity.getBody());
313 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
317 public void testGetPackageArtifact_SuccessResponseFromServerWithNullPackage_Fail() {
318 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
319 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
321 final ResponseEntity<ProblemDetails> responseEntity =
322 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
324 assertNotNull(responseEntity.getBody());
325 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
329 public void testVnfPackagesReceivedAsInlineResponse2001ListIfGetVnfPackagesSuccessful() {
330 final VnfPkgInfo[] responses = createVnfPkgArray();
332 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
333 .andRespond(withSuccess(gson.toJson(responses), MediaType.APPLICATION_JSON));
335 final String testURL = localhostUrl + port + VNFPKGM_BASE_URL;
336 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
338 final ResponseEntity<InlineResponse2001[]> responseEntity = testRestTemplate.withBasicAuth("test", "test")
339 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001[].class);
341 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
342 assertNotNull(responseEntity.getBody());
343 final InlineResponse2001[] inlineResponse2001array = responseEntity.getBody();
344 final InlineResponse2001 inlineResponse2001 = inlineResponse2001array[0];
345 assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
346 assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
347 assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
348 assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
349 assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
350 assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
351 assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
352 assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
353 assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
354 final VnfPackagesLinks links = inlineResponse2001.getLinks();
355 assertNotNull(links);
356 assertEquals(EXPECTED_SELF_HREF, links.getSelf().getHref());
357 assertEquals(EXPECTED_VNFD_HREF, links.getVnfd().getHref());
358 assertEquals(EXPECTED_PACKAGE_CONTENT_HREF, links.getPackageContent().getHref());
362 public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackagesIs400BadRequest() {
363 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
364 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
366 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
367 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
369 assertNotNull(responseEntity.getBody());
370 final ProblemDetails problemDetails = responseEntity.getBody();
371 assertEquals("Error: Bad Request Received", problemDetails.getDetail());
375 public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackagesIs404NotFound() {
376 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
377 .andRespond(withStatus(HttpStatus.NOT_FOUND));
379 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
380 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
382 assertNotNull(responseEntity.getBody());
383 final ProblemDetails problemDetails = responseEntity.getBody();
384 assertEquals("No Vnf Packages found", problemDetails.getDetail());
388 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturns500InternalServerError() {
389 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
390 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
392 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
393 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
395 assertNotNull(responseEntity.getBody());
396 final ProblemDetails problemDetails = responseEntity.getBody();
397 assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
401 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturnsANullPackage() {
402 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
404 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
405 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
407 assertNotNull(responseEntity.getBody());
408 final ProblemDetails problemDetails = responseEntity.getBody();
409 assertEquals("An error occurred, a null response was received by the\n"
410 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" \n"
411 + "endpoint.", problemDetails.getDetail());
415 public void testVnfPackageReceivedAsInlineResponse2001IfGetVnfPackageByIdSuccessful() {
416 final VnfPkgInfo response = createVnfPkgInfo(VNF_PACKAGE_ID);
418 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
419 .andRespond(withSuccess(gson.toJson(response), MediaType.APPLICATION_JSON));
421 final String testURL = localhostUrl + port + VNFPKGM_BASE_URL + "/" + VNF_PACKAGE_ID;
422 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
423 final ResponseEntity<InlineResponse2001> responseEntity = testRestTemplate.withBasicAuth("test", "test")
424 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001.class);
426 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
427 assertNotNull(responseEntity.getBody());
428 final InlineResponse2001 inlineResponse2001 = responseEntity.getBody();
429 assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
430 assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
431 assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
432 assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
433 assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
434 assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
435 assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
436 assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
437 assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
438 final VnfPackagesLinks links = inlineResponse2001.getLinks();
439 assertNotNull(links);
440 assertEquals(EXPECTED_SELF_HREF, links.getSelf().getHref());
441 assertEquals(EXPECTED_VNFD_HREF, links.getVnfd().getHref());
442 assertEquals(EXPECTED_PACKAGE_CONTENT_HREF, links.getPackageContent().getHref());
447 public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs400BadRequest() {
448 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
449 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
451 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
453 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
454 assertNotNull(responseEntity.getBody());
455 final ProblemDetails problemDetails = responseEntity.getBody();
456 assertEquals("Error: Bad Request Received", problemDetails.getDetail());
460 public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs404NotFound() {
461 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
462 .andRespond(withStatus(HttpStatus.NOT_FOUND));
464 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
466 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
467 assertNotNull(responseEntity.getBody());
468 final ProblemDetails problemDetails = responseEntity.getBody();
469 assertEquals("No Vnf Package found with vnfPkgId: " + VNF_PACKAGE_ID, problemDetails.getDetail());
473 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturns500InternalServerError() {
474 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
475 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
477 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
479 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
480 assertNotNull(responseEntity.getBody());
481 final ProblemDetails problemDetails = responseEntity.getBody();
482 assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
486 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturnsANullPackage() {
487 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
488 .andRespond(withSuccess());
490 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
491 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
493 assertNotNull(responseEntity.getBody());
494 final ProblemDetails problemDetails = responseEntity.getBody();
495 assertEquals("An error occurred, a null response was received by the\n"
496 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" by vnfPkgId: \""
497 + VNF_PACKAGE_ID + "\" \n" + "endpoint.", problemDetails.getDetail());
500 // The below test method is here to improve code coverage and provide a foundation for writing
503 public void testGetPackageVnfd_ValidArray_Success() {
504 final byte[] responseArray = buildByteArrayWithRandomData(10);
506 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
507 .andExpect(method(HttpMethod.GET))
508 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
510 final String testURL =
511 "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/" + VNF_PACKAGE_ID + "/vnfd";
512 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
513 final ResponseEntity<byte[]> responseEntity =
514 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
516 assertEquals(byte[].class, responseEntity.getBody().getClass());
517 assertArrayEquals(responseEntity.getBody(), responseArray);
518 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
522 public void testOnGetPackageVnfd_Conflict_Fail() {
523 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
524 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
526 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
528 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
529 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
533 public void testOnGetPackageVnfd_NotFound_Fail() {
534 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
535 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
537 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
539 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
540 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
544 public void testOnGetPackageVnfd_UnauthorizedClient_Fail() {
545 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
546 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
548 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
550 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
551 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
555 public void testOnGetPackageVnfd_InternalServerError_Fail() {
556 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
557 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
559 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
561 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
562 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
566 public void testOnGetPackageVnfd_BadRequest_Fail() {
567 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
568 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
570 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
572 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
573 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
577 public void testOnGetPackageVnfd_UnauthorizedServer_InternalError_Fail() {
578 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
579 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
581 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
583 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
584 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
588 public void testGetPackageVnfd_SuccessResponseFromServerWithNullPackage_Fail() {
589 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
590 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
592 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
594 assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
595 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
598 // Simply returns a byte array filled with random data, for use in the tests.
599 private byte[] buildByteArrayWithRandomData(final int sizeInKb) {
600 final Random rnd = new Random();
601 final byte[] b = new byte[sizeInKb * 1024]; // converting kb to byte
606 private ResponseEntity<ProblemDetails> sendHttpRequest(final String url) {
607 final String testURL = localhostUrl + port + VNFPKGM_BASE_URL + "/" + url;
608 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
609 return testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request,
610 ProblemDetails.class);
613 private VnfPkgInfo[] createVnfPkgArray() {
614 final VnfPkgInfo[] vnfPkgInfoArray = new VnfPkgInfo[1];
615 final VnfPkgInfo vnfPkgInfo = createVnfPkgInfo(VNF_PACKAGE_ID);
616 vnfPkgInfoArray[0] = vnfPkgInfo;
617 return vnfPkgInfoArray;
620 private VnfPkgInfo createVnfPkgInfo(final String vnfPackageId) {
621 final VnfPkgInfo vnfPkgInfo = new VnfPkgInfo();
622 vnfPkgInfo.setId(vnfPackageId);
623 vnfPkgInfo.setVnfdId(VNFD_ID);
624 vnfPkgInfo.setVnfProvider(VNF_PROVIDER);
625 vnfPkgInfo.setVnfProductName(VNF_PRODUCT_NAME);
626 vnfPkgInfo.setVnfSoftwareVersion(VNF_SOFTWARE_VERSION);
627 vnfPkgInfo.setVnfdVersion(VNFD_VERSION);
628 vnfPkgInfo.setChecksum(createVnfPkgChecksum());
629 vnfPkgInfo.setSoftwareImages(createSoftwareImages());
630 vnfPkgInfo.setAdditionalArtifacts(createAdditionalArtifacts());
631 vnfPkgInfo.setLinks(createVNFPKGMLinkSerializerLinks());
635 private Checksum createVnfPkgChecksum() {
636 final Checksum checksum = new Checksum();
637 checksum.setAlgorithm(ALGORITHM);
638 checksum.setHash(HASH);
642 private List<VnfPackageSoftwareImageInfo> createSoftwareImages() {
643 final List<VnfPackageSoftwareImageInfo> softwareImages = new ArrayList<>();
644 final VnfPackageSoftwareImageInfo vnfPackageSoftwareImageInfo = new VnfPackageSoftwareImageInfo();
645 vnfPackageSoftwareImageInfo.setId(VNFD_ID);
646 vnfPackageSoftwareImageInfo.setName(VNF_PRODUCT_NAME);
647 vnfPackageSoftwareImageInfo.setProvider("");
648 vnfPackageSoftwareImageInfo.setVersion("");
649 vnfPackageSoftwareImageInfo.setChecksum(createVnfPkgChecksum());
650 vnfPackageSoftwareImageInfo
651 .setContainerFormat(VnfPackageSoftwareImageInfo.ContainerFormatEnum.fromValue("AKI"));
652 softwareImages.add(vnfPackageSoftwareImageInfo);
653 return softwareImages;
656 private List<VnfPackageArtifactInfo> createAdditionalArtifacts() {
657 final List<VnfPackageArtifactInfo> vnfPackageArtifactInfos = new ArrayList<>();
658 final VnfPackageArtifactInfo vnfPackageArtifactInfo =
659 new VnfPackageArtifactInfo().artifactPath(ARTIFACT_PATH).checksum(createVnfPkgChecksum());
660 vnfPackageArtifactInfos.add(vnfPackageArtifactInfo);
661 return vnfPackageArtifactInfos;
664 private VNFPKGMLinkSerializer createVNFPKGMLinkSerializerLinks() {
665 final String baseUrl = "http://msb-iag:443/api/vnfpkgm/v1/vnf_packages";
666 return new VNFPKGMLinkSerializer().self(new UriLink().href(baseUrl + "/myVnfPackageId"))
667 .vnfd(new UriLink().href(baseUrl + "/myVnfPackageId/vnfd"))
668 .packageContent(new UriLink().href(baseUrl + "/myVnfPackageId/package_content"));