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