Support adding capability types to model
[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.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;
76
77 @TestInstance(Lifecycle.PER_CLASS)
78 class TypesUploadServletTest extends JerseyTest {
79
80     @Mock
81     private HttpServletRequest request;
82     @Mock
83     private HttpSession session;
84     @Mock
85     private ServletContext servletContext;
86     @Mock
87     private WebAppContextWrapper webAppContextWrapper;
88     @Mock
89     private WebApplicationContext webApplicationContext;
90     @Mock
91     private CapabilityTypeImportManager importManager;
92     @Mock
93     private ServletUtils servletUtils;
94     @Mock
95     private UserBusinessLogic userAdmin;
96     @Mock
97     private ComponentsUtils componentUtils;
98     @Mock
99     private ResponseFormat responseFormat;
100
101     @BeforeAll
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);
118     }
119
120     @BeforeEach
121     public void before() throws Exception {
122         super.setUp();
123     }
124
125     @AfterEach
126     void after() throws Exception {
127         super.tearDown();
128     }
129
130     @Test
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);
137
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);
140
141         assertEquals(HttpStatus.CREATED_201, response.getStatus());
142     }
143     
144     @Test
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");
152
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);
155
156         assertEquals(HttpStatus.CREATED_201, response.getStatus());
157     }
158
159     @Override
160     protected void configureClient(ClientConfig config) {
161         config.register(MultiPartFeature.class);
162     }
163
164     @Override
165     protected ResourceConfig configure() {
166         MockitoAnnotations.openMocks(this);
167
168         forceSet(TestProperties.CONTAINER_PORT, "0");
169         final TypesUploadServlet typesUploadServlet = new TypesUploadServlet(null, null, componentUtils,
170             servletUtils, null, importManager, null,
171             null, null,
172             null, null, null);
173         final ResourceConfig resourceConfig = new ResourceConfig().register(typesUploadServlet);
174
175         resourceConfig.register(MultiPartFeature.class);
176         resourceConfig.register(new AbstractBinder() {
177             @Override
178             protected void configure() {
179                 // The below code was cut-pasted to here from setup() because
180                 // due to it now has
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);
191                 }
192
193                 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR))
194                     .thenReturn(configurationManager);
195             }
196         });
197         final ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
198         resourceConfig.property("contextConfig", context);
199
200         return resourceConfig;
201     }
202 }