f9336aa8be9b793cd0046f2ef0f257f2e88f7500
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / ComponentInstanceServletTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
4  * ================================================================================
5  * Copyright (C) 2019 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 fj.data.Either;
24 import java.util.HashMap;
25 import java.util.List;
26 import java.util.Map;
27 import java.util.ArrayList;
28 import javax.ws.rs.client.Entity;
29 import org.eclipse.jetty.http.HttpStatus;
30 import org.glassfish.hk2.utilities.binding.AbstractBinder;
31 import org.glassfish.jersey.server.ResourceConfig;
32 import org.glassfish.jersey.test.JerseyTest;
33 import org.glassfish.jersey.test.TestProperties;
34 import org.junit.BeforeClass;
35 import org.junit.Test;
36 import org.mockito.ArgumentMatchers;
37 import org.mockito.Mockito;
38 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
39 import org.openecomp.sdc.be.config.SpringConfig;
40 import org.openecomp.sdc.be.dao.api.ActionStatus;
41 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
42 import org.openecomp.sdc.be.impl.ComponentsUtils;
43 import org.openecomp.sdc.be.impl.ServletUtils;
44 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
45 import org.openecomp.sdc.be.model.CapabilityDefinition;
46 import org.openecomp.sdc.be.model.ComponentInstance;
47 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
48 import org.openecomp.sdc.be.model.RequirementDefinition;
49 import org.openecomp.sdc.common.api.Constants;
50 import org.openecomp.sdc.exception.ResponseFormat;
51 import org.springframework.context.ApplicationContext;
52 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
53 import org.springframework.web.context.WebApplicationContext;
54 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
55 import org.openecomp.sdc.be.model.User;
56
57 import javax.servlet.ServletContext;
58 import javax.servlet.http.HttpServletRequest;
59 import javax.servlet.http.HttpSession;
60 import javax.ws.rs.core.MediaType;
61 import javax.ws.rs.core.Response;
62
63 import static org.junit.Assert.assertEquals;
64 import static org.mockito.ArgumentMatchers.any;
65 import static org.mockito.ArgumentMatchers.eq;
66 import static org.mockito.Mockito.when;
67 import static org.mockito.ArgumentMatchers.anyString;
68 import static org.mockito.ArgumentMatchers.nullable;
69
70 /**
71  * The test suite designed for test functionality of ComponentInstanceServlet class
72  */
73 public class ComponentInstanceServletTest extends JerseyTest {
74
75     private final static String USER_ID = "jh0003";
76     private static HttpServletRequest request;
77     private static HttpSession session;
78     private static ServletContext servletContext;
79     private static WebAppContextWrapper webAppContextWrapper;
80     private static WebApplicationContext webApplicationContext;
81     private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
82     private static ComponentsUtils componentsUtils;
83     private static ServletUtils servletUtils;
84     private static ResponseFormat responseFormat;
85
86     @BeforeClass
87     public static void setup() {
88         createMocks();
89         stubMethods();
90     }
91
92     @Test
93     public void testGetRelationByIdSuccess(){
94
95         String containerComponentType = "resources";
96         String componentId = "componentId";
97         String relationId = "relationId";
98         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/" + relationId + "/relationId";
99         Either<RequirementCapabilityRelDef, ResponseFormat> successResponse = Either.left(new RequirementCapabilityRelDef());
100         when(componentInstanceBusinessLogic.getRelationById(eq(componentId), eq(relationId), eq(USER_ID), eq(ComponentTypeEnum.RESOURCE))).thenReturn(successResponse);
101         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
102         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
103         Response response = target()
104                 .path(path)
105                 .request(MediaType.APPLICATION_JSON)
106                 .header("USER_ID", USER_ID)
107                 .get( Response.class);
108
109         assertEquals(response.getStatus(), HttpStatus.OK_200);
110     }
111
112     @Test
113     public void testGetRelationByIdFailure(){
114
115         String containerComponentType = "unknown_type";
116         String componentId = "componentId";
117         String relationId = "relationId";
118         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/" + relationId + "/relationId";
119         when(responseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400);
120         when(componentsUtils.getResponseFormat(eq(ActionStatus.UNSUPPORTED_ERROR), eq(containerComponentType))).thenReturn(responseFormat);
121         Response response = target()
122                 .path(path)
123                 .request(MediaType.APPLICATION_JSON)
124                 .header("USER_ID", USER_ID)
125                 .get( Response.class);
126
127         assertEquals(response.getStatus(), HttpStatus.BAD_REQUEST_400);
128     }
129
130     @Test
131     public void testCopyComponentInstanceSuccess(){
132
133         String componentId = "componentId";
134         String componentInstanceId = "componentInstanceId";
135         String path = "/v1/catalog/services/" + componentId + "/copyComponentInstance/" + componentInstanceId;
136
137         Either<Map<String, ComponentInstance>, ResponseFormat> successResponse = Either.left(new HashMap<String, ComponentInstance>());
138         when(componentInstanceBusinessLogic.copyComponentInstance(any(ComponentInstance.class), eq(componentId), eq(componentInstanceId), eq(USER_ID))).thenReturn(successResponse);
139         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
140         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
141
142         ComponentInstance c = new ComponentInstance();
143         c.setName("comp1");
144         c.setUniqueId("comp1");
145         c.setComponentUid("comp1");
146         c.setPosX("10");
147         c.setPosY("10");
148         c.setCapabilities(new HashMap<String, List<CapabilityDefinition>>());
149         c.setRequirements(new HashMap<String, List<RequirementDefinition>>());
150
151         Response response = target()
152             .path(path)
153             .request(MediaType.APPLICATION_JSON)
154             .header("USER_ID", USER_ID)
155             .post(Entity.json(c));
156
157         assertEquals(response.getStatus(), HttpStatus.OK_200);
158     }
159
160     @Test
161     public void testBatchDeleteResourceInstancesSuccess() {
162
163         String componentId = "componentId";
164         String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
165         String compId1 = "compId1";
166         String[] delCompIds = new String[1];
167         delCompIds[0] = compId1;
168         List<ComponentInstance> compInsts = new ArrayList<ComponentInstance>();
169         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/batchDeleteResourceInstances";
170
171         ComponentInstance compInst = new ComponentInstance();
172         compInst.setName(compId1);
173         compInst.setUniqueId(compId1);
174         compInst.setComponentUid(compId1);
175         compInst.setInvariantName(compId1);
176         compInsts.add(compInst);
177
178         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
179         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
180         Either<String[], ResponseFormat> convertStatusEither = Either.left(delCompIds);
181         when(componentsUtils
182                 .convertJsonToObjectUsingObjectMapper(anyString(), any(User.class), ArgumentMatchers.<Class<String[]>>any(),
183                         nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))).thenReturn(convertStatusEither);
184         when(componentInstanceBusinessLogic
185                 .batchDeleteComponentInstance(eq(containerComponentType), eq(componentId), any(List.class),
186                         eq(USER_ID))).thenReturn(Mockito.mock(Map.class));
187
188         Response response = target()
189                 .path(path)
190                 .request(MediaType.APPLICATION_JSON)
191                 .header("USER_ID", USER_ID)
192                 .post(Entity.json(compInsts));
193
194         assertEquals(HttpStatus.OK_200, response.getStatus());
195     }
196
197     @Test
198     public void testBatchDeleteResourceInstancesFailure() {
199
200         String componentId = "componentId";
201         String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
202         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/batchDeleteResourceInstances";
203
204         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
205         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
206
207         Response response = target()
208                 .path(path)
209                 .request(MediaType.APPLICATION_JSON)
210                 .header("USER_ID", USER_ID)
211                 .post(Entity.json(""));
212
213         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
214     }
215
216     @Test
217     public void testBatchDissociateRIFromRISuccess() {
218
219         String componentId = "componentId";
220         String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
221         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/resourceInstance/batchDissociate";
222         RequirementCapabilityRelDef[] refs = new RequirementCapabilityRelDef[1];
223         RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
224         refs[0] = ref;
225
226         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
227         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
228         Either<RequirementCapabilityRelDef[], ResponseFormat> convertReqEither = Either.left(refs);
229         when(componentsUtils.convertJsonToObjectUsingObjectMapper(anyString(), any(User.class),
230                 ArgumentMatchers.<Class<RequirementCapabilityRelDef[]>>any(),
231                 nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))).thenReturn(convertReqEither);
232         Either<RequirementCapabilityRelDef, ResponseFormat> actionResponseEither = Either.left(ref);
233         when(componentInstanceBusinessLogic
234                 .dissociateRIFromRI(componentId, USER_ID, ref, ComponentTypeEnum.findByParamName(containerComponentType)))
235                 .thenReturn(actionResponseEither);
236
237         Response response = target()
238                 .path(path)
239                 .request(MediaType.APPLICATION_JSON)
240                 .header("USER_ID", USER_ID)
241                 .put(Entity.json(refs));
242
243         assertEquals(HttpStatus.OK_200, response.getStatus());
244     }
245
246     @Test
247     public void testBatchDissociateRIFromRIFailure() {
248
249         String componentId = "componentId";
250         String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
251         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/resourceInstance/batchDissociate";
252
253         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
254         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
255
256         Response response = target()
257                 .path(path)
258                 .request(MediaType.APPLICATION_JSON)
259                 .header("USER_ID", USER_ID)
260                 .put(Entity.json(""));
261
262         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
263     }
264
265     @Override
266     protected ResourceConfig configure() {
267         forceSet(TestProperties.CONTAINER_PORT, "0");
268         ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
269         return new ResourceConfig(ComponentInstanceServlet.class)
270                 .register(new AbstractBinder() {
271                     @Override
272                     protected void configure() {
273                         bind(request).to(HttpServletRequest.class);
274                     }
275                 })
276                 .property("contextConfig", context);
277     }
278
279     private static void createMocks() {
280         request = Mockito.mock(HttpServletRequest.class);
281         session = Mockito.mock(HttpSession.class);
282         servletContext = Mockito.mock(ServletContext.class);
283         webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
284         webApplicationContext = Mockito.mock(WebApplicationContext.class);
285         componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
286         componentsUtils = Mockito.mock(ComponentsUtils.class);
287         servletUtils = Mockito.mock(ServletUtils.class);
288         responseFormat = Mockito.mock(ResponseFormat.class);
289     }
290
291     private static void stubMethods() {
292         when(request.getSession()).thenReturn(session);
293         when(session.getServletContext()).thenReturn(servletContext);
294         when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
295         when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
296         when(webApplicationContext.getBean(ComponentInstanceBusinessLogic.class)).thenReturn(componentInstanceBusinessLogic);
297         when(request.getHeader("USER_ID")).thenReturn(USER_ID);
298         when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
299         when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils);
300     }
301 }