Merge "SOL003 Adapter Package Management - Fetch VNF Package Content"
[so.git] / adapters / mso-vnfm-adapter / mso-vnfm-etsi-adapter / src / test / java / org / onap / so / adapters / vnfmadapter / rest / Sol003PackageManagementControllerTest.java
1 /*-
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
8  *
9  *      http://www.apache.org/licenses/LICENSE-2.0
10  *
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.
16  *
17  * SPDX-License-Identifier: Apache-2.0
18  * ============LICENSE_END=========================================================
19  */
20
21 package org.onap.so.adapters.vnfmadapter.rest;
22
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;
55
56 /**
57  * @author gareth.roper@est.tech
58  */
59 @RunWith(SpringRunner.class)
60 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
61 @ActiveProfiles("test")
62 public class Sol003PackageManagementControllerTest {
63
64     private static final Logger logger = LoggerFactory.getLogger(Sol003PackageManagementControllerTest.class);
65
66     @LocalServerPort
67     private int port;
68
69     @Autowired
70     @Qualifier(CONFIGURABLE_REST_TEMPLATE)
71     private RestTemplate testRestTemplate;
72
73     @Autowired
74     private Sol003PackageManagementController controller;
75
76     @Autowired
77     private TestRestTemplate restTemplate;
78
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:";
84
85     private MockRestServiceServer mockRestServer;
86     private BasicHttpHeadersProvider basicHttpHeadersProvider;
87
88
89     public Sol003PackageManagementControllerTest() {}
90
91     @Before
92     public void setUp() {
93         MockRestServiceServer.MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(testRestTemplate);
94         builder.ignoreExpectOrder(true);
95         mockRestServer = builder.build();
96         basicHttpHeadersProvider = new BasicHttpHeadersProvider();
97     }
98
99     @Test
100     public void testGetPackageContent_ValidArray_Success() {
101         byte[] responseArray = buildByteArrayWithRandomData(10);
102
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));
106
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);
112
113         assertEquals(byte[].class, responseEntity.getBody().getClass());
114         assertArrayEquals(responseEntity.getBody(), responseArray);
115         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
116     }
117
118     @Test
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));
122
123         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
124
125         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
126         assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
127     }
128
129     @Test
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));
133
134         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
135
136         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
137         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
138     }
139
140     @Test
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);
147
148         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
149         assertEquals(HttpStatus.UNAUTHORIZED, responseEntity.getStatusCode());
150     }
151
152     @Test
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));
156
157         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
158
159         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
160         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
161     }
162
163     @Test
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));
167
168         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
169
170         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
171         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
172     }
173
174     @Test
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));
178
179         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
180
181         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
182         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
183     }
184
185     @Test
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());
189
190         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
191
192         assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
193         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
194     }
195
196     // The below 4 test methods are here to improve code coverage and provide a foundation for writing future tests
197     @Test
198     public void testGetVnfPackage_Not_Implemented() {
199         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID);
200         assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
201     }
202
203     @Test
204     public void testGetVnfPackages_Not_Implemented() {
205         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest("");
206         assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
207     }
208
209     @Test
210     public void testGetVnfd_Not_Implemented() {
211         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
212         assertEquals(HttpStatus.NOT_IMPLEMENTED, responseEntity.getStatusCode());
213     }
214
215     @Test
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());
220     }
221
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
226         rnd.nextBytes(b);
227         return b;
228     }
229
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);
235     }
236 }