catalog-be servlets refactoring
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / externalapi / servlet / AssetsDataServletTest.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.externalapi.servlet;
22
23
24 import fj.data.Either;
25 import org.apache.http.HttpStatus;
26 import org.glassfish.hk2.utilities.binding.AbstractBinder;
27 import org.glassfish.jersey.server.ResourceConfig;
28 import org.glassfish.jersey.test.JerseyTest;
29 import org.glassfish.jersey.test.TestProperties;
30 import org.json.simple.JSONObject;
31 import org.json.simple.parser.JSONParser;
32 import org.junit.BeforeClass;
33 import org.junit.Test;
34 import org.mockito.Mockito;
35 import org.mockito.stubbing.Answer;
36 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
37 import org.openecomp.sdc.be.components.impl.ElementBusinessLogic;
38 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
39 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
40 import org.openecomp.sdc.be.components.lifecycle.LifecycleBusinessLogic;
41 import org.openecomp.sdc.be.config.SpringConfig;
42 import org.openecomp.sdc.be.dao.api.ActionStatus;
43 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
44 import org.openecomp.sdc.be.ecomp.converters.AssetMetadataConverter;
45 import org.openecomp.sdc.be.externalapi.servlet.representation.ResourceAssetMetadata;
46 import org.openecomp.sdc.be.impl.ComponentsUtils;
47 import org.openecomp.sdc.be.impl.ServletUtils;
48 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
49 import org.openecomp.sdc.be.model.Resource;
50 import org.openecomp.sdc.be.model.category.CategoryDefinition;
51 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
52 import org.openecomp.sdc.be.user.UserBusinessLogic;
53 import org.openecomp.sdc.common.api.Constants;
54 import org.openecomp.sdc.common.datastructure.FunctionalInterfaces;
55 import org.openecomp.sdc.common.impl.ExternalConfiguration;
56 import org.openecomp.sdc.exception.ResponseFormat;
57 import org.springframework.context.ApplicationContext;
58 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
59 import org.springframework.web.context.WebApplicationContext;
60
61 import javax.servlet.ServletContext;
62 import javax.servlet.http.HttpServletRequest;
63 import javax.servlet.http.HttpSession;
64 import javax.ws.rs.client.Entity;
65 import javax.ws.rs.core.Application;
66 import javax.ws.rs.core.MediaType;
67 import javax.ws.rs.core.Response;
68 import java.util.Arrays;
69
70 import static org.junit.Assert.assertEquals;
71 import static org.mockito.Mockito.when;
72
73 public class AssetsDataServletTest extends JerseyTest {
74
75     public static final HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
76     public static final HttpSession session = Mockito.mock(HttpSession.class);
77     public static final ServletContext servletContext = Mockito.mock(ServletContext.class);
78     public static final WebAppContextWrapper webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
79     public static final WebApplicationContext webApplicationContext = Mockito.mock(WebApplicationContext.class);
80     public static final ResponseFormat responseFormat = Mockito.mock(ResponseFormat.class);
81     public static final ServletUtils servletUtils = Mockito.mock(ServletUtils.class);
82     public static final ComponentsUtils componentsUtils = Mockito.mock(ComponentsUtils.class);
83     public static final ResourceImportManager resourceImportManager = Mockito.mock(ResourceImportManager.class);
84     public static final ResourceBusinessLogic resourceBusinessLogic = Mockito.mock(ResourceBusinessLogic.class);
85     public static final ElementBusinessLogic elementBusinessLogic = Mockito.mock(ElementBusinessLogic.class);
86     public static final Resource resource = Mockito.mock(Resource.class);
87     public static final CategoryDefinition categoryDefinition = Mockito.mock(CategoryDefinition.class);
88     public static final SubCategoryDefinition subCategoryDefinition = Mockito.mock(SubCategoryDefinition.class);
89     public static final AssetMetadataConverter assetMetadataConverter = Mockito.mock(AssetMetadataConverter.class);
90     public static final ResourceAssetMetadata resourceAssetMetadata = new ResourceAssetMetadata();
91     public static final UserBusinessLogic userBusinessLogic = Mockito.mock(UserBusinessLogic.class);
92     public static final ComponentInstanceBusinessLogic componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
93     public static final LifecycleBusinessLogic lifecycleBusinessLogic = Mockito.mock(LifecycleBusinessLogic.class);
94
95
96
97
98     @BeforeClass
99     public static void setup() {
100         ExternalConfiguration.setAppName("catalog-be");
101         when(request.getSession()).thenReturn(session);
102         when(request.getHeader(Constants.X_ECOMP_INSTANCE_ID_HEADER)).thenReturn("mockXEcompInstanceId");
103         when(request.getHeader(Constants.USER_ID_HEADER)).thenReturn("mockAttID");
104         when(request.getRequestURL()).thenReturn(new StringBuffer("sdc/v1/catalog/resources"));
105
106         when(session.getServletContext()).thenReturn(servletContext);
107         when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
108         when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
109
110         when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
111         when(webApplicationContext.getBean(ResourceBusinessLogic.class)).thenReturn(resourceBusinessLogic);
112
113         when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils);
114         mockResponseFormat();
115
116         when(resource.getName()).thenReturn("MockVFCMT");
117         when(resource.getSystemName()).thenReturn("mockvfcmt");
118         Either<Resource, ResponseFormat>  eitherRet = Either.left(resource);
119         when(componentsUtils.convertJsonToObjectUsingObjectMapper(Mockito.any(), Mockito.any(), Mockito.eq(Resource.class), Mockito.any(), Mockito.eq(ComponentTypeEnum.RESOURCE))).thenReturn(eitherRet);
120
121         when(webApplicationContext.getBean(ResourceImportManager.class)).thenReturn(resourceImportManager);
122         when(webApplicationContext.getBean(ElementBusinessLogic.class)).thenReturn(elementBusinessLogic);
123         when(categoryDefinition.getName()).thenReturn("Template");
124         when(subCategoryDefinition.getName()).thenReturn("Monitoring Template");
125         when(categoryDefinition.getSubcategories()).thenReturn(Arrays.asList(subCategoryDefinition));
126         when(elementBusinessLogic.getAllResourceCategories()).thenReturn(Either.left(Arrays.asList(categoryDefinition)));
127         when(resourceBusinessLogic.createResource(Mockito.eq(resource), Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(resource);
128         when(webApplicationContext.getBean(AssetMetadataConverter.class)).thenReturn(assetMetadataConverter);
129
130         Mockito.doReturn(Either.left(resourceAssetMetadata)).when(assetMetadataConverter).convertToSingleAssetMetadata(Mockito.eq(resource), Mockito.anyString(),
131                 Mockito.eq(true));
132
133
134
135     }
136
137
138     private static void mockResponseFormat() {
139         when(componentsUtils.getResponseFormat(Mockito.any(ActionStatus.class), Mockito.any(String[].class))).thenAnswer((Answer<ResponseFormat>) invocation -> {
140             ResponseFormat ret;
141             final ActionStatus actionStatus = invocation.getArgument(0);
142             switch( actionStatus ){
143             case CREATED :{
144                 ret = new ResponseFormat(HttpStatus.SC_CREATED);
145                 break;
146             }
147             default :{
148                 ret = new ResponseFormat(HttpStatus.SC_INTERNAL_SERVER_ERROR);
149                 break;
150             }
151             }
152             return ret;
153         });
154     }
155
156
157
158     @Test
159     public void createVfcmtHappyScenario() {
160         final JSONObject createRequest = buildCreateJsonRequest();
161         Response response = target().path("/v1/catalog/resources").request(MediaType.APPLICATION_JSON).header(Constants.X_ECOMP_INSTANCE_ID_HEADER, "mockXEcompInstanceId").header(Constants.USER_ID_HEADER, "mockAttID")
162                 .post(Entity.json(createRequest.toJSONString()), Response.class);
163         assertEquals(HttpStatus.SC_CREATED, response.getStatus());
164
165     }
166     private static final String BASIC_CREATE_REQUEST = "{\r\n" +
167             "  \"name\": \"VFCMT_1\",\r\n" +
168             "  \"description\": \"VFCMT Description\",\r\n" +
169             "  \"resourceType\" : \"VFCMT\",\r\n" +
170             "  \"category\": \"Template\",\r\n" +
171             "  \"subcategory\": \"Monitoring Template\",\r\n" +
172             "  \"vendorName\" : \"DCAE\",\r\n" +
173             "  \"vendorRelease\" : \"1.0\",\r\n" +
174             "  \"tags\": [\r\n" +
175             "    \"VFCMT_1\"\r\n" +
176             "  ],\r\n" +
177             "  \"icon\" : \"defaulticon\",\r\n" +
178             "  \"contactId\": \"cs0008\"\r\n" +
179             "}";
180     private JSONObject buildCreateJsonRequest() {
181
182         JSONParser parser = new JSONParser();
183         return (JSONObject) FunctionalInterfaces.swallowException( () -> parser.parse(BASIC_CREATE_REQUEST));
184
185     }
186
187     @Override
188     protected Application configure() {
189         ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
190         forceSet(TestProperties.CONTAINER_PORT, "0");
191         return new ResourceConfig(CrudExternalServlet.class)
192                 .register(new AbstractBinder() {
193
194                     @Override
195                     protected void configure() {
196                         bind(request).to(HttpServletRequest.class);
197                         bind(userBusinessLogic).to(UserBusinessLogic.class);
198                         bind(componentInstanceBusinessLogic).to(ComponentInstanceBusinessLogic.class);
199                         bind(componentsUtils).to(ComponentsUtils.class);
200                         bind(servletUtils).to(ServletUtils.class);
201                         bind(resourceImportManager).to(ResourceImportManager.class);
202                         bind(elementBusinessLogic).to(ElementBusinessLogic.class);
203                         bind(assetMetadataConverter).to(AssetMetadataConverter.class);
204                         bind(lifecycleBusinessLogic).to(LifecycleBusinessLogic.class);
205                         bind(resourceBusinessLogic).to(ResourceBusinessLogic.class);
206                     }
207                 })
208                 .property("contextConfig", context);
209     }
210 }