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.FormDataBodyPart;
41 import org.glassfish.jersey.media.multipart.FormDataMultiPart;
42 import org.glassfish.jersey.media.multipart.MultiPart;
43 import org.glassfish.jersey.media.multipart.MultiPartFeature;
44 import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
45 import org.glassfish.jersey.server.ResourceConfig;
46 import org.glassfish.jersey.test.JerseyTest;
47 import org.glassfish.jersey.test.TestProperties;
48 import org.junit.jupiter.api.AfterEach;
49 import org.junit.jupiter.api.BeforeAll;
50 import org.junit.jupiter.api.BeforeEach;
51 import org.junit.jupiter.api.Test;
52 import org.junit.jupiter.api.TestInstance;
53 import org.junit.jupiter.api.TestInstance.Lifecycle;
54 import org.mockito.Mock;
55 import org.mockito.Mockito;
56 import org.mockito.MockitoAnnotations;
57 import org.openecomp.sdc.be.components.impl.CapabilityTypeImportManager;
58 import org.openecomp.sdc.be.config.ConfigurationManager;
59 import org.openecomp.sdc.be.config.SpringConfig;
60 import org.openecomp.sdc.be.dao.api.ActionStatus;
61 import org.openecomp.sdc.be.impl.ComponentsUtils;
62 import org.openecomp.sdc.be.impl.ServletUtils;
63 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
64 import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
65 import org.openecomp.sdc.be.model.User;
66 import org.openecomp.sdc.be.user.Role;
67 import org.openecomp.sdc.be.user.UserBusinessLogic;
68 import org.openecomp.sdc.common.api.ConfigurationSource;
69 import org.openecomp.sdc.common.api.Constants;
70 import org.openecomp.sdc.common.impl.ExternalConfiguration;
71 import org.openecomp.sdc.common.impl.FSConfigurationSource;
72 import org.openecomp.sdc.exception.ResponseFormat;
73 import org.springframework.context.ApplicationContext;
74 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
75 import org.springframework.web.context.WebApplicationContext;
77 @TestInstance(Lifecycle.PER_CLASS)
78 class TypesUploadServletTest extends JerseyTest {
81 private HttpServletRequest request;
83 private HttpSession session;
85 private ServletContext servletContext;
87 private WebAppContextWrapper webAppContextWrapper;
89 private WebApplicationContext webApplicationContext;
91 private CapabilityTypeImportManager importManager;
93 private ServletUtils servletUtils;
95 private UserBusinessLogic userAdmin;
97 private ComponentsUtils componentUtils;
99 private ResponseFormat responseFormat;
102 public void setup() {
103 ExternalConfiguration.setAppName("catalog-be");
104 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR))
105 .thenReturn(webAppContextWrapper);
106 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
107 when(webApplicationContext.getBean(CapabilityTypeImportManager.class)).thenReturn(importManager);
108 when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
109 when(servletUtils.getComponentsUtils()).thenReturn(componentUtils);
110 when(servletUtils.getUserAdmin()).thenReturn(userAdmin);
111 final String userId = "jh0003";
112 final User user = new User(userId);
113 user.setRole(Role.ADMIN.name());
114 when(userAdmin.getUser(userId)).thenReturn(user);
115 when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(userId);
116 when(responseFormat.getStatus()).thenReturn(HttpStatus.CREATED_201);
117 when(componentUtils.getResponseFormat(ActionStatus.CREATED)).thenReturn(responseFormat);
121 public void before() throws Exception {
126 void after() throws Exception {
131 void creatingCapabilityTypeSuccessTest() {
132 final Either<List<ImmutablePair<CapabilityTypeDefinition, Boolean>>, ResponseFormat> either = Either.left(emptyList());
133 when(importManager.createCapabilityTypes(Mockito.anyString(), Mockito.isNull())).thenReturn(either);
134 final FileDataBodyPart filePart = new FileDataBodyPart("capabilityTypeZip", new File("src/test/resources/types/capabilityTypes.zip"));
135 MultiPart multipartEntity = new FormDataMultiPart();
136 multipartEntity.bodyPart(filePart);
138 final Response response = target().path("/v1/catalog/uploadType/capability").request(MediaType.APPLICATION_JSON)
139 .post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA), Response.class);
141 assertEquals(HttpStatus.CREATED_201, response.getStatus());
145 void creatingCapabilityTypeWithModelSuccessTest() {
146 final Either<List<ImmutablePair<CapabilityTypeDefinition, Boolean>>, ResponseFormat> either = Either.left(emptyList());
147 when(importManager.createCapabilityTypes(Mockito.anyString(), Mockito.eq("testModel"))).thenReturn(either);
148 final FileDataBodyPart filePart = new FileDataBodyPart("capabilityTypeZip", new File("src/test/resources/types/capabilityTypes.zip"));
149 FormDataMultiPart multipartEntity = new FormDataMultiPart();
150 multipartEntity.bodyPart(filePart);
151 multipartEntity.field("model", "testModel");
153 final Response response = target().path("/v1/catalog/uploadType/capability").request(MediaType.APPLICATION_JSON)
154 .post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA), Response.class);
156 assertEquals(HttpStatus.CREATED_201, response.getStatus());
160 protected void configureClient(ClientConfig config) {
161 config.register(MultiPartFeature.class);
165 protected ResourceConfig configure() {
166 MockitoAnnotations.openMocks(this);
168 forceSet(TestProperties.CONTAINER_PORT, "0");
169 final TypesUploadServlet typesUploadServlet = new TypesUploadServlet(null, null, componentUtils,
170 servletUtils, null, importManager, null,
173 final ResourceConfig resourceConfig = new ResourceConfig().register(typesUploadServlet);
175 resourceConfig.register(MultiPartFeature.class);
176 resourceConfig.register(new AbstractBinder() {
178 protected void configure() {
179 // The below code was cut-pasted to here from setup() because
181 // to be executed during servlet initialization
182 bind(request).to(HttpServletRequest.class);
183 when(request.getSession()).thenReturn(session);
184 when(session.getServletContext()).thenReturn(servletContext);
185 String appConfigDir = "src/test/resources/config/catalog-be";
186 ConfigurationSource configurationSource = new FSConfigurationSource(
187 ExternalConfiguration.getChangeListener(), appConfigDir);
188 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
189 for (final String mandatoryHeader : configurationManager.getConfiguration().getIdentificationHeaderFields()) {
190 when(request.getHeader(mandatoryHeader)).thenReturn(mandatoryHeader);
193 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
194 .thenReturn(configurationManager);
197 final ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
198 resourceConfig.property("contextConfig", context);
200 return resourceConfig;