2 * ============LICENSE_START=======================================================
3 * Copyright (C) 2019 Nordix Foundation.
4 * ================================================================================
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
17 * SPDX-License-Identifier: Apache-2.0
18 * ============LICENSE_END=========================================================
21 package org.onap.so.adapters.vnfmadapter.rest;
23 import static org.junit.Assert.*;
24 import static org.onap.so.adapters.vnfmadapter.Constants.PACKAGE_MANAGEMENT_BASE_URL;
25 import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
26 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
27 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
28 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
29 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
30 import java.util.ArrayList;
31 import java.util.List;
32 import java.util.Random;
33 import org.junit.Before;
34 import org.junit.Test;
35 import org.junit.runner.RunWith;
36 import org.onap.so.adapters.vnfmadapter.VnfmAdapterApplication;
37 import org.onap.so.adapters.vnfmadapter.extclients.etsicatalog.model.*;
38 import org.onap.so.adapters.vnfmadapter.extclients.vnfm.packagemanagement.model.InlineResponse2001;
39 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
40 import org.slf4j.Logger;
41 import org.slf4j.LoggerFactory;
42 import org.springframework.beans.factory.annotation.Autowired;
43 import org.springframework.beans.factory.annotation.Qualifier;
44 import org.springframework.boot.test.context.SpringBootTest;
45 import org.springframework.boot.test.web.client.TestRestTemplate;
46 import org.springframework.boot.web.server.LocalServerPort;
47 import org.springframework.http.*;
48 import org.springframework.test.context.ActiveProfiles;
49 import org.springframework.test.context.junit4.SpringRunner;
50 import org.springframework.test.web.client.MockRestServiceServer;
51 import org.springframework.web.client.RestTemplate;
52 import com.google.gson.Gson;
55 * @author gareth.roper@est.tech
57 @RunWith(SpringRunner.class)
58 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
59 @ActiveProfiles("test")
60 public class Sol003PackageManagementControllerTest {
62 private static final Logger logger = LoggerFactory.getLogger(Sol003PackageManagementControllerTest.class);
68 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
69 private RestTemplate testRestTemplate;
72 private Sol003PackageManagementController controller;
75 private TestRestTemplate restTemplate;
77 private static final String VNF_PACKAGE_ID = "myVnfPackageId";
78 private static final String ARTIFACT_PATH = "myArtifactPath";
79 private static final String MSB_BASE_URL = "http://msb_iag.onap:80/api/vnfpkgm/v1/vnf_packages";
80 private static final String VNFPKGM_BASE_URL = PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages";
81 private static final String localhostUrl = "http://localhost:";
82 private static final String GET_VNF_PACKAGES_URL = "";
83 private static final String GET_VNF_PACKAGE_BY_ID_URL = "/" + VNF_PACKAGE_ID;
84 private static final String VNFD_ID = "vnfdId";
85 private static final String VNF_PROVIDER = "vnfProvider";
86 private static final String VNF_PRODUCT_NAME = "vnfProductName";
87 private static final String VNF_SOFTWARE_VERSION = "vnfSoftwareVersion";
88 private static final String VNFD_VERSION = "vnfdVersion";
89 private static final String ALGORITHM = "algorithm";
90 private static final String HASH = "hash";
91 private static final String URI_HREF = "uriHref";
93 private MockRestServiceServer mockRestServer;
94 private BasicHttpHeadersProvider basicHttpHeadersProvider;
95 private final Gson gson = new Gson();
97 public Sol003PackageManagementControllerTest() {}
100 public void setUp() {
101 final MockRestServiceServer.MockRestServiceServerBuilder builder =
102 MockRestServiceServer.bindTo(testRestTemplate);
103 builder.ignoreExpectOrder(true);
104 mockRestServer = builder.build();
105 basicHttpHeadersProvider = new BasicHttpHeadersProvider();
109 public void testGetPackageContent_ValidArray_Success() {
110 final byte[] responseArray = buildByteArrayWithRandomData(10);
112 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
113 .andExpect(method(HttpMethod.GET))
114 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
116 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
117 + VNF_PACKAGE_ID + "/package_content";
118 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
119 final ResponseEntity<byte[]> responseEntity =
120 restTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
122 assertEquals(byte[].class, responseEntity.getBody().getClass());
123 assertArrayEquals(responseEntity.getBody(), responseArray);
124 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
128 public void testOnGetPackageContent_Conflict_Fail() {
129 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
130 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
132 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
134 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
135 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
139 public void testOnGetPackageContent_NotFound_Fail() {
140 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
141 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
143 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
145 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
146 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
150 public void testOnGetPackageContent_UnauthorizedClient_Fail() {
151 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
152 + VNF_PACKAGE_ID + "/package_content";
153 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
154 final ResponseEntity<ProblemDetails> responseEntity =
155 restTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
157 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
158 assertEquals(HttpStatus.UNAUTHORIZED, responseEntity.getStatusCode());
162 public void testOnGetPackageContent_InternalServerError_Fail() {
163 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
164 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
166 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
168 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
169 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
173 public void testOnGetPackageContent_BadRequest_Fail() {
174 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
175 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
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_UnauthorizedServer_InternalError_Fail() {
185 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
186 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
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 testGetPackageContent_SuccessResponseFromServerWithNullPackage_Fail() {
196 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
197 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
199 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
201 assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
202 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
206 public void testGetPackageArtifact_ValidArray_Success() {
207 final byte[] responseArray = buildByteArrayWithRandomData(10);
209 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
210 .andExpect(method(HttpMethod.GET))
211 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
213 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
214 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
215 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
216 final ResponseEntity<byte[]> responseEntity =
217 restTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
219 assertEquals(byte[].class, responseEntity.getBody().getClass());
220 assertArrayEquals(responseEntity.getBody(), responseArray);
221 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
225 public void testOnGetPackageArtifact_Conflict_Fail() {
226 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
227 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
229 final ResponseEntity<ProblemDetails> responseEntity =
230 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
232 assertNotNull(responseEntity.getBody());
233 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
237 public void testOnGetPackageArtifact_NotFound_Fail() {
238 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
239 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
241 final ResponseEntity<ProblemDetails> responseEntity =
242 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
244 assertNotNull(responseEntity.getBody());
245 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
249 public void testOnGetPackageArtifact_UnauthorizedClient_Fail() {
250 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
251 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
252 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
253 final ResponseEntity<ProblemDetails> responseEntity =
254 restTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
256 assertNotNull(responseEntity.getBody());
257 assertEquals(HttpStatus.UNAUTHORIZED, responseEntity.getStatusCode());
261 public void testOnGetPackageArtifact_InternalServerError_Fail() {
262 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
263 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
265 final ResponseEntity<ProblemDetails> responseEntity =
266 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
268 assertNotNull(responseEntity.getBody());
269 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
273 public void testOnGetPackageArtifact_BadRequest_Fail() {
274 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
275 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
277 final ResponseEntity<ProblemDetails> responseEntity =
278 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
280 assertNotNull(responseEntity.getBody());
281 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
285 public void testOnGetPackageArtifact_UnauthorizedServer_InternalError_Fail() {
286 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
287 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
289 final ResponseEntity<ProblemDetails> responseEntity =
290 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
292 assertNotNull(responseEntity.getBody());
293 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
297 public void testGetPackageArtifact_SuccessResponseFromServerWithNullPackage_Fail() {
298 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
299 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
301 final ResponseEntity<ProblemDetails> responseEntity =
302 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
304 assertNotNull(responseEntity.getBody());
305 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
309 public void testVnfPackagesReceivedAsInlineResponse2001ListIfGetVnfPackagesSuccessful() {
310 final VnfPkgInfo[] responses = createVnfPkgArray();
312 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
313 .andRespond(withSuccess(gson.toJson(responses), MediaType.APPLICATION_JSON));
315 final String testURL = localhostUrl + port + VNFPKGM_BASE_URL;
316 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
318 final ResponseEntity<InlineResponse2001[]> responseEntity = restTemplate.withBasicAuth("test", "test")
319 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001[].class);
321 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
322 assertNotNull(responseEntity.getBody());
323 final InlineResponse2001[] inlineResponse2001array = responseEntity.getBody();
324 final InlineResponse2001 inlineResponse2001 = inlineResponse2001array[0];
325 assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
326 assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
327 assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
328 assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
329 assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
330 assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
331 assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
332 assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
333 assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
334 assertEquals(URI_HREF, inlineResponse2001.getLinks().getSelf().getHref());
338 public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackagesIs400BadRequest() {
339 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
340 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
342 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
343 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
345 assertNotNull(responseEntity.getBody());
346 final ProblemDetails problemDetails = responseEntity.getBody();
347 assertEquals("Error: Bad Request Received", problemDetails.getDetail());
351 public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackagesIs404NotFound() {
352 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
353 .andRespond(withStatus(HttpStatus.NOT_FOUND));
355 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
356 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
358 assertNotNull(responseEntity.getBody());
359 final ProblemDetails problemDetails = responseEntity.getBody();
360 assertEquals("No Vnf Packages found", problemDetails.getDetail());
364 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturns500InternalServerError() {
365 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
366 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
368 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
369 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
371 assertNotNull(responseEntity.getBody());
372 final ProblemDetails problemDetails = responseEntity.getBody();
373 assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
377 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturnsANullPackage() {
378 mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
380 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
381 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
383 assertNotNull(responseEntity.getBody());
384 final ProblemDetails problemDetails = responseEntity.getBody();
385 assertEquals("An error occurred, a null response was received by the\n"
386 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" \n"
387 + "endpoint.", problemDetails.getDetail());
391 public void testVnfPackageReceivedAsInlineResponse2001IfGetVnfPackageByIdSuccessful() {
392 final VnfPkgInfo response = createVnfPkgInfo(VNF_PACKAGE_ID);
394 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
395 .andRespond(withSuccess(gson.toJson(response), MediaType.APPLICATION_JSON));
397 final String testURL = localhostUrl + port + VNFPKGM_BASE_URL + "/" + VNF_PACKAGE_ID;
398 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
399 final ResponseEntity<InlineResponse2001> responseEntity = restTemplate.withBasicAuth("test", "test")
400 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001.class);
402 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
403 assertNotNull(responseEntity.getBody());
404 final InlineResponse2001 inlineResponse2001 = responseEntity.getBody();
405 assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
406 assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
407 assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
408 assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
409 assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
410 assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
411 assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
412 assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
413 assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
414 assertEquals(URI_HREF, inlineResponse2001.getLinks().getSelf().getHref());
418 public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs400BadRequest() {
419 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
420 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
422 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
424 assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
425 assertNotNull(responseEntity.getBody());
426 final ProblemDetails problemDetails = responseEntity.getBody();
427 assertEquals("Error: Bad Request Received", problemDetails.getDetail());
431 public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs404NotFound() {
432 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
433 .andRespond(withStatus(HttpStatus.NOT_FOUND));
435 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
437 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
438 assertNotNull(responseEntity.getBody());
439 final ProblemDetails problemDetails = responseEntity.getBody();
440 assertEquals("No Vnf Package found with vnfPkgId: " + VNF_PACKAGE_ID, problemDetails.getDetail());
444 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturns500InternalServerError() {
445 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
446 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
448 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
450 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
451 assertNotNull(responseEntity.getBody());
452 final ProblemDetails problemDetails = responseEntity.getBody();
453 assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
457 public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturnsANullPackage() {
458 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
459 .andRespond(withSuccess());
461 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
462 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
464 assertNotNull(responseEntity.getBody());
465 final ProblemDetails problemDetails = responseEntity.getBody();
466 assertEquals("An error occurred, a null response was received by the\n"
467 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" by vnfPkgId: \""
468 + VNF_PACKAGE_ID + "\" \n" + "endpoint.", problemDetails.getDetail());
471 // The below test method is here to improve code coverage and provide a foundation for writing future tests
473 public void testGetPackageVnfd_ValidArray_Success() {
474 final byte[] responseArray = buildByteArrayWithRandomData(10);
476 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
477 .andExpect(method(HttpMethod.GET))
478 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
480 final String testURL =
481 "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/" + VNF_PACKAGE_ID + "/vnfd";
482 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
483 final ResponseEntity<byte[]> responseEntity =
484 restTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
486 assertEquals(byte[].class, responseEntity.getBody().getClass());
487 assertArrayEquals(responseEntity.getBody(), responseArray);
488 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
492 public void testOnGetPackageVnfd_Conflict_Fail() {
493 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
494 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
496 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
498 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
499 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
503 public void testOnGetPackageVnfd_NotFound_Fail() {
504 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
505 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
507 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
509 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
510 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
514 public void testOnGetPackageVnfd_UnauthorizedClient_Fail() {
515 final String testURL =
516 "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/" + VNF_PACKAGE_ID + "/vnfd";
517 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
518 final ResponseEntity<ProblemDetails> responseEntity =
519 restTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
521 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
522 assertEquals(HttpStatus.UNAUTHORIZED, responseEntity.getStatusCode());
526 public void testOnGetPackageVnfd_InternalServerError_Fail() {
527 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
528 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
530 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
532 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
533 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
537 public void testOnGetPackageVnfd_BadRequest_Fail() {
538 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
539 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
541 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
543 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
544 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
548 public void testOnGetPackageVnfd_UnauthorizedServer_InternalError_Fail() {
549 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
550 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
552 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
554 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
555 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
559 public void testGetPackageVnfd_SuccessResponseFromServerWithNullPackage_Fail() {
560 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
561 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
563 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
565 assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
566 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
569 // Simply returns a byte array filled with random data, for use in the tests.
570 private byte[] buildByteArrayWithRandomData(final int sizeInKb) {
571 final Random rnd = new Random();
572 final byte[] b = new byte[sizeInKb * 1024]; // converting kb to byte
577 private ResponseEntity<ProblemDetails> sendHttpRequest(final String url) {
578 final String testURL = localhostUrl + port + VNFPKGM_BASE_URL + "/" + url;
579 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
580 return restTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request,
581 ProblemDetails.class);
584 private VnfPkgInfo[] createVnfPkgArray() {
585 final VnfPkgInfo[] vnfPkgInfoArray = new VnfPkgInfo[1];
586 final VnfPkgInfo vnfPkgInfo = createVnfPkgInfo(VNF_PACKAGE_ID);
587 vnfPkgInfoArray[0] = vnfPkgInfo;
588 return vnfPkgInfoArray;
591 private VnfPkgInfo createVnfPkgInfo(final String vnfPackageId) {
592 final VnfPkgInfo vnfPkgInfo = new VnfPkgInfo();
593 vnfPkgInfo.setId(vnfPackageId);
594 vnfPkgInfo.setVnfdId(VNFD_ID);
595 vnfPkgInfo.setVnfProvider(VNF_PROVIDER);
596 vnfPkgInfo.setVnfProductName(VNF_PRODUCT_NAME);
597 vnfPkgInfo.setVnfSoftwareVersion(VNF_SOFTWARE_VERSION);
598 vnfPkgInfo.setVnfdVersion(VNFD_VERSION);
599 vnfPkgInfo.setChecksum(createVnfPkgChecksum());
600 vnfPkgInfo.setSoftwareImages(createSoftwareImages());
601 vnfPkgInfo.setAdditionalArtifacts(createAdditionalArtifacts());
602 vnfPkgInfo.setLinks(createVNFPKGMLinkSerializerLinks());
606 private Checksum createVnfPkgChecksum() {
607 final Checksum checksum = new Checksum();
608 checksum.setAlgorithm(ALGORITHM);
609 checksum.setHash(HASH);
613 private List<VnfPackageSoftwareImageInfo> createSoftwareImages() {
614 final List<VnfPackageSoftwareImageInfo> softwareImages = new ArrayList<>();
615 final VnfPackageSoftwareImageInfo vnfPackageSoftwareImageInfo = new VnfPackageSoftwareImageInfo();
616 vnfPackageSoftwareImageInfo.setId(VNFD_ID);
617 vnfPackageSoftwareImageInfo.setName(VNF_PRODUCT_NAME);
618 vnfPackageSoftwareImageInfo.setProvider("");
619 vnfPackageSoftwareImageInfo.setVersion("");
620 vnfPackageSoftwareImageInfo.setChecksum(createVnfPkgChecksum());
621 vnfPackageSoftwareImageInfo
622 .setContainerFormat(VnfPackageSoftwareImageInfo.ContainerFormatEnum.fromValue("AKI"));
623 softwareImages.add(vnfPackageSoftwareImageInfo);
624 return softwareImages;
627 private List<VnfPackageArtifactInfo> createAdditionalArtifacts() {
628 final List<VnfPackageArtifactInfo> vnfPackageArtifactInfos = new ArrayList<>();
629 final VnfPackageArtifactInfo vnfPackageArtifactInfo =
630 new VnfPackageArtifactInfo().artifactPath(ARTIFACT_PATH).checksum(createVnfPkgChecksum());
631 vnfPackageArtifactInfos.add(vnfPackageArtifactInfo);
632 return vnfPackageArtifactInfos;
635 private VNFPKGMLinkSerializer createVNFPKGMLinkSerializerLinks() {
636 final UriLink uriLink = new UriLink().href(URI_HREF);
637 final VNFPKGMLinkSerializer vnfpkgmLinkSerializer = new VNFPKGMLinkSerializer().self(uriLink);
638 return vnfpkgmLinkSerializer;