2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2019 Nokia. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.distribution.servlet;
23 import fj.data.Either;
24 import org.apache.commons.text.StrSubstitutor;
25 import org.apache.http.HttpStatus;
26 import org.glassfish.hk2.utilities.binding.AbstractBinder;
27 import org.glassfish.jersey.server.ResourceConfig;
28 import org.glassfish.jersey.test.JerseyTest;
29 import org.junit.Before;
30 import org.junit.BeforeClass;
31 import org.junit.Test;
32 import org.mockito.Mockito;
33 import org.openecomp.sdc.be.components.impl.ArtifactsBusinessLogic;
34 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
35 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
36 import org.openecomp.sdc.be.config.ConfigurationManager;
37 import org.openecomp.sdc.be.config.SpringConfig;
38 import org.openecomp.sdc.be.dao.api.ActionStatus;
39 import org.openecomp.sdc.be.impl.ComponentsUtils;
40 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
41 import org.openecomp.sdc.be.user.UserBusinessLogic;
42 import org.openecomp.sdc.common.api.ConfigurationSource;
43 import org.openecomp.sdc.common.api.Constants;
44 import org.openecomp.sdc.common.impl.ExternalConfiguration;
45 import org.openecomp.sdc.common.impl.FSConfigurationSource;
46 import org.openecomp.sdc.exception.ResponseFormat;
47 import org.springframework.context.ApplicationContext;
48 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
49 import org.springframework.web.context.WebApplicationContext;
51 import javax.servlet.ServletContext;
52 import javax.servlet.http.HttpServletRequest;
53 import javax.ws.rs.core.Application;
54 import javax.ws.rs.core.MediaType;
55 import javax.ws.rs.core.Response;
56 import java.util.HashMap;
58 import java.util.UUID;
60 import static org.assertj.core.api.Assertions.assertThat;
61 import static org.junit.Assert.assertTrue;
62 import static org.mockito.ArgumentMatchers.any;
63 import static org.mockito.ArgumentMatchers.eq;
64 import static org.mockito.Mockito.reset;
65 import static org.mockito.Mockito.when;
66 import static org.openecomp.sdc.TestUtils.downloadedPayloadMatchesExpected;
68 public class DistributionCatalogServletTest extends JerseyTest {
70 private static final HttpServletRequest HTTP_SERVLET_REQUEST = Mockito.mock(HttpServletRequest.class);
71 private static final UserBusinessLogic USER_BUSINESS_LOGIC = Mockito.mock(UserBusinessLogic.class);
72 private static final ArtifactsBusinessLogic ARTIFACTS_BUSINESS_LOGIC = Mockito.mock(ArtifactsBusinessLogic.class);
73 private static final ServletContext SERVLET_CONTEXT = Mockito.mock(ServletContext.class);
74 private static final WebAppContextWrapper WEB_APP_CONTEXT_WRAPPER = Mockito.mock(WebAppContextWrapper.class);
75 private static final WebApplicationContext WEB_APPLICATION_CONTEXT = Mockito.mock(WebApplicationContext.class);
76 private static final ComponentsUtils COMPONENT_UTILS = Mockito.mock(ComponentsUtils.class);
77 private static final ResponseFormat OK_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_OK);
78 private static final ResponseFormat GENERAL_ERROR_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_INTERNAL_SERVER_ERROR);
79 private static final ResponseFormat CREATED_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_CREATED);
80 private static final ResponseFormat NO_CONTENT_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_NO_CONTENT);
81 private static final ResponseFormat UNAUTHORIZED_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_UNAUTHORIZED);
82 private static final ResponseFormat NOT_FOUND_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_NOT_FOUND);
83 private static final ResponseFormat BAD_REQUEST_RESPONSE_FORMAT = new ResponseFormat(HttpStatus.SC_BAD_REQUEST);
84 private static final String SERVICE_VERSION = "serviceVersion";
85 private static final String ARTIFACT_NAME = "artifactName";
86 private static final String SERVICE_NAME = "serviceName";
87 private static final String RESOURCE_NAME = "resourceName";
88 private static final String RESOURCE_VERSION = "resourceVersion";
89 private static final String RESOURCE_INSTANCE_NAME = "resourceInstanceName";
90 private static final byte[] BYTE_ARRAY = new byte[]{0xA, 0xB, 0xC, 0xD};
93 public static void setup() {
94 when(SERVLET_CONTEXT.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(WEB_APP_CONTEXT_WRAPPER);
95 when(WEB_APP_CONTEXT_WRAPPER.getWebAppContext(SERVLET_CONTEXT)).thenReturn(WEB_APPLICATION_CONTEXT);
97 setUpResponseFormatsForMocks();
98 setUpMockTestConfiguration();
101 private static void setUpMockTestConfiguration() {
102 String appConfigDir = "src/test/resources/config";
103 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
104 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
106 org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration();
107 configuration.setJanusGraphInMemoryGraph(true);
109 configurationManager.setConfiguration(configuration);
110 ExternalConfiguration.setAppName("catalog-be");
113 private static void setUpResponseFormatsForMocks() {
114 when(COMPONENT_UTILS.getResponseFormat(ActionStatus.RESTRICTED_OPERATION)).thenReturn(UNAUTHORIZED_RESPONSE_FORMAT);
115 when(COMPONENT_UTILS.getResponseFormat(ActionStatus.OK)).thenReturn(OK_RESPONSE_FORMAT);
116 when(COMPONENT_UTILS.getResponseFormat(ActionStatus.CREATED)).thenReturn(CREATED_RESPONSE_FORMAT);
117 when(COMPONENT_UTILS.getResponseFormat(ActionStatus.NO_CONTENT)).thenReturn(NO_CONTENT_RESPONSE_FORMAT);
118 when(COMPONENT_UTILS.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(BAD_REQUEST_RESPONSE_FORMAT);
119 when(COMPONENT_UTILS.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(GENERAL_ERROR_RESPONSE_FORMAT);
120 when(COMPONENT_UTILS.getResponseFormat(any(ComponentException.class)))
121 .thenReturn(GENERAL_ERROR_RESPONSE_FORMAT);
122 when(COMPONENT_UTILS.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), any())).thenReturn(NOT_FOUND_RESPONSE_FORMAT);
123 when(COMPONENT_UTILS.getResponseFormat(eq(ActionStatus.COMPONENT_VERSION_NOT_FOUND), any())).thenReturn(NOT_FOUND_RESPONSE_FORMAT);
124 when(COMPONENT_UTILS.getResponseFormat(eq(ActionStatus.COMPONENT_INSTANCE_NOT_FOUND), any())).thenReturn(NOT_FOUND_RESPONSE_FORMAT);
125 when(COMPONENT_UTILS.getResponseFormat(eq(ActionStatus.EXT_REF_NOT_FOUND), any())).thenReturn(NOT_FOUND_RESPONSE_FORMAT);
126 when(COMPONENT_UTILS.getResponseFormat(eq(ActionStatus.MISSING_X_ECOMP_INSTANCE_ID), any())).thenReturn(BAD_REQUEST_RESPONSE_FORMAT);
127 ByResponseFormatComponentException ce = Mockito.mock(ByResponseFormatComponentException.class);
128 when(ce.getResponseFormat()).thenReturn(UNAUTHORIZED_RESPONSE_FORMAT);
132 public void resetSomeMocks() {
133 reset(ARTIFACTS_BUSINESS_LOGIC);
137 public void downloadServiceArtifactMissingInstanceIdHeaderTest() {
138 Map<String, String> parametersMap = new HashMap<>();
139 parametersMap.put(SERVICE_NAME, SERVICE_NAME);
140 parametersMap.put(SERVICE_VERSION, SERVICE_VERSION);
141 parametersMap.put(ARTIFACT_NAME, ARTIFACT_NAME);
143 String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/artifacts/{artifactName}";
144 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}");
146 Response response = target()
149 .accept(MediaType.APPLICATION_OCTET_STREAM)
152 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_BAD_REQUEST);
156 public void downloadServiceArtifactNoArtifactFoundTest() {
157 String serviceName = SERVICE_NAME;
158 String serviceVersion = SERVICE_VERSION;
159 String artifactName = ARTIFACT_NAME;
161 Map<String, String> parametersMap = new HashMap<>();
162 parametersMap.put(SERVICE_NAME, serviceName);
163 parametersMap.put(SERVICE_VERSION, serviceVersion);
164 parametersMap.put(ARTIFACT_NAME, artifactName);
166 String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/artifacts/{artifactName}";
167 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}");
169 Either<byte[], ResponseFormat> downloadServiceArtifactEither = Either.right(NOT_FOUND_RESPONSE_FORMAT);
171 when(ARTIFACTS_BUSINESS_LOGIC.downloadServiceArtifactByNames(serviceName, serviceVersion, artifactName))
172 .thenReturn(downloadServiceArtifactEither);
174 Response response = target()
177 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString())
178 .accept(MediaType.APPLICATION_OCTET_STREAM)
181 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NOT_FOUND);
185 public void downloadServiceArtifactExceptionDuringProcessingTest() {
186 String serviceName = SERVICE_NAME;
187 String serviceVersion = SERVICE_VERSION;
188 String artifactName = ARTIFACT_NAME;
190 Map<String, String> parametersMap = new HashMap<>();
191 parametersMap.put(SERVICE_NAME, serviceName);
192 parametersMap.put(SERVICE_VERSION, serviceVersion);
193 parametersMap.put(ARTIFACT_NAME, artifactName);
195 String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/artifacts/{artifactName}";
196 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}");
198 when(ARTIFACTS_BUSINESS_LOGIC.downloadServiceArtifactByNames(serviceName, serviceVersion, artifactName))
199 .thenThrow(new RuntimeException("Test exception: downloadServiceArtifact"));
201 Response response = target()
204 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString())
205 .accept(MediaType.APPLICATION_OCTET_STREAM)
208 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
212 public void downloadServiceArtifactTest() {
213 String serviceName = SERVICE_NAME;
214 String serviceVersion = SERVICE_VERSION;
215 String artifactName = ARTIFACT_NAME;
217 Map<String, String> parametersMap = new HashMap<>();
218 parametersMap.put(SERVICE_NAME, serviceName);
219 parametersMap.put(SERVICE_VERSION, serviceVersion);
220 parametersMap.put(ARTIFACT_NAME, artifactName);
222 String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/artifacts/{artifactName}";
223 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}");
225 Either<byte[], ResponseFormat> downloadServiceArtifactEither = Either.left(BYTE_ARRAY);
226 when(ARTIFACTS_BUSINESS_LOGIC.downloadServiceArtifactByNames(serviceName, serviceVersion, artifactName))
227 .thenReturn(downloadServiceArtifactEither);
229 Response response = target()
232 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString())
233 .accept(MediaType.APPLICATION_OCTET_STREAM)
236 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
237 assertTrue(response.getHeaders().containsKey(Constants.CONTENT_DISPOSITION_HEADER));
238 assertTrue(downloadedPayloadMatchesExpected(response, BYTE_ARRAY));
242 public void downloadResouceArtifactNoArtifactFoundTest() {
243 String serviceName = SERVICE_NAME;
244 String serviceVersion = SERVICE_VERSION;
245 String resourceName = RESOURCE_NAME;
246 String resourceVersion = RESOURCE_VERSION;
247 String artifactName = ARTIFACT_NAME;
249 Map<String, String> parametersMap = new HashMap<>();
250 parametersMap.put(SERVICE_NAME, serviceName);
251 parametersMap.put(SERVICE_VERSION, serviceVersion);
252 parametersMap.put(RESOURCE_NAME, resourceName);
253 parametersMap.put(RESOURCE_VERSION, resourceVersion);
254 parametersMap.put(ARTIFACT_NAME, artifactName);
256 String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/resources/{resourceName}/{resourceVersion}/artifacts/{artifactName}";
257 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}");
259 Either<byte[], ResponseFormat> downloadResourceArtifactEither = Either.right(NOT_FOUND_RESPONSE_FORMAT);
260 when(ARTIFACTS_BUSINESS_LOGIC.downloadRsrcArtifactByNames(serviceName, serviceVersion, resourceName,
261 resourceVersion, artifactName))
262 .thenReturn(downloadResourceArtifactEither);
264 Response response = target()
267 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString())
268 .accept(MediaType.APPLICATION_OCTET_STREAM)
271 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NOT_FOUND);
275 public void downloadResouceArtifactExceptionDuringProcessingTest() {
276 String serviceName = SERVICE_NAME;
277 String serviceVersion = SERVICE_VERSION;
278 String resourceName = RESOURCE_NAME;
279 String resourceVersion = RESOURCE_VERSION;
280 String artifactName = ARTIFACT_NAME;
282 Map<String, String> parametersMap = new HashMap<>();
283 parametersMap.put(SERVICE_NAME, serviceName);
284 parametersMap.put(SERVICE_VERSION, serviceVersion);
285 parametersMap.put(RESOURCE_NAME, resourceName);
286 parametersMap.put(RESOURCE_VERSION, resourceVersion);
287 parametersMap.put(ARTIFACT_NAME, artifactName);
289 String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/resources/{resourceName}/{resourceVersion}/artifacts/{artifactName}";
290 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}");
292 when(ARTIFACTS_BUSINESS_LOGIC.downloadRsrcArtifactByNames(serviceName, serviceVersion, resourceName,
293 resourceVersion, artifactName))
294 .thenThrow(new RuntimeException("Test exception: downloadResouceArtifact"));
296 Response response = target()
299 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString())
300 .accept(MediaType.APPLICATION_OCTET_STREAM)
303 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
307 public void downloadResouceArtifactTest() {
308 String serviceName = SERVICE_NAME;
309 String serviceVersion = SERVICE_VERSION;
310 String resourceName = RESOURCE_NAME;
311 String resourceVersion = RESOURCE_VERSION;
312 String artifactName = ARTIFACT_NAME;
314 Map<String, String> parametersMap = new HashMap<>();
315 parametersMap.put(SERVICE_NAME, serviceName);
316 parametersMap.put(SERVICE_VERSION, serviceVersion);
317 parametersMap.put(RESOURCE_NAME, resourceName);
318 parametersMap.put(RESOURCE_VERSION, resourceVersion);
319 parametersMap.put(ARTIFACT_NAME, artifactName);
321 String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/resources/{resourceName}/{resourceVersion}/artifacts/{artifactName}";
322 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}");
324 Either<byte[], ResponseFormat> downloadResourceArtifactEither = Either.left(BYTE_ARRAY);
325 when(ARTIFACTS_BUSINESS_LOGIC.downloadRsrcArtifactByNames(serviceName, serviceVersion, resourceName,
326 resourceVersion, artifactName))
327 .thenReturn(downloadResourceArtifactEither);
329 Response response = target()
332 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString())
333 .accept(MediaType.APPLICATION_OCTET_STREAM)
336 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
337 assertTrue(response.getHeaders().containsKey(Constants.CONTENT_DISPOSITION_HEADER));
338 assertTrue(downloadedPayloadMatchesExpected(response, BYTE_ARRAY));
342 public void downloadResourceInstanceArtifactNoArtifactFoundTest() {
343 String serviceName = SERVICE_NAME;
344 String serviceVersion = SERVICE_VERSION;
345 String resourceInstanceName = RESOURCE_INSTANCE_NAME;
346 String artifactName = ARTIFACT_NAME;
348 Map<String, String> parametersMap = new HashMap<>();
349 parametersMap.put(SERVICE_NAME, serviceName);
350 parametersMap.put(SERVICE_VERSION, serviceVersion);
351 parametersMap.put(RESOURCE_INSTANCE_NAME, resourceInstanceName);
352 parametersMap.put(ARTIFACT_NAME, artifactName);
354 String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/resourceInstances/{resourceInstanceName}/artifacts/{artifactName}";
355 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}");
357 Either<byte[], ResponseFormat> downloadResourceArtifactEither = Either.right(NOT_FOUND_RESPONSE_FORMAT);
358 when(ARTIFACTS_BUSINESS_LOGIC.downloadRsrcInstArtifactByNames(serviceName, serviceVersion, resourceInstanceName,
360 .thenReturn(downloadResourceArtifactEither);
362 Response response = target()
365 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString())
366 .accept(MediaType.APPLICATION_OCTET_STREAM)
369 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_NOT_FOUND);
373 public void downloadResourceInstanceArtifactExceptionDuringProcessingTest() {
374 String serviceName = SERVICE_NAME;
375 String serviceVersion = SERVICE_VERSION;
376 String resourceInstanceName = RESOURCE_INSTANCE_NAME;
377 String artifactName = ARTIFACT_NAME;
379 Map<String, String> parametersMap = new HashMap<>();
380 parametersMap.put(SERVICE_NAME, serviceName);
381 parametersMap.put(SERVICE_VERSION, serviceVersion);
382 parametersMap.put(RESOURCE_INSTANCE_NAME, resourceInstanceName);
383 parametersMap.put(ARTIFACT_NAME, artifactName);
385 String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/resourceInstances/{resourceInstanceName}/artifacts/{artifactName}";
386 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}");
388 when(ARTIFACTS_BUSINESS_LOGIC.downloadRsrcInstArtifactByNames(serviceName, serviceVersion, resourceInstanceName,
390 .thenThrow(new RuntimeException("Test exception: ownloadResourceInstanceArtifact"));
392 Response response = target()
395 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString())
396 .accept(MediaType.APPLICATION_OCTET_STREAM)
399 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_INTERNAL_SERVER_ERROR);
403 public void downloadResourceInstanceArtifactTest() {
404 String serviceName = SERVICE_NAME;
405 String serviceVersion = SERVICE_VERSION;
406 String resourceInstanceName = RESOURCE_INSTANCE_NAME;
407 String artifactName = ARTIFACT_NAME;
409 Map<String, String> parametersMap = new HashMap<>();
410 parametersMap.put(SERVICE_NAME, serviceName);
411 parametersMap.put(SERVICE_VERSION, serviceVersion);
412 parametersMap.put(RESOURCE_INSTANCE_NAME, resourceInstanceName);
413 parametersMap.put(ARTIFACT_NAME, artifactName);
415 String formatEndpoint = "/v1/catalog/services/{serviceName}/{serviceVersion}/resourceInstances/{resourceInstanceName}/artifacts/{artifactName}";
416 String path = StrSubstitutor.replace(formatEndpoint, parametersMap, "{", "}");
418 Either<byte[], ResponseFormat> downloadResourceArtifactEither = Either.left(BYTE_ARRAY);
419 when(ARTIFACTS_BUSINESS_LOGIC.downloadRsrcInstArtifactByNames(serviceName, serviceVersion, resourceInstanceName,
421 .thenReturn(downloadResourceArtifactEither);
423 Response response = target()
426 .header(Constants.X_ECOMP_INSTANCE_ID_HEADER, UUID.randomUUID().toString())
427 .accept(MediaType.APPLICATION_OCTET_STREAM)
430 assertThat(response.getStatus()).isEqualTo(HttpStatus.SC_OK);
431 assertTrue(response.getHeaders().containsKey(Constants.CONTENT_DISPOSITION_HEADER));
432 assertTrue(downloadedPayloadMatchesExpected(response, BYTE_ARRAY));
436 protected Application configure() {
437 ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
438 return new ResourceConfig(DistributionCatalogServlet.class)
439 .register(new AbstractBinder() {
442 protected void configure() {
443 bind(HTTP_SERVLET_REQUEST).to(HttpServletRequest.class);
444 bind(USER_BUSINESS_LOGIC).to(UserBusinessLogic.class);
445 bind(COMPONENT_UTILS).to(ComponentsUtils.class);
446 bind(ARTIFACTS_BUSINESS_LOGIC).to(ArtifactsBusinessLogic.class);
449 .property("contextConfig", context);