2 * ============LICENSE_START=======================================================
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
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 fj.data.Either;
24 import java.util.HashMap;
25 import java.util.List;
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;
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;
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;
71 * The test suite designed for test functionality of ComponentInstanceServlet class
73 public class ComponentInstanceServletTest extends JerseyTest {
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;
87 public static void setup() {
93 public void testGetRelationByIdSuccess(){
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()
105 .request(MediaType.APPLICATION_JSON)
106 .header("USER_ID", USER_ID)
107 .get( Response.class);
109 assertEquals(response.getStatus(), HttpStatus.OK_200);
113 public void testGetRelationByIdFailure(){
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()
123 .request(MediaType.APPLICATION_JSON)
124 .header("USER_ID", USER_ID)
125 .get( Response.class);
127 assertEquals(response.getStatus(), HttpStatus.BAD_REQUEST_400);
131 public void testCopyComponentInstanceSuccess(){
133 String componentId = "componentId";
134 String componentInstanceId = "componentInstanceId";
135 String path = "/v1/catalog/services/" + componentId + "/copyComponentInstance/" + componentInstanceId;
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);
142 ComponentInstance c = new ComponentInstance();
144 c.setUniqueId("comp1");
145 c.setComponentUid("comp1");
148 c.setCapabilities(new HashMap<String, List<CapabilityDefinition>>());
149 c.setRequirements(new HashMap<String, List<RequirementDefinition>>());
151 Response response = target()
153 .request(MediaType.APPLICATION_JSON)
154 .header("USER_ID", USER_ID)
155 .post(Entity.json(c));
157 assertEquals(response.getStatus(), HttpStatus.OK_200);
161 public void testBatchDeleteResourceInstancesSuccess() {
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";
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);
178 when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
179 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
180 Either<String[], ResponseFormat> convertStatusEither = Either.left(delCompIds);
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));
188 Response response = target()
190 .request(MediaType.APPLICATION_JSON)
191 .header("USER_ID", USER_ID)
192 .post(Entity.json(compInsts));
194 assertEquals(HttpStatus.OK_200, response.getStatus());
198 public void testBatchDeleteResourceInstancesFailure() {
200 String componentId = "componentId";
201 String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
202 String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/batchDeleteResourceInstances";
204 when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
205 when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
207 Response response = target()
209 .request(MediaType.APPLICATION_JSON)
210 .header("USER_ID", USER_ID)
211 .post(Entity.json(""));
213 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
217 public void testBatchDissociateRIFromRISuccess() {
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();
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);
237 Response response = target()
239 .request(MediaType.APPLICATION_JSON)
240 .header("USER_ID", USER_ID)
241 .put(Entity.json(refs));
243 assertEquals(HttpStatus.OK_200, response.getStatus());
247 public void testBatchDissociateRIFromRIFailure() {
249 String componentId = "componentId";
250 String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
251 String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/resourceInstance/batchDissociate";
253 when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
254 when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
256 Response response = target()
258 .request(MediaType.APPLICATION_JSON)
259 .header("USER_ID", USER_ID)
260 .put(Entity.json(""));
262 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
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() {
272 protected void configure() {
273 bind(request).to(HttpServletRequest.class);
276 .property("contextConfig", context);
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);
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);