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.assertTrue;
26 import static org.onap.so.adapters.vnfmadapter.Constants.PACKAGE_MANAGEMENT_BASE_URL;
27 import static org.onap.so.client.RestTemplateConfig.CONFIGURABLE_REST_TEMPLATE;
28 import static org.springframework.test.web.client.match.MockRestRequestMatchers.method;
29 import static org.springframework.test.web.client.match.MockRestRequestMatchers.requestTo;
30 import static org.springframework.test.web.client.response.MockRestResponseCreators.withStatus;
31 import static org.springframework.test.web.client.response.MockRestResponseCreators.withSuccess;
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.ProblemDetails;
38 import org.onap.so.configuration.rest.BasicHttpHeadersProvider;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41 import org.springframework.beans.factory.annotation.Autowired;
42 import org.springframework.beans.factory.annotation.Qualifier;
43 import org.springframework.boot.test.context.SpringBootTest;
44 import org.springframework.boot.test.web.client.TestRestTemplate;
45 import org.springframework.boot.web.server.LocalServerPort;
46 import org.springframework.http.HttpEntity;
47 import org.springframework.http.HttpMethod;
48 import org.springframework.http.HttpStatus;
49 import org.springframework.http.MediaType;
50 import org.springframework.http.ResponseEntity;
51 import org.springframework.test.context.ActiveProfiles;
52 import org.springframework.test.context.junit4.SpringRunner;
53 import org.springframework.test.web.client.MockRestServiceServer;
54 import org.springframework.web.client.RestTemplate;
57 * @author gareth.roper@est.tech
59 @RunWith(SpringRunner.class)
60 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
61 @ActiveProfiles("test")
62 public class Sol003PackageManagementControllerTest {
64 private static final Logger logger = LoggerFactory.getLogger(Sol003PackageManagementControllerTest.class);
70 @Qualifier(CONFIGURABLE_REST_TEMPLATE)
71 private RestTemplate testRestTemplate;
74 private Sol003PackageManagementController controller;
77 private TestRestTemplate restTemplate;
79 private static final String VNF_PACKAGE_ID = "myVnfPackageId";
80 private static final String ARTIFACT_PATH = "myArtifactPath";
81 private static final String MSB_BASE_URL = "http://msb_iag.onap:80/api/vnfpkgm/v1/vnf_packages";
82 private static final String VNFPKGM_BASE_URL = PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages";
83 private static final String localhostUrl = "http://localhost:";
85 private MockRestServiceServer mockRestServer;
86 private BasicHttpHeadersProvider basicHttpHeadersProvider;
89 public Sol003PackageManagementControllerTest() {}
93 MockRestServiceServer.MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(testRestTemplate);
94 builder.ignoreExpectOrder(true);
95 mockRestServer = builder.build();
96 basicHttpHeadersProvider = new BasicHttpHeadersProvider();
100 public void testGetPackageContent_ValidArray_Success() {
101 byte[] responseArray = buildByteArrayWithRandomData(10);
103 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
104 .andExpect(method(HttpMethod.GET))
105 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
107 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
108 + VNF_PACKAGE_ID + "/package_content";
109 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
110 final ResponseEntity<byte[]> responseEntity =
111 restTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
113 assertEquals(byte[].class, responseEntity.getBody().getClass());
114 assertArrayEquals(responseEntity.getBody(), responseArray);
115 assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
119 public void testOnGetPackageContent_Conflict_Fail() {
120 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
121 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
123 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
125 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
126 assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
130 public void testOnGetPackageContent_NotFound_Fail() {
131 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
132 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
134 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
136 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
137 assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
141 public void testOnGetPackageContent_UnauthorizedClient_Fail() {
142 final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
143 + VNF_PACKAGE_ID + "/package_content";
144 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
145 final ResponseEntity<ProblemDetails> responseEntity =
146 restTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
148 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
149 assertEquals(HttpStatus.UNAUTHORIZED, responseEntity.getStatusCode());
153 public void testOnGetPackageContent_InternalServerError_Fail() {
154 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
155 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
157 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
159 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
160 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
164 public void testOnGetPackageContent_BadRequest_Fail() {
165 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
166 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
168 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
170 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
171 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
175 public void testOnGetPackageContent_UnauthorizedServer_InternalError_Fail() {
176 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
177 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
179 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
181 assertTrue(responseEntity.getBody() instanceof ProblemDetails);
182 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
186 public void testGetPackageContent_SuccessResponseFromServerWithNullPackage_Fail() {
187 mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
188 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
190 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
192 assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
193 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
196 // The below 4 test methods are here to improve code coverage and provide a foundation for writing future tests
198 public void testGetVnfPackage_Not_Implemented() {
199 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID);
200 assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
204 public void testGetVnfPackages_Not_Implemented() {
205 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest("");
206 assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
210 public void testGetVnfd_Not_Implemented() {
211 final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
212 assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
216 public void testGetArtifact_Not_Implemented() {
217 final ResponseEntity<ProblemDetails> responseEntity =
218 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
219 assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
222 // Simply returns a byte array filled with random data, for use in the tests.
223 private byte[] buildByteArrayWithRandomData(int sizeInKb) {
224 final Random rnd = new Random();
225 final byte[] b = new byte[sizeInKb * 1024]; // converting kb to byte
230 private ResponseEntity<ProblemDetails> sendHttpRequest(String url) {
231 final String testURL = localhostUrl + port + VNFPKGM_BASE_URL + "/" + url;
232 final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
233 return restTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request,
234 ProblemDetails.class);