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 com.google.gson.Gson;
24 import org.glassfish.hk2.utilities.binding.AbstractBinder;
25 import org.glassfish.jersey.client.ClientConfig;
26 import org.glassfish.jersey.media.multipart.FormDataBodyPart;
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.Before;
34 import org.junit.Test;
35 import org.mockito.Mockito;
36 import org.openecomp.sdc.be.auditing.impl.AuditingManager;
37 import org.openecomp.sdc.be.config.ConfigurationManager;
38 import org.openecomp.sdc.be.config.SpringConfig;
39 import org.openecomp.sdc.be.dao.api.ResourceUploadStatus;
40 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
41 import org.openecomp.sdc.be.model.UploadResourceInfo;
42 import org.openecomp.sdc.be.resources.api.IResourceUploader;
43 import org.openecomp.sdc.be.resources.data.ESArtifactData;
44 import org.openecomp.sdc.common.api.ConfigurationSource;
45 import org.openecomp.sdc.common.api.Constants;
46 import org.openecomp.sdc.common.impl.ExternalConfiguration;
47 import org.openecomp.sdc.common.impl.FSConfigurationSource;
48 import org.slf4j.Logger;
49 import org.slf4j.LoggerFactory;
50 import org.springframework.context.ApplicationContext;
51 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
52 import org.springframework.web.context.WebApplicationContext;
54 import javax.servlet.ServletContext;
55 import javax.servlet.http.HttpServletRequest;
56 import javax.servlet.http.HttpSession;
57 import javax.ws.rs.client.Entity;
58 import javax.ws.rs.core.Application;
59 import javax.ws.rs.core.MediaType;
60 import javax.ws.rs.core.Response;
62 import java.util.ArrayList;
63 import java.util.List;
65 import static org.mockito.ArgumentMatchers.any;
66 import static org.mockito.ArgumentMatchers.eq;
67 import static org.mockito.Mockito.when;
69 public class ResourceUploadServletTest extends JerseyTest {
70 private static final Logger log = LoggerFactory.getLogger(ResourceUploadServletTest.class);
71 final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
72 final HttpSession session = Mockito.mock(HttpSession.class);
73 final ServletContext servletContext = Mockito.mock(ServletContext.class);
74 final WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
75 final WebApplicationContext webApplicationContext = Mockito.mock(WebApplicationContext.class);
76 final IResourceUploader iResourceUploader = Mockito.mock(IResourceUploader.class);
77 final AuditingManager iAuditingManager = Mockito.mock(AuditingManager.class);
79 Gson gson = new Gson();
81 public void zipDirectory() {
87 ExternalConfiguration.setAppName("catalog-be");
89 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
90 // when(servletContext.getAttribute(Constants.AUDITING_MANAGER)).thenReturn(iAuditingManager);
91 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
92 when(webApplicationContext.getBean(IResourceUploader.class)).thenReturn(iResourceUploader);
93 when(iResourceUploader.saveArtifact((ESArtifactData) any(), eq(true))).thenReturn(ResourceUploadStatus.OK);
94 when(webApplicationContext.getBean(AuditingManager.class)).thenReturn(iAuditingManager);
98 protected Application configure() {
100 ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
101 return new ResourceConfig(ResourceUploadServlet.class)
102 .register(MultiPartFeature.class)
103 .register(new AbstractBinder() {
106 protected void configure() {
107 // The below code was cut-pasted to here from setup() because
109 // to be executed during servlet initialization
110 bind(request).to(HttpServletRequest.class);
111 when(request.getSession()).thenReturn(session);
112 when(session.getServletContext()).thenReturn(servletContext);
113 String appConfigDir = "src/test/resources/config/catalog-be";
114 ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
115 ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
116 for (String mandatoryHeader : configurationManager.getConfiguration().getIdentificationHeaderFields()) {
118 when(request.getHeader(mandatoryHeader)).thenReturn(mandatoryHeader);
122 when(servletContext.getAttribute(Constants.CONFIGURATION_MANAGER_ATTR)).thenReturn(configurationManager);
125 .property("contextConfig", context);
130 protected void configureClient(ClientConfig config) {
131 config.register(MultiPartFeature.class);
135 public void testMultipart() {
136 FileDataBodyPart filePart = new FileDataBodyPart("resourceZip", new File("src/test/resources/config/normative-types-root.zip"));
137 List<String> tags = new ArrayList<>();
140 UploadResourceInfo resourceInfo = new UploadResourceInfo("payload", "normative-types-root.yml", "my_description", "category/mycategory", tags, null);
142 FormDataBodyPart metadataPart = new FormDataBodyPart("resourceMetadata", gson.toJson(resourceInfo), MediaType.APPLICATION_JSON_TYPE);
143 MultiPart multipartEntity = new FormDataMultiPart();
144 multipartEntity.bodyPart(filePart);
145 multipartEntity.bodyPart(metadataPart);
147 Response response = target().path("/v1/catalog/upload/" + ResourceUploadServlet.NORMATIVE_TYPE_RESOURCE).request(MediaType.APPLICATION_JSON).post(Entity.entity(multipartEntity, MediaType.MULTIPART_FORM_DATA), Response.class);
148 log.debug("{}", response);