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.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
29 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
30 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
31 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
32 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
33 import java.util.ArrayList;
34 import java.util.List;
35 import java.util.Random;
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
40 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.Checksum;
41 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.ProblemDetails;
42 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.UriLink;
43 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VNFPKGMLinkSerializer;
44 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfPackageArtifactInfo;
45 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfPackageSoftwareImageInfo;
46 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.VnfPkgInfo;
47 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2001;
48 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
49 import org.springframework.beans.factory.annotation.Autowired;
50 import org.springframework.beans.factory.annotation.Qualifier;
51 import org.springframework.boot.test.context.SpringBootTest;
52 import org.springframework.boot.test.web.client.TestRestTemplate;
53 import org.springframework.boot.web.server.LocalServerPort;
54 import org.springframework.http.HttpEntity;
55 import org.springframework.http.HttpMethod;
56 import org.springframework.http.HttpStatus;
57 import org.springframework.http.MediaType;
58 import org.springframework.http.ResponseEntity;
59 import org.springframework.test.context.ActiveProfiles;
60 import org.springframework.test.context.junit4.SpringRunner;
61 import org.springframework.test.web.client.MockRestServiceServer;
62 import org.springframework.web.client.RestTemplate;
63 import com.google.gson.Gson;
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(CONFIGURABLE_REST_TEMPLATE)
78 private RestTemplate testRestTemplate;
81 private TestRestTemplate restTemplate;
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 =
108 MockRestServiceServer.bindTo(testRestTemplate);
109 builder.ignoreExpectOrder(true);
110 mockRestServer = builder.build();
111 basicHttpHeadersProvider = new BasicHttpHeadersProvider();
115 public void testGetPackageContent_ValidArray_Success() {
116 final byte[] responseArray = buildByteArrayWithRandomData(10);
118 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
119 .andExpect(method(HttpMethod.GET))
120 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
122 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
123 + VNF_PACKAGE_ID + "/package_content";
124 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
125 final ResponseEntity<byte[]> responseEntity =
126 restTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
128 assertEquals(byte[].class, responseEntity.getBody().getClass());
129 assertArrayEquals(responseEntity.getBody(), responseArray);
130 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
134 public void testOnGetPackageContent_Conflict_Fail() {
135 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
136 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
138 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
140 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
141 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
145 public void testOnGetPackageContent_NotFound_Fail() {
146 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
147 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
149 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
151 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
152 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
156 public void testOnGetPackageContent_UnauthorizedClient_Fail() {
157 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
158 + VNF_PACKAGE_ID + "/package_content";
160 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
161 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
163 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
166 final ResponseEntity<ProblemDetails> responseEntity =
167 restTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
169 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
170 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
174 public void testOnGetPackageContent_InternalServerError_Fail() {
175 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
176 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
178 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
180 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
181 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
185 public void testOnGetPackageContent_BadRequest_Fail() {
186 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
187 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
189 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
191 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
192 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
196 public void testOnGetPackageContent_UnauthorizedServer_InternalError_Fail() {
197 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
198 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
200 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
202 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
203 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
207 public void testGetPackageContent_SuccessResponseFromServerWithNullPackage_Fail() {
208 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
209 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
211 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
213 assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
214 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
218 public void testGetPackageArtifact_ValidArray_Success() {
219 final byte[] responseArray = buildByteArrayWithRandomData(10);
221 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
222 .andExpect(method(HttpMethod.GET))
223 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
225 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
226 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
227 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
228 final ResponseEntity<byte[]> responseEntity =
229 restTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
231 assertEquals(byte[].class, responseEntity.getBody().getClass());
232 assertArrayEquals(responseEntity.getBody(), responseArray);
233 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
237 public void testOnGetPackageArtifact_Conflict_Fail() {
238 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
239 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
241 final ResponseEntity<ProblemDetails> responseEntity =
242 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
244 assertNotNull(responseEntity.getBody());
245 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
249 public void testOnGetPackageArtifact_NotFound_Fail() {
250 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
251 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
253 final ResponseEntity<ProblemDetails> responseEntity =
254 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
256 assertNotNull(responseEntity.getBody());
257 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
261 public void testOnGetPackageArtifact_UnauthorizedClient_Fail() {
262 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
263 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
265 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
266 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
268 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
269 final ResponseEntity<ProblemDetails> responseEntity =
270 restTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
272 assertNotNull(responseEntity.getBody());
273 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
277 public void testOnGetPackageArtifact_InternalServerError_Fail() {
278 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
279 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
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_BadRequest_Fail() {
290 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
291 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
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 testOnGetPackageArtifact_UnauthorizedServer_InternalError_Fail() {
302 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
303 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
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 testGetPackageArtifact_SuccessResponseFromServerWithNullPackage_Fail() {
314 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
315 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
317 final ResponseEntity<ProblemDetails> responseEntity =
318 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
320 assertNotNull(responseEntity.getBody());
321 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
325 public void testVnfPackagesReceivedAsInlineResponse2001ListIfGetVnfPackagesSuccessful() {
326 final VnfPkgInfo[] responses = createVnfPkgArray();
328 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
329 .andRespond(withSuccess(gson.toJson(responses), MediaType.APPLICATION_JSON));
331 final String testURL = localhostUrl + port + VNFPKGM_BASE_URL;
332 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
334 final ResponseEntity<InlineResponse2001[]> responseEntity = restTemplate.withBasicAuth("test", "test")
335 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001[].class);
337 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
338 assertNotNull(responseEntity.getBody());
339 final InlineResponse2001[] inlineResponse2001array = responseEntity.getBody();
340 final InlineResponse2001 inlineResponse2001 = inlineResponse2001array[0];
341 assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
342 assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
343 assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
344 assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
345 assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
346 assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
347 assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
348 assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
349 assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
350 assertEquals(URI_HREF, inlineResponse2001.getLinks().getSelf().getHref());
354 public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackagesIs400BadRequest() {
355 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
356 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
358 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
359 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
361 assertNotNull(responseEntity.getBody());
362 final ProblemDetails problemDetails = responseEntity.getBody();
363 assertEquals("Error: Bad Request Received", problemDetails.getDetail());
367 public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackagesIs404NotFound() {
368 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
369 .andRespond(withStatus(HttpStatus.NOT_FOUND));
371 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
372 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
374 assertNotNull(responseEntity.getBody());
375 final ProblemDetails problemDetails = responseEntity.getBody();
376 assertEquals("No Vnf Packages found", problemDetails.getDetail());
380 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturns500InternalServerError() {
381 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
382 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
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("Internal Server Error Occurred.", problemDetails.getDetail());
393 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturnsANullPackage() {
394 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
396 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
397 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
399 assertNotNull(responseEntity.getBody());
400 final ProblemDetails problemDetails = responseEntity.getBody();
401 assertEquals("An error occurred, a null response was received by the\n"
402 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" \n"
403 + "endpoint.", problemDetails.getDetail());
407 public void testVnfPackageReceivedAsInlineResponse2001IfGetVnfPackageByIdSuccessful() {
408 final VnfPkgInfo response = createVnfPkgInfo(VNF_PACKAGE_ID);
410 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
411 .andRespond(withSuccess(gson.toJson(response), MediaType.APPLICATION_JSON));
413 final String testURL = localhostUrl + port + VNFPKGM_BASE_URL + "/" + VNF_PACKAGE_ID;
414 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
415 final ResponseEntity<InlineResponse2001> responseEntity = restTemplate.withBasicAuth("test", "test")
416 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001.class);
418 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
419 assertNotNull(responseEntity.getBody());
420 final InlineResponse2001 inlineResponse2001 = responseEntity.getBody();
421 assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
422 assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
423 assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
424 assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
425 assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
426 assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
427 assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
428 assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
429 assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
430 assertEquals(URI_HREF, inlineResponse2001.getLinks().getSelf().getHref());
434 public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs400BadRequest() {
435 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
436 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
438 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
440 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
441 assertNotNull(responseEntity.getBody());
442 final ProblemDetails problemDetails = responseEntity.getBody();
443 assertEquals("Error: Bad Request Received", problemDetails.getDetail());
447 public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs404NotFound() {
448 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
449 .andRespond(withStatus(HttpStatus.NOT_FOUND));
451 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
453 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
454 assertNotNull(responseEntity.getBody());
455 final ProblemDetails problemDetails = responseEntity.getBody();
456 assertEquals("No Vnf Package found with vnfPkgId: " + VNF_PACKAGE_ID, problemDetails.getDetail());
460 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturns500InternalServerError() {
461 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
462 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
464 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
466 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
467 assertNotNull(responseEntity.getBody());
468 final ProblemDetails problemDetails = responseEntity.getBody();
469 assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
473 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturnsANullPackage() {
474 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
475 .andRespond(withSuccess());
477 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
478 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
480 assertNotNull(responseEntity.getBody());
481 final ProblemDetails problemDetails = responseEntity.getBody();
482 assertEquals("An error occurred, a null response was received by the\n"
483 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" by vnfPkgId: \""
484 + VNF_PACKAGE_ID + "\" \n" + "endpoint.", problemDetails.getDetail());
487 // The below test method is here to improve code coverage and provide a foundation for writing future tests
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 restTemplate.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 restTemplate.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;