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 fj.data.Either;
24 import org.eclipse.jetty.http.HttpStatus;
25 import org.glassfish.hk2.utilities.binding.AbstractBinder;
26 import org.glassfish.jersey.client.ClientConfig;
27 import org.glassfish.jersey.media.multipart.FormDataMultiPart;
28 import org.glassfish.jersey.media.multipart.MultiPart;
29 import org.glassfish.jersey.media.multipart.MultiPartFeature;
30 import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
31 import org.glassfish.jersey.server.ResourceConfig;
32 import org.glassfish.jersey.test.JerseyTest;
33 import org.junit.BeforeClass;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.openecomp.sdc.be.components.impl.CapabilityTypeImportManager;
37 import org.openecomp.sdc.be.config.ConfigurationManager;
38 import org.openecomp.sdc.be.config.SpringConfig;
39 import org.openecomp.sdc.be.dao.api.ActionStatus;
40 import org.openecomp.sdc.be.impl.ComponentsUtils;
41 import org.openecomp.sdc.be.impl.ServletUtils;
42 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
43 import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
44 import org.openecomp.sdc.be.model.User;
45 import org.openecomp.sdc.be.user.Role;
46 import org.openecomp.sdc.be.user.UserBusinessLogic;
47 import org.openecomp.sdc.common.api.ConfigurationSource;
48 import org.openecomp.sdc.common.api.Constants;
49 import org.openecomp.sdc.common.impl.ExternalConfiguration;
50 import org.openecomp.sdc.common.impl.FSConfigurationSource;
51 import org.openecomp.sdc.exception.ResponseFormat;
52 import org.springframework.context.ApplicationContext;
53 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
54 import org.springframework.web.context.WebApplicationContext;
56 import javax.servlet.ServletContext;
57 import javax.servlet.http.HttpServletRequest;
58 import javax.servlet.http.HttpSession;
59 import javax.ws.rs.client.Entity;
60 import javax.ws.rs.core.MediaType;
61 import javax.ws.rs.core.Response;
63 import java.util.ArrayList;
64 import java.util.List;
66 import static org.junit.Assert.assertTrue;
67 import static org.mockito.Mockito.when;
69 public class TypesUploadServletTest extends JerseyTest {
71 public static final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
72 private static final HttpSession session = Mockito.mock(HttpSession.class);
73 public static final ServletContext servletContext = Mockito.mock(ServletContext.class);
74 public static final WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
75 private static final WebApplicationContext webApplicationContext = Mockito.mock(WebApplicationContext.class);
76 private static final CapabilityTypeImportManager importManager = Mockito.mock(CapabilityTypeImportManager.class);
77 private static final ServletUtils servletUtils = Mockito.mock(ServletUtils.class);
78 private static final UserBusinessLogic userAdmin = Mockito.mock(UserBusinessLogic.class);
79 private static final ComponentsUtils componentUtils = Mockito.mock(ComponentsUtils.class);
80 private static final ResponseFormat responseFormat = Mockito.mock(ResponseFormat.class);
83 public static void setup() {
84 ExternalConfiguration.setAppName("catalog-be");
85 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
86 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
87 when(webApplicationContext.getBean(CapabilityTypeImportManager.class)).thenReturn(importManager);
88 when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
89 when(servletUtils.getComponentsUtils()).thenReturn(componentUtils);
90 when(servletUtils.getUserAdmin()).thenReturn(userAdmin);
91 String userId = "jh0003";
92 User user = new User();
93 user.setUserId(userId);
94 user.setRole(Role.ADMIN.name());
95 Either<User, ActionStatus> eitherUser = Either.left(user);
96 when(userAdmin.getUser(userId, false)).thenReturn(eitherUser);
97 when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(userId);
98 when(responseFormat.getStatus()).thenReturn(HttpStatus.CREATED_201);
99 when(componentUtils.getResponseFormat(ActionStatus.CREATED)).thenReturn(responseFormat);
104 public void creatingCapabilityTypeSuccessTest() {
105 List<CapabilityTypeDefinition> emptyList = new ArrayList<CapabilityTypeDefinition>();
106 Either<List<CapabilityTypeDefinition>, ResponseFormat> either = Either.left(emptyList);
107 when(importManager.createCapabilityTypes(Mockito.anyString())).thenReturn(either);
108 FileDataBodyPart filePart = new FileDataBodyPart("capabilityTypeZip", new File("src/test/resources/types/capabilityTypes.zip"));
109 MultiPart multipartEntity = new FormDataMultiPart();
110 multipartEntity.bodyPart(filePart);
112 Response response = target().path("/v1/catalog/uploadType/capability").request(MediaType.APPLICATION_JSON).post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA), Response.class);
114 assertTrue(response.getStatus() == HttpStatus.CREATED_201);
119 protected void configureClient(ClientConfig config) {
120 config.register(MultiPartFeature.class);
124 protected ResourceConfig configure() {
126 ResourceConfig resourceConfig = new ResourceConfig()
127 .register(new TypesUploadServlet(importManager, null, null, null, null, null));
129 resourceConfig.register(MultiPartFeature.class);
130 resourceConfig.register(new AbstractBinder() {
133 protected void configure() {
134 // The below code was cut-pasted to here from setup() because
136 // to be executed during servlet initialization
137 bind(request).to(HttpServletRequest.class);
138 when(request.getSession()).thenReturn(session);
139 when(session.getServletContext()).thenReturn(servletContext);
140 String appConfigDir = "src/test/resources/config/catalog-be";
141 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
142 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
143 for (String mandatoryHeader : configurationManager.getConfiguration().getIdentificationHeaderFields()) {
145 when(request.getHeader(mandatoryHeader)).thenReturn(mandatoryHeader);
149 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
152 ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
153 resourceConfig.property("contextConfig", context);
155 return resourceConfig;