2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. 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.servlets;
23 import static java.util.Collections.emptyList;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.Mockito.when;
27 import fj.data.Either;
29 import java.util.List;
30 import javax.servlet.ServletContext;
31 import javax.servlet.http.HttpServletRequest;
32 import javax.servlet.http.HttpSession;
33 import javax.ws.rs.client.Entity;
34 import javax.ws.rs.core.MediaType;
35 import javax.ws.rs.core.Response;
36 import org.apache.commons.lang3.tuple.ImmutablePair;
37 import org.eclipse.jetty.http.HttpStatus;
38 import org.glassfish.hk2.utilities.binding.AbstractBinder;
39 import org.glassfish.jersey.client.ClientConfig;
40 import org.glassfish.jersey.media.multipart.FormDataMultiPart;
41 import org.glassfish.jersey.media.multipart.MultiPart;
42 import org.glassfish.jersey.media.multipart.MultiPartFeature;
43 import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
44 import org.glassfish.jersey.server.ResourceConfig;
45 import org.glassfish.jersey.test.JerseyTest;
46 import org.glassfish.jersey.test.TestProperties;
47 import org.junit.jupiter.api.AfterEach;
48 import org.junit.jupiter.api.BeforeAll;
49 import org.junit.jupiter.api.BeforeEach;
50 import org.junit.jupiter.api.Test;
51 import org.mockito.Mockito;
52 import org.openecomp.sdc.be.components.impl.CapabilityTypeImportManager;
53 import org.openecomp.sdc.be.config.ConfigurationManager;
54 import org.openecomp.sdc.be.config.SpringConfig;
55 import org.openecomp.sdc.be.dao.api.ActionStatus;
56 import org.openecomp.sdc.be.impl.ComponentsUtils;
57 import org.openecomp.sdc.be.impl.ServletUtils;
58 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
59 import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
60 import org.openecomp.sdc.be.model.User;
61 import org.openecomp.sdc.be.user.Role;
62 import org.openecomp.sdc.be.user.UserBusinessLogic;
63 import org.openecomp.sdc.common.api.ConfigurationSource;
64 import org.openecomp.sdc.common.api.Constants;
65 import org.openecomp.sdc.common.impl.ExternalConfiguration;
66 import org.openecomp.sdc.common.impl.FSConfigurationSource;
67 import org.openecomp.sdc.exception.ResponseFormat;
68 import org.springframework.context.ApplicationContext;
69 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
70 import org.springframework.web.context.WebApplicationContext;
72 class TypesUploadServletTest extends JerseyTest {
74 public static final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
75 private static final HttpSession session = Mockito.mock(HttpSession.class);
76 public static final ServletContext servletContext = Mockito.mock(ServletContext.class);
77 public static final WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
78 private static final WebApplicationContext webApplicationContext = Mockito.mock(WebApplicationContext.class);
79 private static final CapabilityTypeImportManager importManager = Mockito.mock(CapabilityTypeImportManager.class);
80 private static final ServletUtils servletUtils = Mockito.mock(ServletUtils.class);
81 private static final UserBusinessLogic userAdmin = Mockito.mock(UserBusinessLogic.class);
82 private static final ComponentsUtils componentUtils = Mockito.mock(ComponentsUtils.class);
83 private static final ResponseFormat responseFormat = Mockito.mock(ResponseFormat.class);
86 public static void setup() {
87 ExternalConfiguration.setAppName("catalog-be");
88 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR))
89 .thenReturn(webAppContextWrapper);
90 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
91 when(webApplicationContext.getBean(CapabilityTypeImportManager.class)).thenReturn(importManager);
92 when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
93 when(servletUtils.getComponentsUtils()).thenReturn(componentUtils);
94 when(servletUtils.getUserAdmin()).thenReturn(userAdmin);
95 String userId = "jh0003";
96 User user = new User();
97 user.setUserId(userId);
98 user.setRole(Role.ADMIN.name());
99 when(userAdmin.getUser(userId)).thenReturn(user);
100 when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(userId);
101 when(responseFormat.getStatus()).thenReturn(HttpStatus.CREATED_201);
102 when(componentUtils.getResponseFormat(ActionStatus.CREATED)).thenReturn(responseFormat);
107 public void before() throws Exception {
112 void after() throws Exception {
117 void creatingCapabilityTypeSuccessTest() {
118 Either<List<ImmutablePair<CapabilityTypeDefinition, Boolean>>, ResponseFormat> either = Either
120 when(importManager.createCapabilityTypes(Mockito.anyString())).thenReturn(either);
121 FileDataBodyPart filePart = new FileDataBodyPart("capabilityTypeZip",
122 new File("src/test/resources/types/capabilityTypes.zip"));
123 MultiPart multipartEntity = new FormDataMultiPart();
124 multipartEntity.bodyPart(filePart);
126 Response response = target().path("/v1/catalog/uploadType/capability").request(MediaType.APPLICATION_JSON)
127 .post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA), Response.class);
129 assertEquals(HttpStatus.CREATED_201, response.getStatus());
134 protected void configureClient(ClientConfig config) {
135 config.register(MultiPartFeature.class);
139 protected ResourceConfig configure() {
141 TypesUploadServlet typesUploadServlet = new TypesUploadServlet(null, null, componentUtils,
142 servletUtils, null, importManager, null,
145 ResourceConfig resourceConfig = new ResourceConfig()
146 .register(typesUploadServlet);
148 resourceConfig.register(MultiPartFeature.class);
149 resourceConfig.register(new AbstractBinder() {
152 protected void configure() {
153 // The below code was cut-pasted to here from setup() because
155 // to be executed during servlet initialization
156 bind(request).to(HttpServletRequest.class);
157 when(request.getSession()).thenReturn(session);
158 when(session.getServletContext()).thenReturn(servletContext);
159 String appConfigDir = "src/test/resources/config/catalog-be";
160 ConfigurationSource configurationSource = new FSConfigurationSource(
161 ExternalConfiguration.getChangeListener(), appConfigDir);
162 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
163 for (String mandatoryHeader : configurationManager.getConfiguration().getIdentificationHeaderFields()) {
165 when(request.getHeader(mandatoryHeader)).thenReturn(mandatoryHeader);
169 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
170 .thenReturn(configurationManager);
173 ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
174 resourceConfig.property("contextConfig", context);
176 return resourceConfig;