Implement Attributes/Outputs BE (part 3)
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / TypesUploadServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  *
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  *
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.servlets;
22
23 import static java.util.Collections.emptyList;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.Mockito.when;
26
27 import fj.data.Either;
28 import java.io.File;
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.junit.jupiter.api.TestInstance;
52 import org.junit.jupiter.api.TestInstance.Lifecycle;
53 import org.mockito.Mock;
54 import org.mockito.Mockito;
55 import org.mockito.MockitoAnnotations;
56 import org.openecomp.sdc.be.components.impl.CapabilityTypeImportManager;
57 import org.openecomp.sdc.be.config.ConfigurationManager;
58 import org.openecomp.sdc.be.config.SpringConfig;
59 import org.openecomp.sdc.be.dao.api.ActionStatus;
60 import org.openecomp.sdc.be.impl.ComponentsUtils;
61 import org.openecomp.sdc.be.impl.ServletUtils;
62 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
63 import org.openecomp.sdc.be.model.CapabilityTypeDefinition;
64 import org.openecomp.sdc.be.model.User;
65 import org.openecomp.sdc.be.user.Role;
66 import org.openecomp.sdc.be.user.UserBusinessLogic;
67 import org.openecomp.sdc.common.api.ConfigurationSource;
68 import org.openecomp.sdc.common.api.Constants;
69 import org.openecomp.sdc.common.impl.ExternalConfiguration;
70 import org.openecomp.sdc.common.impl.FSConfigurationSource;
71 import org.openecomp.sdc.exception.ResponseFormat;
72 import org.springframework.context.ApplicationContext;
73 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
74 import org.springframework.web.context.WebApplicationContext;
75
76 @TestInstance(Lifecycle.PER_CLASS)
77 class TypesUploadServletTest extends JerseyTest {
78
79     @Mock
80     private HttpServletRequest request;
81     @Mock
82     private HttpSession session;
83     @Mock
84     private ServletContext servletContext;
85     @Mock
86     private WebAppContextWrapper webAppContextWrapper;
87     @Mock
88     private WebApplicationContext webApplicationContext;
89     @Mock
90     private CapabilityTypeImportManager importManager;
91     @Mock
92     private ServletUtils servletUtils;
93     @Mock
94     private UserBusinessLogic userAdmin;
95     @Mock
96     private ComponentsUtils componentUtils;
97     @Mock
98     private ResponseFormat responseFormat;
99
100     @BeforeAll
101     public void setup() {
102         ExternalConfiguration.setAppName("catalog-be");
103         when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR))
104             .thenReturn(webAppContextWrapper);
105         when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
106         when(webApplicationContext.getBean(CapabilityTypeImportManager.class)).thenReturn(importManager);
107         when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
108         when(servletUtils.getComponentsUtils()).thenReturn(componentUtils);
109         when(servletUtils.getUserAdmin()).thenReturn(userAdmin);
110         final String userId = "jh0003";
111         final User user = new User(userId);
112         user.setRole(Role.ADMIN.name());
113         when(userAdmin.getUser(userId)).thenReturn(user);
114         when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn(userId);
115         when(responseFormat.getStatus()).thenReturn(HttpStatus.CREATED_201);
116         when(componentUtils.getResponseFormat(ActionStatus.CREATED)).thenReturn(responseFormat);
117     }
118
119     @BeforeEach
120     public void before() throws Exception {
121         super.setUp();
122     }
123
124     @AfterEach
125     void after() throws Exception {
126         super.tearDown();
127     }
128
129     @Test
130     void creatingCapabilityTypeSuccessTest() {
131         final Either<List<ImmutablePair<CapabilityTypeDefinition, Boolean>>, ResponseFormat> either = Either.left(emptyList());
132         when(importManager.createCapabilityTypes(Mockito.anyString())).thenReturn(either);
133         final FileDataBodyPart filePart = new FileDataBodyPart("capabilityTypeZip", new File("src/test/resources/types/capabilityTypes.zip"));
134         MultiPart multipartEntity = new FormDataMultiPart();
135         multipartEntity.bodyPart(filePart);
136
137         final Response response = target().path("/v1/catalog/uploadType/capability").request(MediaType.APPLICATION_JSON)
138             .post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA), Response.class);
139
140         assertEquals(HttpStatus.CREATED_201, response.getStatus());
141     }
142
143     @Override
144     protected void configureClient(ClientConfig config) {
145         config.register(MultiPartFeature.class);
146     }
147
148     @Override
149     protected ResourceConfig configure() {
150         MockitoAnnotations.openMocks(this);
151
152         forceSet(TestProperties.CONTAINER_PORT, "0");
153         final TypesUploadServlet typesUploadServlet = new TypesUploadServlet(null, null, componentUtils,
154             servletUtils, null, importManager, null,
155             null, null,
156             null, null, null);
157         final ResourceConfig resourceConfig = new ResourceConfig().register(typesUploadServlet);
158
159         resourceConfig.register(MultiPartFeature.class);
160         resourceConfig.register(new AbstractBinder() {
161             @Override
162             protected void configure() {
163                 // The below code was cut-pasted to here from setup() because
164                 // due to it now has
165                 // to be executed during servlet initialization
166                 bind(request).to(HttpServletRequest.class);
167                 when(request.getSession()).thenReturn(session);
168                 when(session.getServletContext()).thenReturn(servletContext);
169                 String appConfigDir = "src/test/resources/config/catalog-be";
170                 ConfigurationSource configurationSource = new FSConfigurationSource(
171                     ExternalConfiguration.getChangeListener(), appConfigDir);
172                 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
173                 for (final String mandatoryHeader : configurationManager.getConfiguration().getIdentificationHeaderFields()) {
174                     when(request.getHeader(mandatoryHeader)).thenReturn(mandatoryHeader);
175                 }
176
177                 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
178                     .thenReturn(configurationManager);
179             }
180         });
181         final ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
182         resourceConfig.property("contextConfig", context);
183
184         return resourceConfig;
185     }
186 }