1 package org.openecomp.sdc.be.servlets;
4 import java.util.HashMap;
7 import java.util.ArrayList;
8 import javax.ws.rs.client.Entity;
9 import org.eclipse.jetty.http.HttpStatus;
10 import org.glassfish.hk2.utilities.binding.AbstractBinder;
11 import org.glassfish.jersey.server.ResourceConfig;
12 import org.glassfish.jersey.test.JerseyTest;
13 import org.glassfish.jersey.test.TestProperties;
14 import org.junit.BeforeClass;
15 import org.junit.Test;
16 import org.mockito.ArgumentMatchers;
17 import org.mockito.Mockito;
18 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
19 import org.openecomp.sdc.be.config.SpringConfig;
20 import org.openecomp.sdc.be.dao.api.ActionStatus;
21 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
22 import org.openecomp.sdc.be.impl.ComponentsUtils;
23 import org.openecomp.sdc.be.impl.ServletUtils;
24 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
25 import org.openecomp.sdc.be.model.CapabilityDefinition;
26 import org.openecomp.sdc.be.model.ComponentInstance;
27 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
28 import org.openecomp.sdc.be.model.RequirementDefinition;
29 import org.openecomp.sdc.common.api.Constants;
30 import org.openecomp.sdc.exception.ResponseFormat;
31 import org.springframework.context.ApplicationContext;
32 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
33 import org.springframework.web.context.WebApplicationContext;
34 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
35 import org.openecomp.sdc.be.model.User;
37 import javax.servlet.ServletContext;
38 import javax.servlet.http.HttpServletRequest;
39 import javax.servlet.http.HttpSession;
40 import javax.ws.rs.core.MediaType;
41 import javax.ws.rs.core.Response;
43 import static org.junit.Assert.assertEquals;
44 import static org.mockito.ArgumentMatchers.any;
45 import static org.mockito.ArgumentMatchers.eq;
46 import static org.mockito.Mockito.when;
47 import static org.mockito.ArgumentMatchers.anyString;
48 import static org.mockito.ArgumentMatchers.nullable;
51 * The test suite designed for test functionality of ComponentInstanceServlet class
53 public class ComponentInstanceServletTest extends JerseyTest {
55 private final static String USER_ID = "jh0003";
56 private static HttpServletRequest request;
57 private static HttpSession session;
58 private static ServletContext servletContext;
59 private static WebAppContextWrapper webAppContextWrapper;
60 private static WebApplicationContext webApplicationContext;
61 private static ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
62 private static ComponentsUtils componentsUtils;
63 private static ServletUtils servletUtils;
64 private static ResponseFormat responseFormat;
67 public static void setup() {
73 public void testGetRelationByIdSuccess(){
75 String containerComponentType = "resources";
76 String componentId = "componentId";
77 String relationId = "relationId";
78 String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/" + relationId + "/relationId";
79 Either<RequirementCapabilityRelDef, ResponseFormat> successResponse = Either.left(new RequirementCapabilityRelDef());
80 when(componentInstanceBusinessLogic.getRelationById(eq(componentId), eq(relationId), eq(USER_ID), eq(ComponentTypeEnum.RESOURCE))).thenReturn(successResponse);
81 when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
82 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
83 Response response = target()
85 .request(MediaType.APPLICATION_JSON)
86 .header("USER_ID", USER_ID)
87 .get( Response.class);
89 assertEquals(response.getStatus(), HttpStatus.OK_200);
93 public void testGetRelationByIdFailure(){
95 String containerComponentType = "unknown_type";
96 String componentId = "componentId";
97 String relationId = "relationId";
98 String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/" + relationId + "/relationId";
99 when(responseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400);
100 when(componentsUtils.getResponseFormat(eq(ActionStatus.UNSUPPORTED_ERROR), eq(containerComponentType))).thenReturn(responseFormat);
101 Response response = target()
103 .request(MediaType.APPLICATION_JSON)
104 .header("USER_ID", USER_ID)
105 .get( Response.class);
107 assertEquals(response.getStatus(), HttpStatus.BAD_REQUEST_400);
111 public void testCopyComponentInstanceSuccess(){
113 String componentId = "componentId";
114 String componentInstanceId = "componentInstanceId";
115 String path = "/v1/catalog/services/" + componentId + "/copyComponentInstance/" + componentInstanceId;
117 Either<Map<String, ComponentInstance>, ResponseFormat> successResponse = Either.left(new HashMap<String, ComponentInstance>());
118 when(componentInstanceBusinessLogic.copyComponentInstance(any(ComponentInstance.class), eq(componentId), eq(componentInstanceId), eq(USER_ID))).thenReturn(successResponse);
119 when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
120 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
122 ComponentInstance c = new ComponentInstance();
124 c.setUniqueId("comp1");
125 c.setComponentUid("comp1");
128 c.setCapabilities(new HashMap<String, List<CapabilityDefinition>>());
129 c.setRequirements(new HashMap<String, List<RequirementDefinition>>());
131 Response response = target()
133 .request(MediaType.APPLICATION_JSON)
134 .header("USER_ID", USER_ID)
135 .post(Entity.json(c));
137 assertEquals(response.getStatus(), HttpStatus.OK_200);
141 public void testBatchDeleteResourceInstancesSuccess() {
143 String componentId = "componentId";
144 String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
145 String compId1 = "compId1";
146 String[] delCompIds = new String[1];
147 delCompIds[0] = compId1;
148 List<ComponentInstance> compInsts = new ArrayList<ComponentInstance>();
149 String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/batchDeleteResourceInstances";
151 ComponentInstance compInst = new ComponentInstance();
152 compInst.setName(compId1);
153 compInst.setUniqueId(compId1);
154 compInst.setComponentUid(compId1);
155 compInst.setInvariantName(compId1);
156 compInsts.add(compInst);
158 when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
159 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
160 Either<String[], ResponseFormat> convertStatusEither = Either.left(delCompIds);
162 .convertJsonToObjectUsingObjectMapper(anyString(), any(User.class), ArgumentMatchers.<Class<String[]>>any(),
163 nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))).thenReturn(convertStatusEither);
164 when(componentInstanceBusinessLogic
165 .batchDeleteComponentInstance(eq(containerComponentType), eq(componentId), any(List.class),
166 eq(USER_ID))).thenReturn(Mockito.mock(Map.class));
168 Response response = target()
170 .request(MediaType.APPLICATION_JSON)
171 .header("USER_ID", USER_ID)
172 .post(Entity.json(compInsts));
174 assertEquals(HttpStatus.OK_200, response.getStatus());
178 public void testBatchDeleteResourceInstancesFailure() {
180 String componentId = "componentId";
181 String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
182 String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/batchDeleteResourceInstances";
184 when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
185 when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
187 Response response = target()
189 .request(MediaType.APPLICATION_JSON)
190 .header("USER_ID", USER_ID)
191 .post(Entity.json(""));
193 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
197 public void testBatchDissociateRIFromRISuccess() {
199 String componentId = "componentId";
200 String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
201 String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/resourceInstance/batchDissociate";
202 RequirementCapabilityRelDef[] refs = new RequirementCapabilityRelDef[1];
203 RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
206 when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
207 when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
208 Either<RequirementCapabilityRelDef[], ResponseFormat> convertReqEither = Either.left(refs);
209 when(componentsUtils.convertJsonToObjectUsingObjectMapper(anyString(), any(User.class),
210 ArgumentMatchers.<Class<RequirementCapabilityRelDef[]>>any(),
211 nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))).thenReturn(convertReqEither);
212 Either<RequirementCapabilityRelDef, ResponseFormat> actionResponseEither = Either.left(ref);
213 when(componentInstanceBusinessLogic
214 .dissociateRIFromRI(componentId, USER_ID, ref, ComponentTypeEnum.findByParamName(containerComponentType)))
215 .thenReturn(actionResponseEither);
217 Response response = target()
219 .request(MediaType.APPLICATION_JSON)
220 .header("USER_ID", USER_ID)
221 .put(Entity.json(refs));
223 assertEquals(HttpStatus.OK_200, response.getStatus());
227 public void testBatchDissociateRIFromRIFailure() {
229 String componentId = "componentId";
230 String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
231 String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/resourceInstance/batchDissociate";
233 when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
234 when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
236 Response response = target()
238 .request(MediaType.APPLICATION_JSON)
239 .header("USER_ID", USER_ID)
240 .put(Entity.json(""));
242 assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
246 protected ResourceConfig configure() {
247 forceSet(TestProperties.CONTAINER_PORT, "0");
248 ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
249 return new ResourceConfig(ComponentInstanceServlet.class)
250 .register(new AbstractBinder() {
252 protected void configure() {
253 bind(request).to(HttpServletRequest.class);
256 .property("contextConfig", context);
259 private static void createMocks() {
260 request = Mockito.mock(HttpServletRequest.class);
261 session = Mockito.mock(HttpSession.class);
262 servletContext = Mockito.mock(ServletContext.class);
263 webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
264 webApplicationContext = Mockito.mock(WebApplicationContext.class);
265 componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
266 componentsUtils = Mockito.mock(ComponentsUtils.class);
267 servletUtils = Mockito.mock(ServletUtils.class);
268 responseFormat = Mockito.mock(ResponseFormat.class);
271 private static void stubMethods() {
272 when(request.getSession()).thenReturn(session);
273 when(session.getServletContext()).thenReturn(servletContext);
274 when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
275 when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
276 when(webApplicationContext.getBean(ComponentInstanceBusinessLogic.class)).thenReturn(componentInstanceBusinessLogic);
277 when(request.getHeader("USER_ID")).thenReturn(USER_ID);
278 when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
279 when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils);