Add HTTPS support between SOL003 Adapter and ETSI
[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.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.adapters.vnfmadapter.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.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;
64
65 /**
66  * @author gareth.roper@est.tech
67  */
68 @RunWith(SpringRunner.class)
69 @SpringBootTest(classes = VnfmAdapterApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
70 @ActiveProfiles("test")
71 public class Sol003PackageManagementControllerTest {
72
73     @LocalServerPort
74     private int port;
75
76     @Autowired
77     @Qualifier(ETSI_CATALOG_REST_TEMPLATE_BEAN)
78     private RestTemplate restTemplate;
79
80     @Autowired
81     private TestRestTemplate testRestTemplate;
82
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";
98
99     private MockRestServiceServer mockRestServer;
100     private BasicHttpHeadersProvider basicHttpHeadersProvider;
101     private final Gson gson = new Gson();
102
103     public Sol003PackageManagementControllerTest() {}
104
105     @Before
106     public void setUp() {
107         final MockRestServiceServer.MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(restTemplate);
108         builder.ignoreExpectOrder(true);
109         mockRestServer = builder.build();
110         basicHttpHeadersProvider = new BasicHttpHeadersProvider();
111     }
112
113     @Test
114     public void testGetPackageContent_ValidArray_Success() {
115         final byte[] responseArray = buildByteArrayWithRandomData(10);
116
117         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
118                 .andExpect(method(HttpMethod.GET))
119                 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
120
121         final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
122                 + VNF_PACKAGE_ID + "/package_content";
123         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
124         final ResponseEntity<byte[]> responseEntity =
125                 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
126
127         assertEquals(byte[].class, responseEntity.getBody().getClass());
128         assertArrayEquals(responseEntity.getBody(), responseArray);
129         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
130     }
131
132     @Test
133     public void testOnGetPackageContent_Conflict_Fail() {
134         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
135                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
136
137         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
138
139         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
140         assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
141     }
142
143     @Test
144     public void testOnGetPackageContent_NotFound_Fail() {
145         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
146                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
147
148         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
149
150         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
151         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
152     }
153
154     @Test
155     public void testOnGetPackageContent_UnauthorizedClient_Fail() {
156         final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
157                 + VNF_PACKAGE_ID + "/package_content";
158
159         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
160                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
161
162         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
163
164
165         final ResponseEntity<ProblemDetails> responseEntity =
166                 testRestTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
167
168         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
169         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
170     }
171
172     @Test
173     public void testOnGetPackageContent_InternalServerError_Fail() {
174         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
175                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
176
177         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
178
179         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
180         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
181     }
182
183     @Test
184     public void testOnGetPackageContent_BadRequest_Fail() {
185         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
186                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
187
188         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
189
190         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
191         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
192     }
193
194     @Test
195     public void testOnGetPackageContent_UnauthorizedServer_InternalError_Fail() {
196         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
197                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
198
199         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
200
201         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
202         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
203     }
204
205     @Test
206     public void testGetPackageContent_SuccessResponseFromServerWithNullPackage_Fail() {
207         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
208                 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
209
210         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
211
212         assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
213         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
214     }
215
216     @Test
217     public void testGetPackageArtifact_ValidArray_Success() {
218         final byte[] responseArray = buildByteArrayWithRandomData(10);
219
220         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
221                 .andExpect(method(HttpMethod.GET))
222                 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
223
224         final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
225                 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
226         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
227         final ResponseEntity<byte[]> responseEntity =
228                 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
229
230         assertEquals(byte[].class, responseEntity.getBody().getClass());
231         assertArrayEquals(responseEntity.getBody(), responseArray);
232         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
233     }
234
235     @Test
236     public void testOnGetPackageArtifact_Conflict_Fail() {
237         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
238                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.CONFLICT));
239
240         final ResponseEntity<ProblemDetails> responseEntity =
241                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
242
243         assertNotNull(responseEntity.getBody());
244         assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
245     }
246
247     @Test
248     public void testOnGetPackageArtifact_NotFound_Fail() {
249         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
250                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.NOT_FOUND));
251
252         final ResponseEntity<ProblemDetails> responseEntity =
253                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
254
255         assertNotNull(responseEntity.getBody());
256         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
257     }
258
259     @Test
260     public void testOnGetPackageArtifact_UnauthorizedClient_Fail() {
261         final String testURL = "http://localhost:" + port + PACKAGE_MANAGEMENT_BASE_URL + "/vnf_packages/"
262                 + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH;
263
264         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
265                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
266
267         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
268         final ResponseEntity<ProblemDetails> responseEntity =
269                 testRestTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
270
271         assertNotNull(responseEntity.getBody());
272         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
273     }
274
275     @Test
276     public void testOnGetPackageArtifact_InternalServerError_Fail() {
277         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
278                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
279
280         final ResponseEntity<ProblemDetails> responseEntity =
281                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
282
283         assertNotNull(responseEntity.getBody());
284         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
285     }
286
287     @Test
288     public void testOnGetPackageArtifact_BadRequest_Fail() {
289         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
290                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.BAD_REQUEST));
291
292         final ResponseEntity<ProblemDetails> responseEntity =
293                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
294
295         assertNotNull(responseEntity.getBody());
296         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
297     }
298
299     @Test
300     public void testOnGetPackageArtifact_UnauthorizedServer_InternalError_Fail() {
301         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
302                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
303
304         final ResponseEntity<ProblemDetails> responseEntity =
305                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
306
307         assertNotNull(responseEntity.getBody());
308         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
309     }
310
311     @Test
312     public void testGetPackageArtifact_SuccessResponseFromServerWithNullPackage_Fail() {
313         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
314                 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
315
316         final ResponseEntity<ProblemDetails> responseEntity =
317                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
318
319         assertNotNull(responseEntity.getBody());
320         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
321     }
322
323     @Test
324     public void testVnfPackagesReceivedAsInlineResponse2001ListIfGetVnfPackagesSuccessful() {
325         final VnfPkgInfo[] responses = createVnfPkgArray();
326
327         mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
328                 .andRespond(withSuccess(gson.toJson(responses), MediaType.APPLICATION_JSON));
329
330         final String testURL = localhostUrl + port + VNFPKGM_BASE_URL;
331         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
332
333         final ResponseEntity<InlineResponse2001[]> responseEntity = testRestTemplate.withBasicAuth("test", "test")
334                 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001[].class);
335
336         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
337         assertNotNull(responseEntity.getBody());
338         final InlineResponse2001[] inlineResponse2001array = responseEntity.getBody();
339         final InlineResponse2001 inlineResponse2001 = inlineResponse2001array[0];
340         assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
341         assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
342         assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
343         assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
344         assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
345         assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
346         assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
347         assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
348         assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
349         assertEquals(URI_HREF, inlineResponse2001.getLinks().getSelf().getHref());
350     }
351
352     @Test
353     public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackagesIs400BadRequest() {
354         mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
355                 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
356
357         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
358         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
359
360         assertNotNull(responseEntity.getBody());
361         final ProblemDetails problemDetails = responseEntity.getBody();
362         assertEquals("Error: Bad Request Received", problemDetails.getDetail());
363     }
364
365     @Test
366     public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackagesIs404NotFound() {
367         mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
368                 .andRespond(withStatus(HttpStatus.NOT_FOUND));
369
370         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
371         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
372
373         assertNotNull(responseEntity.getBody());
374         final ProblemDetails problemDetails = responseEntity.getBody();
375         assertEquals("No Vnf Packages found", problemDetails.getDetail());
376     }
377
378     @Test
379     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturns500InternalServerError() {
380         mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
381                 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
382
383         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
384         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
385
386         assertNotNull(responseEntity.getBody());
387         final ProblemDetails problemDetails = responseEntity.getBody();
388         assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
389     }
390
391     @Test
392     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturnsANullPackage() {
393         mockRestServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
394
395         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
396         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
397
398         assertNotNull(responseEntity.getBody());
399         final ProblemDetails problemDetails = responseEntity.getBody();
400         assertEquals("An error occurred, a null response was received by the\n"
401                 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" \n"
402                 + "endpoint.", problemDetails.getDetail());
403     }
404
405     @Test
406     public void testVnfPackageReceivedAsInlineResponse2001IfGetVnfPackageByIdSuccessful() {
407         final VnfPkgInfo response = createVnfPkgInfo(VNF_PACKAGE_ID);
408
409         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
410                 .andRespond(withSuccess(gson.toJson(response), MediaType.APPLICATION_JSON));
411
412         final String testURL = localhostUrl + port + VNFPKGM_BASE_URL + "/" + VNF_PACKAGE_ID;
413         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
414         final ResponseEntity<InlineResponse2001> responseEntity = testRestTemplate.withBasicAuth("test", "test")
415                 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001.class);
416
417         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
418         assertNotNull(responseEntity.getBody());
419         final InlineResponse2001 inlineResponse2001 = responseEntity.getBody();
420         assertEquals(VNF_PACKAGE_ID, inlineResponse2001.getId());
421         assertEquals(VNFD_ID, inlineResponse2001.getVnfdId());
422         assertEquals(VNFD_ID, inlineResponse2001.getSoftwareImages().get(0).getId());
423         assertEquals(VNF_PRODUCT_NAME, inlineResponse2001.getSoftwareImages().get(0).getName());
424         assertEquals(ALGORITHM, inlineResponse2001.getChecksum().getAlgorithm());
425         assertEquals(HASH, inlineResponse2001.getChecksum().getHash());
426         assertEquals(ARTIFACT_PATH, inlineResponse2001.getAdditionalArtifacts().get(0).getArtifactPath());
427         assertEquals(ALGORITHM, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getAlgorithm());
428         assertEquals(HASH, inlineResponse2001.getAdditionalArtifacts().get(0).getChecksum().getHash());
429         assertEquals(URI_HREF, inlineResponse2001.getLinks().getSelf().getHref());
430     }
431
432     @Test
433     public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs400BadRequest() {
434         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
435                 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
436
437         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
438
439         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
440         assertNotNull(responseEntity.getBody());
441         final ProblemDetails problemDetails = responseEntity.getBody();
442         assertEquals("Error: Bad Request Received", problemDetails.getDetail());
443     }
444
445     @Test
446     public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs404NotFound() {
447         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
448                 .andRespond(withStatus(HttpStatus.NOT_FOUND));
449
450         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
451
452         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
453         assertNotNull(responseEntity.getBody());
454         final ProblemDetails problemDetails = responseEntity.getBody();
455         assertEquals("No Vnf Package found with vnfPkgId: " + VNF_PACKAGE_ID, problemDetails.getDetail());
456     }
457
458     @Test
459     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturns500InternalServerError() {
460         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
461                 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
462
463         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
464
465         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
466         assertNotNull(responseEntity.getBody());
467         final ProblemDetails problemDetails = responseEntity.getBody();
468         assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
469     }
470
471     @Test
472     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturnsANullPackage() {
473         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
474                 .andRespond(withSuccess());
475
476         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
477         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
478
479         assertNotNull(responseEntity.getBody());
480         final ProblemDetails problemDetails = responseEntity.getBody();
481         assertEquals("An error occurred, a null response was received by the\n"
482                 + " Sol003PackageManagementController from the EtsiCatalogManager using the GET \"vnf_packages\" by vnfPkgId: \""
483                 + VNF_PACKAGE_ID + "\" \n" + "endpoint.", problemDetails.getDetail());
484     }
485
486     // The below test method is here to improve code coverage and provide a foundation for writing
487     // future tests
488     @Test
489     public void testGetPackageVnfd_ValidArray_Success() {
490         final byte[] responseArray = buildByteArrayWithRandomData(10);
491
492         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
493                 .andExpect(method(HttpMethod.GET))
494                 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
495
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                 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request, byte[].class);
501
502         assertEquals(byte[].class, responseEntity.getBody().getClass());
503         assertArrayEquals(responseEntity.getBody(), responseArray);
504         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
505     }
506
507     @Test
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));
511
512         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
513
514         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
515         assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
516     }
517
518     @Test
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));
522
523         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
524
525         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
526         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
527     }
528
529     @Test
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));
533
534         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
535
536         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
537         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
538     }
539
540     @Test
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));
544
545         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
546
547         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
548         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
549     }
550
551     @Test
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));
555
556         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
557
558         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
559         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
560     }
561
562     @Test
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));
566
567         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
568
569         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
570         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
571     }
572
573     @Test
574     public void testGetPackageVnfd_SuccessResponseFromServerWithNullPackage_Fail() {
575         mockRestServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
576                 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
577
578         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
579
580         assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
581         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
582     }
583
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
588         rnd.nextBytes(b);
589         return b;
590     }
591
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 testRestTemplate.withBasicAuth("test", "test").exchange(testURL, HttpMethod.GET, request,
596                 ProblemDetails.class);
597     }
598
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;
604     }
605
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());
618         return vnfPkgInfo;
619     }
620
621     private Checksum createVnfPkgChecksum() {
622         final Checksum checksum = new Checksum();
623         checksum.setAlgorithm(ALGORITHM);
624         checksum.setHash(HASH);
625         return checksum;
626     }
627
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;
640     }
641
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;
648     }
649
650     private VNFPKGMLinkSerializer createVNFPKGMLinkSerializerLinks() {
651         final UriLink uriLink = new UriLink().href(URI_HREF);
652         final VNFPKGMLinkSerializer vnfpkgmLinkSerializer = new VNFPKGMLinkSerializer().self(uriLink);
653         return vnfpkgmLinkSerializer;
654     }
655
656 }