Refactor SOL003 Adapter to organize its modules
[so.git] / adapters / etsi-sol003-adapter / etsi-sol003-package-management / etsi-sol003-package-management-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.common.CommonConstants.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.After;
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
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.packagemanagement.model.InlineResponse2001;
48 import org.onap.so.adapters.vnfmadapter.packagemanagement.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;
65
66 /**
67  * @author gareth.roper@est.tech
68  */
69 @RunWith(SpringRunner.class)
70 @SpringBootTest(classes = TestApplication.class, webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
71 @ActiveProfiles("test")
72 public class Sol003PackageManagementControllerTest {
73
74     @LocalServerPort
75     private int port;
76
77     @Autowired
78     @Qualifier(ETSI_CATALOG_REST_TEMPLATE_BEAN)
79     private RestTemplate restTemplate;
80
81     @Autowired
82     private TestRestTemplate testRestTemplate;
83
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";
103
104     private MockRestServiceServer mockRestServiceServer;
105     private BasicHttpHeadersProvider basicHttpHeadersProvider;
106     private final Gson gson = new Gson();
107
108     public Sol003PackageManagementControllerTest() {}
109
110     @Before
111     public void setUp() {
112         final MockRestServiceServer.MockRestServiceServerBuilder builder = MockRestServiceServer.bindTo(restTemplate);
113         builder.ignoreExpectOrder(true);
114         mockRestServiceServer = builder.build();
115         basicHttpHeadersProvider = new BasicHttpHeadersProvider();
116     }
117
118     @After
119     public void after() {
120         mockRestServiceServer.reset();
121     }
122
123     @Test
124     public void testGetPackageContent_ValidArray_Success() {
125         final byte[] responseArray = buildByteArrayWithRandomData(10);
126
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));
130
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);
136
137         assertEquals(byte[].class, responseEntity.getBody().getClass());
138         assertArrayEquals(responseEntity.getBody(), responseArray);
139         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
140     }
141
142     @Test
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));
146
147         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
148
149         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
150         assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
151     }
152
153     @Test
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));
157
158         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
159
160         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
161         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
162     }
163
164     @Test
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";
168
169         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/package_content"))
170                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
171
172         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
173
174
175         final ResponseEntity<ProblemDetails> responseEntity =
176                 testRestTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
177
178         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
179         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
180     }
181
182     @Test
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));
186
187         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
188
189         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
190         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
191     }
192
193     @Test
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));
197
198         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
199
200         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
201         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
202     }
203
204     @Test
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));
208
209         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
210
211         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
212         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
213     }
214
215     @Test
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());
219
220         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/package_content");
221
222         assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
223         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
224     }
225
226     @Test
227     public void testGetPackageArtifact_ValidArray_Success() {
228         final byte[] responseArray = buildByteArrayWithRandomData(10);
229
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));
233
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);
239
240         assertEquals(byte[].class, responseEntity.getBody().getClass());
241         assertArrayEquals(responseEntity.getBody(), responseArray);
242         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
243     }
244
245     @Test
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));
249
250         final ResponseEntity<ProblemDetails> responseEntity =
251                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
252
253         assertNotNull(responseEntity.getBody());
254         assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
255     }
256
257     @Test
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));
261
262         final ResponseEntity<ProblemDetails> responseEntity =
263                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
264
265         assertNotNull(responseEntity.getBody());
266         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
267     }
268
269     @Test
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;
273
274         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH))
275                 .andExpect(method(HttpMethod.GET)).andRespond(withStatus(HttpStatus.UNAUTHORIZED));
276
277         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
278         final ResponseEntity<ProblemDetails> responseEntity =
279                 testRestTemplate.exchange(testURL, HttpMethod.GET, request, ProblemDetails.class);
280
281         assertNotNull(responseEntity.getBody());
282         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
283     }
284
285     @Test
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));
289
290         final ResponseEntity<ProblemDetails> responseEntity =
291                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
292
293         assertNotNull(responseEntity.getBody());
294         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
295     }
296
297     @Test
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));
301
302         final ResponseEntity<ProblemDetails> responseEntity =
303                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
304
305         assertNotNull(responseEntity.getBody());
306         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
307     }
308
309     @Test
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));
313
314         final ResponseEntity<ProblemDetails> responseEntity =
315                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
316
317         assertNotNull(responseEntity.getBody());
318         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
319     }
320
321     @Test
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());
325
326         final ResponseEntity<ProblemDetails> responseEntity =
327                 sendHttpRequest(VNF_PACKAGE_ID + "/artifacts/" + ARTIFACT_PATH);
328
329         assertNotNull(responseEntity.getBody());
330         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
331     }
332
333     @Test
334     public void testVnfPackagesReceivedAsInlineResponse2001ListIfGetVnfPackagesSuccessful() {
335         final VnfPkgInfo[] responses = createVnfPkgArray();
336
337         mockRestServiceServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
338                 .andRespond(withSuccess(gson.toJson(responses), MediaType.APPLICATION_JSON));
339
340         final String testURL = localhostUrl + port + VNFPKGM_BASE_URL;
341         final HttpEntity<?> request = new HttpEntity<>(basicHttpHeadersProvider.getHttpHeaders());
342
343         final ResponseEntity<InlineResponse2001[]> responseEntity = testRestTemplate.withBasicAuth("test", "test")
344                 .exchange(testURL, HttpMethod.GET, request, InlineResponse2001[].class);
345
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());
364     }
365
366     @Test
367     public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackagesIs400BadRequest() {
368         mockRestServiceServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
369                 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
370
371         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
372         assertEquals(HttpStatus.BAD_REQUEST, responseEntity.getStatusCode());
373
374         assertNotNull(responseEntity.getBody());
375         final ProblemDetails problemDetails = responseEntity.getBody();
376         assertEquals("Error: Bad Request Received", problemDetails.getDetail());
377     }
378
379     @Test
380     public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackagesIs404NotFound() {
381         mockRestServiceServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
382                 .andRespond(withStatus(HttpStatus.NOT_FOUND));
383
384         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
385         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
386
387         assertNotNull(responseEntity.getBody());
388         final ProblemDetails problemDetails = responseEntity.getBody();
389         assertEquals("No Vnf Packages found", problemDetails.getDetail());
390     }
391
392     @Test
393     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturns500InternalServerError() {
394         mockRestServiceServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
395                 .andRespond(withStatus(HttpStatus.INTERNAL_SERVER_ERROR));
396
397         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
398         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
399
400         assertNotNull(responseEntity.getBody());
401         final ProblemDetails problemDetails = responseEntity.getBody();
402         assertEquals("Internal Server Error Occurred.", problemDetails.getDetail());
403     }
404
405     @Test
406     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackagesReturnsANullPackage() {
407         mockRestServiceServer.expect(requestTo(MSB_BASE_URL)).andExpect(method(HttpMethod.GET))
408                 .andRespond(withSuccess());
409
410         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGES_URL);
411         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
412
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());
418     }
419
420     @Test
421     public void testVnfPackageReceivedAsInlineResponse2001IfGetVnfPackageByIdSuccessful() {
422         final VnfPkgInfo response = createVnfPkgInfo(VNF_PACKAGE_ID);
423
424         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
425                 .andRespond(withSuccess(gson.toJson(response), MediaType.APPLICATION_JSON));
426
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);
431
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());
449
450     }
451
452     @Test
453     public void test400BadRequestInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs400BadRequest() {
454         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
455                 .andRespond(withStatus(HttpStatus.BAD_REQUEST));
456
457         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
458
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());
463     }
464
465     @Test
466     public void test404NotFoundInfoReceivedAsProblemDetailsIfGetVnfPackageByIdIs404NotFound() {
467         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
468                 .andRespond(withStatus(HttpStatus.NOT_FOUND));
469
470         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
471
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());
476     }
477
478     @Test
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));
482
483         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
484
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());
489     }
490
491     @Test
492     public void test500InternalServerErrorProblemDetailsReceivedIfGetVnfPackageByIdReturnsANullPackage() {
493         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID)).andExpect(method(HttpMethod.GET))
494                 .andRespond(withSuccess());
495
496         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(GET_VNF_PACKAGE_BY_ID_URL);
497         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
498
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());
504     }
505
506     // The below test method is here to improve code coverage and provide a foundation for writing
507     // future tests
508     @Test
509     public void testGetPackageVnfd_ValidArray_Success() {
510         final byte[] responseArray = buildByteArrayWithRandomData(10);
511
512         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
513                 .andExpect(method(HttpMethod.GET))
514                 .andRespond(withSuccess(responseArray, MediaType.APPLICATION_OCTET_STREAM));
515
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);
521
522         assertEquals(byte[].class, responseEntity.getBody().getClass());
523         assertArrayEquals(responseEntity.getBody(), responseArray);
524         assertEquals(HttpStatus.OK, responseEntity.getStatusCode());
525     }
526
527     @Test
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));
531
532         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
533
534         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
535         assertEquals(HttpStatus.CONFLICT, responseEntity.getStatusCode());
536     }
537
538     @Test
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));
542
543         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
544
545         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
546         assertEquals(HttpStatus.NOT_FOUND, responseEntity.getStatusCode());
547     }
548
549     @Test
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));
553
554         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
555
556         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
557         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
558     }
559
560     @Test
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));
564
565         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
566
567         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
568         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
569     }
570
571     @Test
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));
575
576         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
577
578         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
579         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
580     }
581
582     @Test
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));
586
587         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
588
589         assertTrue(responseEntity.getBody() instanceof ProblemDetails);
590         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
591     }
592
593     @Test
594     public void testGetPackageVnfd_SuccessResponseFromServerWithNullPackage_Fail() {
595         mockRestServiceServer.expect(requestTo(MSB_BASE_URL + "/" + VNF_PACKAGE_ID + "/vnfd"))
596                 .andExpect(method(HttpMethod.GET)).andRespond(withSuccess());
597
598         final ResponseEntity<ProblemDetails> responseEntity = sendHttpRequest(VNF_PACKAGE_ID + "/vnfd");
599
600         assertEquals(ProblemDetails.class, responseEntity.getBody().getClass());
601         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR, responseEntity.getStatusCode());
602     }
603
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
608         rnd.nextBytes(b);
609         return b;
610     }
611
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);
617     }
618
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;
624     }
625
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());
638         return vnfPkgInfo;
639     }
640
641     private Checksum createVnfPkgChecksum() {
642         final Checksum checksum = new Checksum();
643         checksum.setAlgorithm(ALGORITHM);
644         checksum.setHash(HASH);
645         return checksum;
646     }
647
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;
660     }
661
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;
668     }
669
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"));
675     }
676
677 }