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.configuration.rest.BasicHttpHeadersProvider;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.beans.factory.annotation.Qualifier;
51 import org.springframework.boot.test.context.SpringBootTest;
52 import org.springframework.boot.test.web.client.TestRestTemplate;
53 import org.springframework.boot.web.server.LocalServerPort;
54 import org.springframework.http.HttpEntity;
55 import org.springframework.http.HttpMethod;
56 import org.springframework.http.HttpStatus;
57 import org.springframework.http.MediaType;
58 import org.springframework.http.ResponseEntity;
59 import org.springframework.test.context.ActiveProfiles;
60 import org.springframework.test.context.junit4.SpringRunner;
61 import org.springframework.test.web.client.MockRestServiceServer;
62 import org.springframework.web.client.RestTemplate;
63 import com.google.gson.Gson;
66 * @author gareth.roper@est.tech
68 @RunWith(SpringRunner.class)
69 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
70 @ActiveProfiles("test")
71 public class Sol003PackageManagementControllerTest {
77 @Qualifier(ETSI_CATALOG_REST_TEMPLATE_BEAN)
78 private RestTemplate restTemplate;
81 private TestRestTemplate testRestTemplate;
83 private static final String VNF_PACKAGE_ID = "myVnfPackageId";
84 private static final String ARTIFACT_PATH = "myArtifactPath";
85 private static final String MSB_BASE_URL = "http://msb-iag.onap:80/api/vnfpkgm/v1/vnf_packages";
86 private static final String VNFPKGM_BASE_URL = PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages";
87 private static final String localhostUrl = "http://localhost:";
88 private static final String GET_VNF_PACKAGES_URL = "";
89 private static final String GET_VNF_PACKAGE_BY_ID_URL = "/" + VNF_PACKAGE_ID;
90 private static final String VNFD_ID = "vnfdId";
91 private static final String VNF_PROVIDER = "vnfProvider";
92 private static final String VNF_PRODUCT_NAME = "vnfProductName";
93 private static final String VNF_SOFTWARE_VERSION = "vnfSoftwareVersion";
94 private static final String VNFD_VERSION = "vnfdVersion";
95 private static final String ALGORITHM = "algorithm";
96 private static final String HASH = "hash";
97 private static final String URI_HREF = "uriHref";
99 private MockRestServiceServer mockRestServer;
100 private BasicHttpHeadersProvider basicHttpHeadersProvider;
101 private final Gson gson = new Gson();
103 public Sol003PackageManagementControllerTest() {}
106 public void setUp() {
107 final MockRestServiceServer.MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(restTemplate);
108 builder.ignoreExpectOrder(true);
109 mockRestServer = builder.build();
110 basicHttpHeadersProvider = new BasicHttpHeadersProvider();
114 public void testGetPackageContent_ValidArray_Success() {
115 final byte[] responseArray = buildByteArrayWithRandomData(10);
117 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
118 .andExpect(method(HttpMethod.GET))
119 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
121 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
122 + VNF_PACKAGE_ID + "/package_content";
123 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
124 final ResponseEntity<byte[]> responseEntity =
125 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
127 assertEquals(byte[].class, responseEntity.getBody().getClass());
128 assertArrayEquals(responseEntity.getBody(), responseArray);
129 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
133 public void testOnGetPackageContent_Conflict_Fail() {
134 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
135 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
137 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
139 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
140 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
144 public void testOnGetPackageContent_NotFound_Fail() {
145 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
146 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
148 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
150 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
151 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
155 public void testOnGetPackageContent_UnauthorizedClient_Fail() {
156 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
157 + VNF_PACKAGE_ID + "/package_content";
159 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
160 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
162 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
165 final ResponseEntity<ProblemDetails> responseEntity =
166 testRestTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
168 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
169 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
173 public void testOnGetPackageContent_InternalServerError_Fail() {
174 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
175 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
177 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
179 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
180 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
184 public void testOnGetPackageContent_BadRequest_Fail() {
185 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
186 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
188 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
190 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
191 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
195 public void testOnGetPackageContent_UnauthorizedServer_InternalError_Fail() {
196 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
197 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
199 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
201 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
202 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
206 public void testGetPackageContent_SuccessResponseFromServerWithNullPackage_Fail() {
207 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
208 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
210 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
212 assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
213 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
217 public void testGetPackageArtifact_ValidArray_Success() {
218 final byte[] responseArray = buildByteArrayWithRandomData(10);
220 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
221 .andExpect(method(HttpMethod.GET))
222 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
224 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
225 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
226 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
227 final ResponseEntity<byte[]> responseEntity =
228 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
230 assertEquals(byte[].class, responseEntity.getBody().getClass());
231 assertArrayEquals(responseEntity.getBody(), responseArray);
232 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
236 public void testOnGetPackageArtifact_Conflict_Fail() {
237 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
238 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
240 final ResponseEntity<ProblemDetails> responseEntity =
241 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
243 assertNotNull(responseEntity.getBody());
244 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
248 public void testOnGetPackageArtifact_NotFound_Fail() {
249 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
250 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
252 final ResponseEntity<ProblemDetails> responseEntity =
253 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
255 assertNotNull(responseEntity.getBody());
256 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
260 public void testOnGetPackageArtifact_UnauthorizedClient_Fail() {
261 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
262 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
264 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
265 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
267 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
268 final ResponseEntity<ProblemDetails> responseEntity =
269 testRestTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
271 assertNotNull(responseEntity.getBody());
272 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
276 public void testOnGetPackageArtifact_InternalServerError_Fail() {
277 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
278 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
280 final ResponseEntity<ProblemDetails> responseEntity =
281 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
283 assertNotNull(responseEntity.getBody());
284 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
288 public void testOnGetPackageArtifact_BadRequest_Fail() {
289 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
290 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
292 final ResponseEntity<ProblemDetails> responseEntity =
293 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
295 assertNotNull(responseEntity.getBody());
296 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
300 public void testOnGetPackageArtifact_UnauthorizedServer_InternalError_Fail() {
301 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
302 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
304 final ResponseEntity<ProblemDetails> responseEntity =
305 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
307 assertNotNull(responseEntity.getBody());
308 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
312 public void testGetPackageArtifact_SuccessResponseFromServerWithNullPackage_Fail() {
313 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
314 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
316 final ResponseEntity<ProblemDetails> responseEntity =
317 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
319 assertNotNull(responseEntity.getBody());
320 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
324 public void testVnfPackagesReceivedAsInlineResponse2001ListIfGetVnfPackagesSuccessful() {
325 final VnfPkgInfo[] responses = createVnfPkgArray();
327 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
328 .andRespond(withSuccess(gson.toJson(responses), MediaType.APPLICATION_JSON));
330 final String testURL = localhostUrl + port + VNFPKGM_BASE_URL;
331 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
333 final ResponseEntity<InlineResponse2001[]> responseEntity = testRestTemplate.withBasicAuth("test", "test")
334 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001[].class);
336 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
337 assertNotNull(responseEntity.getBody());
338 final InlineResponse2001[] inlineResponse2001array = responseEntity.getBody();
339 final InlineResponse2001 inlineResponse2001 = inlineResponse2001array[0];
340 assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
341 assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
342 assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
343 assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
344 assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
345 assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
346 assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
347 assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
348 assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
349 assertEquals(URI_HREF, inlineResponse2001.getLinks().getSelf().getHref());
353 public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackagesIs400BadRequest() {
354 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
355 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
357 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
358 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
360 assertNotNull(responseEntity.getBody());
361 final ProblemDetails problemDetails = responseEntity.getBody();
362 assertEquals("Error: Bad Request Received", problemDetails.getDetail());
366 public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackagesIs404NotFound() {
367 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
368 .andRespond(withStatus(HttpStatus.NOT_FOUND));
370 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
371 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
373 assertNotNull(responseEntity.getBody());
374 final ProblemDetails problemDetails = responseEntity.getBody();
375 assertEquals("No Vnf Packages found", problemDetails.getDetail());
379 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturns500InternalServerError() {
380 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
381 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
383 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
384 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
386 assertNotNull(responseEntity.getBody());
387 final ProblemDetails problemDetails = responseEntity.getBody();
388 assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
392 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturnsANullPackage() {
393 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
395 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
396 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
398 assertNotNull(responseEntity.getBody());
399 final ProblemDetails problemDetails = responseEntity.getBody();
400 assertEquals("An error occurred, a null response was received by the\n"
401 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" \n"
402 + "endpoint.", problemDetails.getDetail());
406 public void testVnfPackageReceivedAsInlineResponse2001IfGetVnfPackageByIdSuccessful() {
407 final VnfPkgInfo response = createVnfPkgInfo(VNF_PACKAGE_ID);
409 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
410 .andRespond(withSuccess(gson.toJson(response), MediaType.APPLICATION_JSON));
412 final String testURL = localhostUrl + port + VNFPKGM_BASE_URL + "/" + VNF_PACKAGE_ID;
413 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
414 final ResponseEntity<InlineResponse2001> responseEntity = testRestTemplate.withBasicAuth("test", "test")
415 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001.class);
417 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
418 assertNotNull(responseEntity.getBody());
419 final InlineResponse2001 inlineResponse2001 = responseEntity.getBody();
420 assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
421 assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
422 assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
423 assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
424 assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
425 assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
426 assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
427 assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
428 assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
429 assertEquals(URI_HREF, inlineResponse2001.getLinks().getSelf().getHref());
433 public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs400BadRequest() {
434 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
435 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
437 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
439 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
440 assertNotNull(responseEntity.getBody());
441 final ProblemDetails problemDetails = responseEntity.getBody();
442 assertEquals("Error: Bad Request Received", problemDetails.getDetail());
446 public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs404NotFound() {
447 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
448 .andRespond(withStatus(HttpStatus.NOT_FOUND));
450 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
452 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
453 assertNotNull(responseEntity.getBody());
454 final ProblemDetails problemDetails = responseEntity.getBody();
455 assertEquals("No Vnf Package found with vnfPkgId: " + VNF_PACKAGE_ID, problemDetails.getDetail());
459 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturns500InternalServerError() {
460 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
461 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
463 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
465 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
466 assertNotNull(responseEntity.getBody());
467 final ProblemDetails problemDetails = responseEntity.getBody();
468 assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
472 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturnsANullPackage() {
473 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
474 .andRespond(withSuccess());
476 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
477 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
479 assertNotNull(responseEntity.getBody());
480 final ProblemDetails problemDetails = responseEntity.getBody();
481 assertEquals("An error occurred, a null response was received by the\n"
482 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" by vnfPkgId: \""
483 + VNF_PACKAGE_ID + "\" \n" + "endpoint.", problemDetails.getDetail());
486 // The below test method is here to improve code coverage and provide a foundation for writing
489 public void testGetPackageVnfd_ValidArray_Success() {
490 final byte[] responseArray = buildByteArrayWithRandomData(10);
492 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
493 .andExpect(method(HttpMethod.GET))
494 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
496 final String testURL =
497 "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/" + VNF_PACKAGE_ID + "/vnfd";
498 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
499 final ResponseEntity<byte[]> responseEntity =
500 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
502 assertEquals(byte[].class, responseEntity.getBody().getClass());
503 assertArrayEquals(responseEntity.getBody(), responseArray);
504 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
508 public void testOnGetPackageVnfd_Conflict_Fail() {
509 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
510 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
512 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
514 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
515 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
519 public void testOnGetPackageVnfd_NotFound_Fail() {
520 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
521 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
523 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
525 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
526 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
530 public void testOnGetPackageVnfd_UnauthorizedClient_Fail() {
531 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
532 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
534 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
536 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
537 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
541 public void testOnGetPackageVnfd_InternalServerError_Fail() {
542 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
543 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
545 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
547 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
548 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
552 public void testOnGetPackageVnfd_BadRequest_Fail() {
553 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
554 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
556 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
558 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
559 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
563 public void testOnGetPackageVnfd_UnauthorizedServer_InternalError_Fail() {
564 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
565 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
567 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
569 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
570 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
574 public void testGetPackageVnfd_SuccessResponseFromServerWithNullPackage_Fail() {
575 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
576 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
578 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
580 assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
581 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
584 // Simply returns a byte array filled with random data, for use in the tests.
585 private byte[] buildByteArrayWithRandomData(final int sizeInKb) {
586 final Random rnd = new Random();
587 final byte[] b = new byte[sizeInKb * 1024]; // converting kb to byte
592 private ResponseEntity<ProblemDetails> sendHttpRequest(final String url) {
593 final String testURL = localhostUrl + port + VNFPKGM_BASE_URL + "/" + url;
594 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
595 return testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request,
596 ProblemDetails.class);
599 private VnfPkgInfo[] createVnfPkgArray() {
600 final VnfPkgInfo[] vnfPkgInfoArray = new VnfPkgInfo[1];
601 final VnfPkgInfo vnfPkgInfo = createVnfPkgInfo(VNF_PACKAGE_ID);
602 vnfPkgInfoArray[0] = vnfPkgInfo;
603 return vnfPkgInfoArray;
606 private VnfPkgInfo createVnfPkgInfo(final String vnfPackageId) {
607 final VnfPkgInfo vnfPkgInfo = new VnfPkgInfo();
608 vnfPkgInfo.setId(vnfPackageId);
609 vnfPkgInfo.setVnfdId(VNFD_ID);
610 vnfPkgInfo.setVnfProvider(VNF_PROVIDER);
611 vnfPkgInfo.setVnfProductName(VNF_PRODUCT_NAME);
612 vnfPkgInfo.setVnfSoftwareVersion(VNF_SOFTWARE_VERSION);
613 vnfPkgInfo.setVnfdVersion(VNFD_VERSION);
614 vnfPkgInfo.setChecksum(createVnfPkgChecksum());
615 vnfPkgInfo.setSoftwareImages(createSoftwareImages());
616 vnfPkgInfo.setAdditionalArtifacts(createAdditionalArtifacts());
617 vnfPkgInfo.setLinks(createVNFPKGMLinkSerializerLinks());
621 private Checksum createVnfPkgChecksum() {
622 final Checksum checksum = new Checksum();
623 checksum.setAlgorithm(ALGORITHM);
624 checksum.setHash(HASH);
628 private List<VnfPackageSoftwareImageInfo> createSoftwareImages() {
629 final List<VnfPackageSoftwareImageInfo> softwareImages = new ArrayList<>();
630 final VnfPackageSoftwareImageInfo vnfPackageSoftwareImageInfo = new VnfPackageSoftwareImageInfo();
631 vnfPackageSoftwareImageInfo.setId(VNFD_ID);
632 vnfPackageSoftwareImageInfo.setName(VNF_PRODUCT_NAME);
633 vnfPackageSoftwareImageInfo.setProvider("");
634 vnfPackageSoftwareImageInfo.setVersion("");
635 vnfPackageSoftwareImageInfo.setChecksum(createVnfPkgChecksum());
636 vnfPackageSoftwareImageInfo
637 .setContainerFormat(VnfPackageSoftwareImageInfo.ContainerFormatEnum.fromValue("AKI"));
638 softwareImages.add(vnfPackageSoftwareImageInfo);
639 return softwareImages;
642 private List<VnfPackageArtifactInfo> createAdditionalArtifacts() {
643 final List<VnfPackageArtifactInfo> vnfPackageArtifactInfos = new ArrayList<>();
644 final VnfPackageArtifactInfo vnfPackageArtifactInfo =
645 new VnfPackageArtifactInfo().artifactPath(ARTIFACT_PATH).checksum(createVnfPkgChecksum());
646 vnfPackageArtifactInfos.add(vnfPackageArtifactInfo);
647 return vnfPackageArtifactInfos;
650 private VNFPKGMLinkSerializer createVNFPKGMLinkSerializerLinks() {
651 final UriLink uriLink = new UriLink().href(URI_HREF);
652 final VNFPKGMLinkSerializer vnfpkgmLinkSerializer = new VNFPKGMLinkSerializer().self(uriLink);
653 return vnfpkgmLinkSerializer;