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.etsisol003adapter.pkgm.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.etsi.sol003.adapter.common.CommonConstants.PACKAGE_MANAGEMENT_BASE_URL;
 
  28 import static org.onap.so.adapters.etsisol003adapter.pkgm.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.After;
 
  37 import org.junit.Before;
 
  38 import org.junit.Test;
 
  39 import org.junit.runner.RunWith;
 
  40 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.Checksum;
 
  41 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.ProblemDetails;
 
  42 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.UriLink;
 
  43 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.VNFPKGMLinkSerializer;
 
  44 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.VnfPackageArtifactInfo;
 
  45 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.VnfPackageSoftwareImageInfo;
 
  46 import org.onap.so.adapters.etsisol003adapter.pkgm.extclients.etsicatalog.model.VnfPkgInfo;
 
  47 import org.onap.so.adapters.etsisol003adapter.pkgm.model.InlineResponse2001;
 
  48 import org.onap.so.adapters.etsisol003adapter.pkgm.model.VnfPackagesLinks;
 
  49 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
 
  50 import org.springframework.beans.factory.annotation.Autowired;
 
  51 import org.springframework.beans.factory.annotation.Qualifier;
 
  52 import org.springframework.boot.test.context.SpringBootTest;
 
  53 import org.springframework.boot.test.web.client.TestRestTemplate;
 
  54 import org.springframework.boot.web.server.LocalServerPort;
 
  55 import org.springframework.http.HttpEntity;
 
  56 import org.springframework.http.HttpMethod;
 
  57 import org.springframework.http.HttpStatus;
 
  58 import org.springframework.http.MediaType;
 
  59 import org.springframework.http.ResponseEntity;
 
  60 import org.springframework.test.context.ActiveProfiles;
 
  61 import org.springframework.test.context.junit4.SpringRunner;
 
  62 import org.springframework.test.web.client.MockRestServiceServer;
 
  63 import org.springframework.web.client.RestTemplate;
 
  64 import com.google.gson.Gson;
 
  67  * @author gareth.roper@est.tech
 
  69 @RunWith(SpringRunner.class)
 
  70 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
 
  71 @ActiveProfiles("test")
 
  72 public class Sol003PackageManagementControllerTest {
 
  78     @Qualifier(ETSI_CATALOG_REST_TEMPLATE_BEAN)
 
  79     private RestTemplate restTemplate;
 
  82     private TestRestTemplate testRestTemplate;
 
  84     private static final String VNF_PACKAGE_ID = "myVnfPackageId";
 
  85     private static final String ARTIFACT_PATH = "myArtifactPath";
 
  86     private static final String MSB_BASE_URL = "http://msb-iag.onap:80/api/vnfpkgm/v1/vnf_packages";
 
  87     private static final String VNFPKGM_BASE_URL = PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages";
 
  88     private static final String localhostUrl = "http://localhost:";
 
  89     private static final String GET_VNF_PACKAGES_URL = "";
 
  90     private static final String GET_VNF_PACKAGE_BY_ID_URL = "/" + VNF_PACKAGE_ID;
 
  91     private static final String VNFD_ID = "vnfdId";
 
  92     private static final String VNF_PROVIDER = "vnfProvider";
 
  93     private static final String VNF_PRODUCT_NAME = "vnfProductName";
 
  94     private static final String VNF_SOFTWARE_VERSION = "vnfSoftwareVersion";
 
  95     private static final String VNFD_VERSION = "vnfdVersion";
 
  96     private static final String ALGORITHM = "algorithm";
 
  97     private static final String HASH = "hash";
 
  98     private static final String EXPECTED_BASE_URL =
 
  99             "https://so-vnfm-adapter.onap:30406/so/vnfm-adapter/v1/vnfpkgm/v1/vnf_packages/";
 
 100     private static final String EXPECTED_SELF_HREF = EXPECTED_BASE_URL + VNF_PACKAGE_ID;
 
 101     private static final String EXPECTED_VNFD_HREF = EXPECTED_BASE_URL + VNF_PACKAGE_ID + "/vnfd";
 
 102     private static final String EXPECTED_PACKAGE_CONTENT_HREF = EXPECTED_BASE_URL + VNF_PACKAGE_ID + "/package_content";
 
 104     private MockRestServiceServer mockRestServiceServer;
 
 105     private BasicHttpHeadersProvider basicHttpHeadersProvider;
 
 106     private final Gson gson = new Gson();
 
 108     public Sol003PackageManagementControllerTest() {}
 
 111     public void setUp() {
 
 112         final MockRestServiceServer.MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(restTemplate);
 
 113         builder.ignoreExpectOrder(true);
 
 114         mockRestServiceServer = builder.build();
 
 115         basicHttpHeadersProvider = new BasicHttpHeadersProvider();
 
 119     public void after() {
 
 120         mockRestServiceServer.reset();
 
 124     public void testGetPackageContent_ValidArray_Success() {
 
 125         final byte[] responseArray = buildByteArrayWithRandomData(10);
 
 127         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
 
 128                 .andExpect(method(HttpMethod.GET))
 
 129                 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
 
 131         final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
 
 132                 + VNF_PACKAGE_ID + "/package_content";
 
 133         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
 
 134         final ResponseEntity<byte[]> responseEntity =
 
 135                 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
 
 137         assertEquals(byte[].class, responseEntity.getBody().getClass());
 
 138         assertArrayEquals(responseEntity.getBody(), responseArray);
 
 139         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
 
 143     public void testOnGetPackageContent_Conflict_Fail() {
 
 144         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
 
 145                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
 
 147         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
 
 149         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
 
 150         assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
 
 154     public void testOnGetPackageContent_NotFound_Fail() {
 
 155         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
 
 156                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
 
 158         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
 
 160         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
 
 161         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
 
 165     public void testOnGetPackageContent_UnauthorizedClient_Fail() {
 
 166         final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
 
 167                 + VNF_PACKAGE_ID + "/package_content";
 
 169         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
 
 170                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
 
 172         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
 
 175         final ResponseEntity<ProblemDetails> responseEntity =
 
 176                 testRestTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
 
 178         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
 
 179         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 183     public void testOnGetPackageContent_InternalServerError_Fail() {
 
 184         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
 
 185                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
 
 187         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
 
 189         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
 
 190         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 194     public void testOnGetPackageContent_BadRequest_Fail() {
 
 195         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
 
 196                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
 
 198         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
 
 200         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
 
 201         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 205     public void testOnGetPackageContent_UnauthorizedServer_InternalError_Fail() {
 
 206         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
 
 207                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
 
 209         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
 
 211         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
 
 212         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 216     public void testGetPackageContent_SuccessResponseFromServerWithNullPackage_Fail() {
 
 217         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
 
 218                 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
 
 220         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
 
 222         assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
 
 223         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 227     public void testGetPackageArtifact_ValidArray_Success() {
 
 228         final byte[] responseArray = buildByteArrayWithRandomData(10);
 
 230         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
 
 231                 .andExpect(method(HttpMethod.GET))
 
 232                 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
 
 234         final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
 
 235                 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
 
 236         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
 
 237         final ResponseEntity<byte[]> responseEntity =
 
 238                 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
 
 240         assertEquals(byte[].class, responseEntity.getBody().getClass());
 
 241         assertArrayEquals(responseEntity.getBody(), responseArray);
 
 242         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
 
 246     public void testOnGetPackageArtifact_Conflict_Fail() {
 
 247         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
 
 248                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
 
 250         final ResponseEntity<ProblemDetails> responseEntity =
 
 251                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
 
 253         assertNotNull(responseEntity.getBody());
 
 254         assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
 
 258     public void testOnGetPackageArtifact_NotFound_Fail() {
 
 259         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
 
 260                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
 
 262         final ResponseEntity<ProblemDetails> responseEntity =
 
 263                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
 
 265         assertNotNull(responseEntity.getBody());
 
 266         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
 
 270     public void testOnGetPackageArtifact_UnauthorizedClient_Fail() {
 
 271         final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
 
 272                 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
 
 274         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
 
 275                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
 
 277         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
 
 278         final ResponseEntity<ProblemDetails> responseEntity =
 
 279                 testRestTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
 
 281         assertNotNull(responseEntity.getBody());
 
 282         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 286     public void testOnGetPackageArtifact_InternalServerError_Fail() {
 
 287         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
 
 288                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
 
 290         final ResponseEntity<ProblemDetails> responseEntity =
 
 291                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
 
 293         assertNotNull(responseEntity.getBody());
 
 294         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 298     public void testOnGetPackageArtifact_BadRequest_Fail() {
 
 299         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
 
 300                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
 
 302         final ResponseEntity<ProblemDetails> responseEntity =
 
 303                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
 
 305         assertNotNull(responseEntity.getBody());
 
 306         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 310     public void testOnGetPackageArtifact_UnauthorizedServer_InternalError_Fail() {
 
 311         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
 
 312                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
 
 314         final ResponseEntity<ProblemDetails> responseEntity =
 
 315                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
 
 317         assertNotNull(responseEntity.getBody());
 
 318         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 322     public void testGetPackageArtifact_SuccessResponseFromServerWithNullPackage_Fail() {
 
 323         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
 
 324                 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
 
 326         final ResponseEntity<ProblemDetails> responseEntity =
 
 327                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
 
 329         assertNotNull(responseEntity.getBody());
 
 330         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 334     public void testVnfPackagesReceivedAsInlineResponse2001ListIfGetVnfPackagesSuccessful() {
 
 335         final VnfPkgInfo[] responses = createVnfPkgArray();
 
 337         mockRestServiceServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
 
 338                 .andRespond(withSuccess(gson.toJson(responses), MediaType.APPLICATION_JSON));
 
 340         final String testURL = localhostUrl + port + VNFPKGM_BASE_URL;
 
 341         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
 
 343         final ResponseEntity<InlineResponse2001[]> responseEntity = testRestTemplate.withBasicAuth("test", "test")
 
 344                 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001[].class);
 
 346         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
 
 347         assertNotNull(responseEntity.getBody());
 
 348         final InlineResponse2001[] inlineResponse2001array = responseEntity.getBody();
 
 349         final InlineResponse2001 inlineResponse2001 = inlineResponse2001array[0];
 
 350         assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
 
 351         assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
 
 352         assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
 
 353         assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
 
 354         assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
 
 355         assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
 
 356         assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
 
 357         assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
 
 358         assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
 
 359         final VnfPackagesLinks links = inlineResponse2001.getLinks();
 
 360         assertNotNull(links);
 
 361         assertEquals(EXPECTED_SELF_HREF, links.getSelf().getHref());
 
 362         assertEquals(EXPECTED_VNFD_HREF, links.getVnfd().getHref());
 
 363         assertEquals(EXPECTED_PACKAGE_CONTENT_HREF, links.getPackageContent().getHref());
 
 367     public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackagesIs400BadRequest() {
 
 368         mockRestServiceServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
 
 369                 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
 
 371         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
 
 372         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
 
 374         assertNotNull(responseEntity.getBody());
 
 375         final ProblemDetails problemDetails = responseEntity.getBody();
 
 376         assertEquals("Error: Bad Request Received", problemDetails.getDetail());
 
 380     public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackagesIs404NotFound() {
 
 381         mockRestServiceServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
 
 382                 .andRespond(withStatus(HttpStatus.NOT_FOUND));
 
 384         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
 
 385         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
 
 387         assertNotNull(responseEntity.getBody());
 
 388         final ProblemDetails problemDetails = responseEntity.getBody();
 
 389         assertEquals("No Vnf Packages found", problemDetails.getDetail());
 
 393     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturns500InternalServerError() {
 
 394         mockRestServiceServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
 
 395                 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
 
 397         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
 
 398         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 400         assertNotNull(responseEntity.getBody());
 
 401         final ProblemDetails problemDetails = responseEntity.getBody();
 
 402         assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
 
 406     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturnsANullPackage() {
 
 407         mockRestServiceServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
 
 408                 .andRespond(withSuccess());
 
 410         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
 
 411         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 413         assertNotNull(responseEntity.getBody());
 
 414         final ProblemDetails problemDetails = responseEntity.getBody();
 
 415         assertEquals("An error occurred, a null response was received by the\n"
 
 416                 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" \n"
 
 417                 + "endpoint.", problemDetails.getDetail());
 
 421     public void testVnfPackageReceivedAsInlineResponse2001IfGetVnfPackageByIdSuccessful() {
 
 422         final VnfPkgInfo response = createVnfPkgInfo(VNF_PACKAGE_ID);
 
 424         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
 
 425                 .andRespond(withSuccess(gson.toJson(response), MediaType.APPLICATION_JSON));
 
 427         final String testURL = localhostUrl + port + VNFPKGM_BASE_URL + "/" + VNF_PACKAGE_ID;
 
 428         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
 
 429         final ResponseEntity<InlineResponse2001> responseEntity = testRestTemplate.withBasicAuth("test", "test")
 
 430                 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001.class);
 
 432         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
 
 433         assertNotNull(responseEntity.getBody());
 
 434         final InlineResponse2001 inlineResponse2001 = responseEntity.getBody();
 
 435         assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
 
 436         assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
 
 437         assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
 
 438         assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
 
 439         assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
 
 440         assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
 
 441         assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
 
 442         assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
 
 443         assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
 
 444         final VnfPackagesLinks links = inlineResponse2001.getLinks();
 
 445         assertNotNull(links);
 
 446         assertEquals(EXPECTED_SELF_HREF, links.getSelf().getHref());
 
 447         assertEquals(EXPECTED_VNFD_HREF, links.getVnfd().getHref());
 
 448         assertEquals(EXPECTED_PACKAGE_CONTENT_HREF, links.getPackageContent().getHref());
 
 453     public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs400BadRequest() {
 
 454         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
 
 455                 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
 
 457         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
 
 459         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
 
 460         assertNotNull(responseEntity.getBody());
 
 461         final ProblemDetails problemDetails = responseEntity.getBody();
 
 462         assertEquals("Error: Bad Request Received", problemDetails.getDetail());
 
 466     public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs404NotFound() {
 
 467         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
 
 468                 .andRespond(withStatus(HttpStatus.NOT_FOUND));
 
 470         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
 
 472         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
 
 473         assertNotNull(responseEntity.getBody());
 
 474         final ProblemDetails problemDetails = responseEntity.getBody();
 
 475         assertEquals("No Vnf Package found with vnfPkgId: " + VNF_PACKAGE_ID, problemDetails.getDetail());
 
 479     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturns500InternalServerError() {
 
 480         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
 
 481                 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
 
 483         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
 
 485         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 486         assertNotNull(responseEntity.getBody());
 
 487         final ProblemDetails problemDetails = responseEntity.getBody();
 
 488         assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
 
 492     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturnsANullPackage() {
 
 493         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
 
 494                 .andRespond(withSuccess());
 
 496         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
 
 497         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 499         assertNotNull(responseEntity.getBody());
 
 500         final ProblemDetails problemDetails = responseEntity.getBody();
 
 501         assertEquals("An error occurred, a null response was received by the\n"
 
 502                 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" by vnfPkgId: \""
 
 503                 + VNF_PACKAGE_ID + "\" \n" + "endpoint.", problemDetails.getDetail());
 
 506     // The below test method is here to improve code coverage and provide a foundation for writing
 
 509     public void testGetPackageVnfd_ValidArray_Success() {
 
 510         final byte[] responseArray = buildByteArrayWithRandomData(10);
 
 512         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
 
 513                 .andExpect(method(HttpMethod.GET))
 
 514                 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
 
 516         final String testURL =
 
 517                 "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/" + VNF_PACKAGE_ID + "/vnfd";
 
 518         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
 
 519         final ResponseEntity<byte[]> responseEntity =
 
 520                 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
 
 522         assertEquals(byte[].class, responseEntity.getBody().getClass());
 
 523         assertArrayEquals(responseEntity.getBody(), responseArray);
 
 524         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
 
 528     public void testOnGetPackageVnfd_Conflict_Fail() {
 
 529         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
 
 530                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
 
 532         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
 
 534         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
 
 535         assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
 
 539     public void testOnGetPackageVnfd_NotFound_Fail() {
 
 540         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
 
 541                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
 
 543         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
 
 545         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
 
 546         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
 
 550     public void testOnGetPackageVnfd_UnauthorizedClient_Fail() {
 
 551         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
 
 552                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
 
 554         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
 
 556         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
 
 557         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 561     public void testOnGetPackageVnfd_InternalServerError_Fail() {
 
 562         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
 
 563                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
 
 565         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
 
 567         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
 
 568         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 572     public void testOnGetPackageVnfd_BadRequest_Fail() {
 
 573         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
 
 574                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
 
 576         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
 
 578         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
 
 579         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 583     public void testOnGetPackageVnfd_UnauthorizedServer_InternalError_Fail() {
 
 584         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
 
 585                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
 
 587         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
 
 589         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
 
 590         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 594     public void testGetPackageVnfd_SuccessResponseFromServerWithNullPackage_Fail() {
 
 595         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
 
 596                 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
 
 598         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
 
 600         assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
 
 601         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
 
 604     // Simply returns a byte array filled with random data, for use in the tests.
 
 605     private byte[] buildByteArrayWithRandomData(final int sizeInKb) {
 
 606         final Random rnd = new Random();
 
 607         final byte[] b = new byte[sizeInKb * 1024]; // converting kb to byte
 
 612     private ResponseEntity<ProblemDetails> sendHttpRequest(final String url) {
 
 613         final String testURL = localhostUrl + port + VNFPKGM_BASE_URL + "/" + url;
 
 614         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
 
 615         return testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request,
 
 616                 ProblemDetails.class);
 
 619     private VnfPkgInfo[] createVnfPkgArray() {
 
 620         final VnfPkgInfo[] vnfPkgInfoArray = new VnfPkgInfo[1];
 
 621         final VnfPkgInfo vnfPkgInfo = createVnfPkgInfo(VNF_PACKAGE_ID);
 
 622         vnfPkgInfoArray[0] = vnfPkgInfo;
 
 623         return vnfPkgInfoArray;
 
 626     private VnfPkgInfo createVnfPkgInfo(final String vnfPackageId) {
 
 627         final VnfPkgInfo vnfPkgInfo = new VnfPkgInfo();
 
 628         vnfPkgInfo.setId(vnfPackageId);
 
 629         vnfPkgInfo.setVnfdId(VNFD_ID);
 
 630         vnfPkgInfo.setVnfProvider(VNF_PROVIDER);
 
 631         vnfPkgInfo.setVnfProductName(VNF_PRODUCT_NAME);
 
 632         vnfPkgInfo.setVnfSoftwareVersion(VNF_SOFTWARE_VERSION);
 
 633         vnfPkgInfo.setVnfdVersion(VNFD_VERSION);
 
 634         vnfPkgInfo.setChecksum(createVnfPkgChecksum());
 
 635         vnfPkgInfo.setSoftwareImages(createSoftwareImages());
 
 636         vnfPkgInfo.setAdditionalArtifacts(createAdditionalArtifacts());
 
 637         vnfPkgInfo.setLinks(createVNFPKGMLinkSerializerLinks());
 
 641     private Checksum createVnfPkgChecksum() {
 
 642         final Checksum checksum = new Checksum();
 
 643         checksum.setAlgorithm(ALGORITHM);
 
 644         checksum.setHash(HASH);
 
 648     private List<VnfPackageSoftwareImageInfo> createSoftwareImages() {
 
 649         final List<VnfPackageSoftwareImageInfo> softwareImages = new ArrayList<>();
 
 650         final VnfPackageSoftwareImageInfo vnfPackageSoftwareImageInfo = new VnfPackageSoftwareImageInfo();
 
 651         vnfPackageSoftwareImageInfo.setId(VNFD_ID);
 
 652         vnfPackageSoftwareImageInfo.setName(VNF_PRODUCT_NAME);
 
 653         vnfPackageSoftwareImageInfo.setProvider("");
 
 654         vnfPackageSoftwareImageInfo.setVersion("");
 
 655         vnfPackageSoftwareImageInfo.setChecksum(createVnfPkgChecksum());
 
 656         vnfPackageSoftwareImageInfo
 
 657                 .setContainerFormat(VnfPackageSoftwareImageInfo.ContainerFormatEnum.fromValue("AKI"));
 
 658         softwareImages.add(vnfPackageSoftwareImageInfo);
 
 659         return softwareImages;
 
 662     private List<VnfPackageArtifactInfo> createAdditionalArtifacts() {
 
 663         final List<VnfPackageArtifactInfo> vnfPackageArtifactInfos = new ArrayList<>();
 
 664         final VnfPackageArtifactInfo vnfPackageArtifactInfo =
 
 665                 new VnfPackageArtifactInfo().artifactPath(ARTIFACT_PATH).checksum(createVnfPkgChecksum());
 
 666         vnfPackageArtifactInfos.add(vnfPackageArtifactInfo);
 
 667         return vnfPackageArtifactInfos;
 
 670     private VNFPKGMLinkSerializer createVNFPKGMLinkSerializerLinks() {
 
 671         final String baseUrl = "http://msb-iag:443/api/vnfpkgm/v1/vnf_packages";
 
 672         return new VNFPKGMLinkSerializer().self(new UriLink().href(baseUrl + "/myVnfPackageId"))
 
 673                 .vnfd(new UriLink().href(baseUrl + "/myVnfPackageId/vnfd"))
 
 674                 .packageContent(new UriLink().href(baseUrl + "/myVnfPackageId/package_content"));