Update vulnerable package dependencies
[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 static java.util.Collections.singletonList;
24 import static org.assertj.core.api.Assertions.assertThat;
25 import static org.junit.Assert.assertEquals;
26 import static org.mockito.ArgumentMatchers.any;
27 import static org.mockito.ArgumentMatchers.anyBoolean;
28 import static org.mockito.ArgumentMatchers.anyList;
29 import static org.mockito.ArgumentMatchers.anyString;
30 import static org.mockito.ArgumentMatchers.eq;
31 import static org.mockito.ArgumentMatchers.nullable;
32 import static org.mockito.Mockito.when;
33 import static org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum.SERVICE_PARAM_NAME;
34
35 import com.fasterxml.jackson.core.JsonProcessingException;
36 import com.fasterxml.jackson.databind.ObjectMapper;
37 import com.google.common.collect.Lists;
38 import fj.data.Either;
39 import java.io.ByteArrayInputStream;
40 import java.io.IOException;
41 import java.io.InputStream;
42 import java.util.ArrayList;
43 import java.util.Arrays;
44 import java.util.List;
45 import java.util.Map;
46 import java.util.Optional;
47 import javax.servlet.ReadListener;
48 import javax.servlet.ServletContext;
49 import javax.servlet.ServletInputStream;
50 import javax.servlet.http.HttpServletRequest;
51 import javax.servlet.http.HttpSession;
52 import javax.ws.rs.client.Entity;
53 import javax.ws.rs.core.MediaType;
54 import javax.ws.rs.core.Response;
55 import org.apache.commons.lang3.StringUtils;
56 import org.eclipse.jetty.http.HttpStatus;
57 import org.glassfish.hk2.utilities.binding.AbstractBinder;
58 import org.glassfish.jersey.server.ResourceConfig;
59 import org.glassfish.jersey.test.JerseyTest;
60 import org.glassfish.jersey.test.TestProperties;
61 import org.junit.jupiter.api.AfterEach;
62 import org.junit.jupiter.api.BeforeAll;
63 import org.junit.jupiter.api.BeforeEach;
64 import org.junit.jupiter.api.Test;
65 import org.junit.jupiter.api.TestInstance;
66 import org.junit.jupiter.api.TestInstance.Lifecycle;
67 import org.mockito.ArgumentMatchers;
68 import org.mockito.Mockito;
69 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
70 import org.openecomp.sdc.be.components.impl.ComponentNodeFilterBusinessLogic;
71 import org.openecomp.sdc.be.components.impl.GroupBusinessLogic;
72 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
73 import org.openecomp.sdc.be.components.impl.ServiceBusinessLogic;
74 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
75 import org.openecomp.sdc.be.components.validation.UserValidations;
76 import org.openecomp.sdc.be.config.ConfigurationManager;
77 import org.openecomp.sdc.be.config.SpringConfig;
78 import org.openecomp.sdc.be.dao.api.ActionStatus;
79 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
80 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
81 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
82 import org.openecomp.sdc.be.impl.ComponentsUtils;
83 import org.openecomp.sdc.be.impl.ServletUtils;
84 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
85 import org.openecomp.sdc.be.info.CreateAndAssotiateInfo;
86 import org.openecomp.sdc.be.model.CapabilityRequirementRelationship;
87 import org.openecomp.sdc.be.model.Component;
88 import org.openecomp.sdc.be.model.ComponentInstance;
89 import org.openecomp.sdc.be.model.ComponentInstanceInput;
90 import org.openecomp.sdc.be.model.ComponentInstanceProperty;
91 import org.openecomp.sdc.be.model.RelationshipInfo;
92 import org.openecomp.sdc.be.model.RequirementCapabilityRelDef;
93 import org.openecomp.sdc.be.model.RequirementDefinition;
94 import org.openecomp.sdc.be.model.User;
95 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
96 import org.openecomp.sdc.be.user.Role;
97 import org.openecomp.sdc.be.user.UserBusinessLogic;
98 import org.openecomp.sdc.common.api.ConfigurationSource;
99 import org.openecomp.sdc.common.api.Constants;
100 import org.openecomp.sdc.common.impl.ExternalConfiguration;
101 import org.openecomp.sdc.common.impl.FSConfigurationSource;
102 import org.openecomp.sdc.exception.ResponseFormat;
103 import org.springframework.context.ApplicationContext;
104 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
105 import org.springframework.web.context.WebApplicationContext;
106
107
108 /**
109  * The test suite designed for test functionality of ComponentInstanceServlet class
110  */
111 @TestInstance(Lifecycle.PER_CLASS)
112 class ComponentInstanceServletTest extends JerseyTest {
113
114     private static final String USER_ID = "jh0003";
115     private static final String componentId = "componentId";
116     private static final String componentInstanceId = "componentInstanceIdInstanceId";
117     public static final String INVALID_CONTENT = "InvalidContent";
118     private HttpServletRequest request;
119     private HttpSession session;
120     private ServletContext servletContext;
121     private WebAppContextWrapper webAppContextWrapper;
122     private WebApplicationContext webApplicationContext;
123     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
124     private ComponentsUtils componentsUtils;
125     private ServletUtils servletUtils;
126     private ResponseFormat responseFormat;
127     private UserBusinessLogic userBusinessLogic;
128     private UserValidations userValidations;
129     private GroupBusinessLogic groupBusinessLogic;
130     private ResourceImportManager resourceImportManager;
131     private ServiceBusinessLogic serviceBusinessLogic;
132     private ComponentNodeFilterBusinessLogic componentNodeFilterBusinessLogic;
133     private ConfigurationManager configurationManager;
134     private User user;
135     private String inputData;
136     private ComponentInstance componentInstance;
137     private CINodeFilterDataDefinition ciNodeFilterDataDefinition;
138     private RequirementCapabilityRelDef requirementCapabilityRelDef;
139
140     @BeforeAll
141     public void setup() {
142         createMocks();
143         stubMethods();
144         initTestData();
145         final String appConfigDir = "src/test/resources/config/catalog-be";
146         final ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(),
147             appConfigDir);
148         configurationManager = new ConfigurationManager(configurationSource);
149         org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration();
150         configuration.setJanusGraphInMemoryGraph(true);
151         configurationManager.setConfiguration(configuration);
152         ExternalConfiguration.setAppName("catalog-be");
153     }
154
155     @BeforeEach
156     public void before() throws Exception {
157         super.setUp();
158     }
159
160     @AfterEach
161     public void tearDown() throws Exception {
162         super.tearDown();
163     }
164
165     private void initTestData() {
166         componentInstance = getComponentInstance();
167         inputData = getInputData(componentInstance);
168         ciNodeFilterDataDefinition = getCiNodeFilterDataDefinition();
169
170         user = new User();
171         user.setUserId(USER_ID);
172         user.setRole(Role.ADMIN.name());
173
174         requirementCapabilityRelDef = new RequirementCapabilityRelDef();
175         final CapabilityRequirementRelationship capabilityRequirementRelationship = new CapabilityRequirementRelationship();
176         final RelationshipInfo relationInfo = new RelationshipInfo();
177         relationInfo.setId("RELATION_ID");
178         capabilityRequirementRelationship.setRelation(relationInfo);
179         requirementCapabilityRelDef.setRelationships(Lists.newArrayList(capabilityRequirementRelationship));
180         requirementCapabilityRelDef.setToNode("TO_INSTANCE_ID");
181         requirementCapabilityRelDef.setFromNode("FROM_INSTANCE_ID");
182     }
183
184     @Test
185     void testGetRelationByIdSuccess() {
186
187         String containerComponentType = "resources";
188         String componentId = "componentId";
189         String relationId = "relationId";
190         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/" + relationId + "/relationId";
191         Either<RequirementCapabilityRelDef, ResponseFormat> successResponse = Either
192             .left(new RequirementCapabilityRelDef());
193         when(componentInstanceBusinessLogic
194             .getRelationById(eq(componentId), eq(relationId), eq(USER_ID), eq(ComponentTypeEnum.RESOURCE)))
195             .thenReturn(successResponse);
196         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
197         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
198         Response response = target()
199             .path(path)
200             .request(MediaType.APPLICATION_JSON)
201             .accept(MediaType.APPLICATION_JSON)
202             .header("USER_ID", USER_ID)
203             .get(Response.class);
204
205         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
206     }
207
208     @Test
209     void testGetRelationByIdFailure() {
210
211         String containerComponentType = "unknown_type";
212         String relationId = "relationId";
213         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/" + relationId + "/relationId";
214         when(responseFormat.getStatus()).thenReturn(HttpStatus.BAD_REQUEST_400);
215         when(componentsUtils.getResponseFormat(eq(ActionStatus.UNSUPPORTED_ERROR), eq(containerComponentType)))
216             .thenReturn(responseFormat);
217         Response response = target()
218             .path(path)
219             .request(MediaType.APPLICATION_JSON)
220             .header("USER_ID", USER_ID)
221             .get(Response.class);
222
223         assertEquals(HttpStatus.BAD_REQUEST_400, response.getStatus());
224     }
225
226     @Test
227     void testBatchDeleteResourceInstancesSuccess() {
228
229         String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
230         String compId1 = "compId1";
231         String[] delCompIds = new String[1];
232         delCompIds[0] = compId1;
233         List<ComponentInstance> compInsts = new ArrayList<ComponentInstance>();
234         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/batchDeleteResourceInstances";
235
236         ComponentInstance compInst = new ComponentInstance();
237         compInst.setName(compId1);
238         compInst.setUniqueId(compId1);
239         compInst.setComponentUid(compId1);
240         compInst.setInvariantName(compId1);
241         compInsts.add(compInst);
242
243         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
244         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
245         Either<String[], ResponseFormat> convertStatusEither = Either.left(delCompIds);
246         when(componentsUtils
247             .convertJsonToObjectUsingObjectMapper(anyString(), any(User.class), ArgumentMatchers.<Class<String[]>>any(),
248                 nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))).thenReturn(convertStatusEither);
249         when(componentInstanceBusinessLogic
250             .batchDeleteComponentInstance(eq(containerComponentType), eq(componentId), any(List.class),
251                 eq(USER_ID))).thenReturn(Mockito.mock(Map.class));
252
253         Response response = target()
254             .path(path)
255             .request(MediaType.APPLICATION_JSON)
256             .header("USER_ID", USER_ID)
257             .post(Entity.json(compInsts));
258
259         assertEquals(HttpStatus.OK_200, response.getStatus());
260     }
261
262     @Test
263     void testBatchDeleteResourceInstancesFailure() {
264
265         String componentId = "componentId";
266         String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
267         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/batchDeleteResourceInstances";
268
269         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
270         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
271
272         Response response = target()
273             .path(path)
274             .request(MediaType.APPLICATION_JSON)
275             .header("USER_ID", USER_ID)
276             .post(Entity.json(""));
277
278         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
279     }
280
281     @Test
282     void testBatchDissociateRIFromRISuccess() {
283
284         String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
285         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/resourceInstance/batchDissociate";
286         RequirementCapabilityRelDef[] refs = new RequirementCapabilityRelDef[1];
287         RequirementCapabilityRelDef ref = new RequirementCapabilityRelDef();
288         refs[0] = ref;
289
290         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
291         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
292         Either<RequirementCapabilityRelDef[], ResponseFormat> convertReqEither = Either.left(refs);
293         when(componentsUtils.convertJsonToObjectUsingObjectMapper(anyString(), any(User.class),
294             ArgumentMatchers.<Class<RequirementCapabilityRelDef[]>>any(),
295             nullable(AuditingActionEnum.class), nullable(ComponentTypeEnum.class))).thenReturn(convertReqEither);
296         RequirementCapabilityRelDef actionResponseEither = ref;
297         when(componentInstanceBusinessLogic
298             .dissociateRIFromRI(componentId, USER_ID, ref, ComponentTypeEnum.findByParamName(containerComponentType)))
299             .thenReturn(actionResponseEither);
300
301         Response response = target()
302             .path(path)
303             .request(MediaType.APPLICATION_JSON)
304             .header("USER_ID", USER_ID)
305             .put(Entity.json(refs));
306
307         assertEquals(HttpStatus.OK_200, response.getStatus());
308     }
309
310     @Test
311     void testBatchDissociateRIFromRIFailure() {
312
313         String containerComponentType = ComponentTypeEnum.SERVICE_PARAM_NAME;
314         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/resourceInstance/batchDissociate";
315
316         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
317         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
318
319         Response response = target()
320             .path(path)
321             .request(MediaType.APPLICATION_JSON)
322             .header("USER_ID", USER_ID)
323             .put(Entity.json(""));
324
325         assertEquals(HttpStatus.INTERNAL_SERVER_ERROR_500, response.getStatus());
326     }
327
328     @Override
329     protected ResourceConfig configure() {
330         forceSet(TestProperties.CONTAINER_PORT, "0");
331         ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
332         return new ResourceConfig(ComponentInstanceServlet.class)
333             .register(new AbstractBinder() {
334                 @Override
335                 protected void configure() {
336                     bind(request).to(HttpServletRequest.class);
337                     bind(userBusinessLogic).to(UserBusinessLogic.class);
338                     bind(groupBusinessLogic).to(GroupBusinessLogic.class);
339                     bind(componentInstanceBusinessLogic).to(ComponentInstanceBusinessLogic.class);
340                     bind(componentsUtils).to(ComponentsUtils.class);
341                     bind(servletUtils).to(ServletUtils.class);
342                     bind(resourceImportManager).to(ResourceImportManager.class);
343                     bind(serviceBusinessLogic).to(ServiceBusinessLogic.class);
344                     bind(componentNodeFilterBusinessLogic).to(ComponentNodeFilterBusinessLogic.class);
345                 }
346             })
347             .property("contextConfig", context);
348     }
349
350     private void createMocks() {
351         request = Mockito.mock(HttpServletRequest.class);
352         session = Mockito.mock(HttpSession.class);
353         servletContext = Mockito.mock(ServletContext.class);
354         webAppContextWrapper = Mockito.mock(WebAppContextWrapper.class);
355         webApplicationContext = Mockito.mock(WebApplicationContext.class);
356         userBusinessLogic = Mockito.mock(UserBusinessLogic.class);
357         groupBusinessLogic = Mockito.mock(GroupBusinessLogic.class);
358         resourceImportManager = Mockito.mock(ResourceImportManager.class);
359         componentInstanceBusinessLogic = Mockito.mock(ComponentInstanceBusinessLogic.class);
360         componentsUtils = Mockito.mock(ComponentsUtils.class);
361         servletUtils = Mockito.mock(ServletUtils.class);
362         responseFormat = Mockito.mock(ResponseFormat.class);
363         serviceBusinessLogic = Mockito.mock(ServiceBusinessLogic.class);
364         componentNodeFilterBusinessLogic = Mockito.mock(ComponentNodeFilterBusinessLogic.class);
365         userValidations = Mockito.mock(UserValidations.class);
366     }
367
368     private void stubMethods() {
369         when(request.getSession()).thenReturn(session);
370         when(session.getServletContext()).thenReturn(servletContext);
371         when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR))
372             .thenReturn(webAppContextWrapper);
373         when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
374         when(webApplicationContext.getBean(ComponentInstanceBusinessLogic.class))
375             .thenReturn(componentInstanceBusinessLogic);
376         when(request.getHeader("USER_ID")).thenReturn(USER_ID);
377         when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
378         when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils);
379     }
380
381     @Test
382     void testUpdateResourceInstancePropertiesSuccess() {
383
384         String containerComponentType = "services";
385         String resourceInstanceId = "resourceInstanceId";
386         ComponentInstanceProperty[] properties = new ComponentInstanceProperty[1];
387         ComponentInstanceProperty property = new ComponentInstanceProperty();
388         property.setName("property");
389         property.setValue("value");
390         property.setType("String");
391         properties[0] = (property);
392         ObjectMapper mapper = new ObjectMapper();
393         String propertyJson = null;
394         try {
395             propertyJson = mapper.writeValueAsString(properties);
396         } catch (JsonProcessingException e) {
397             e.printStackTrace();
398         }
399         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/resourceInstance" + "/" +
400             resourceInstanceId + "/properties";
401         when(componentsUtils
402             .convertJsonToObjectUsingObjectMapper(propertyJson, new User(), ComponentInstanceProperty[].class,
403                 null, ComponentTypeEnum.RESOURCE_INSTANCE)).thenReturn(Either.left(properties));
404         when(componentInstanceBusinessLogic
405             .createOrUpdatePropertiesValues(eq(ComponentTypeEnum.findByParamName(SERVICE_PARAM_NAME)),
406                 eq(componentId), eq(resourceInstanceId), eq(Arrays.asList(properties)), eq(USER_ID)))
407             .thenReturn(Either.left(Arrays.asList(properties)));
408         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
409         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
410         Response response = target()
411             .path(path)
412             .request(MediaType.APPLICATION_JSON)
413             .header("USER_ID", USER_ID).post(Entity.entity(properties, MediaType.APPLICATION_JSON));
414         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
415     }
416
417     @Test
418     void testUpdateResourceInstanceInputsSuccess() {
419
420         String containerComponentType = "services";
421         String resourceInstanceId = "resourceInstanceId";
422         ComponentInstanceInput[] inputs = new ComponentInstanceInput[1];
423         ComponentInstanceInput input = new ComponentInstanceInput();
424         input.setName("input");
425         input.setValue("value");
426         input.setType("String");
427         inputs[0] = (input);
428         ObjectMapper mapper = new ObjectMapper();
429         String inputJson = null;
430         try {
431             inputJson = mapper.writeValueAsString(inputs);
432         } catch (JsonProcessingException e) {
433             e.printStackTrace();
434         }
435         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/resourceInstance" + "/" +
436             resourceInstanceId + "/inputs";
437         when(componentsUtils.convertJsonToObjectUsingObjectMapper(inputJson, new User(), ComponentInstanceInput[].class,
438             null, ComponentTypeEnum.RESOURCE_INSTANCE)).thenReturn(Either.left(inputs));
439         when(componentInstanceBusinessLogic
440             .createOrUpdateInstanceInputValues(eq(ComponentTypeEnum.findByParamName(SERVICE_PARAM_NAME)),
441                 eq(componentId), eq(resourceInstanceId), eq(Arrays.asList(inputs)), eq(USER_ID)))
442             .thenReturn(Either.left(Arrays.asList(inputs)));
443         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
444         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
445         Response response = target()
446             .path(path)
447             .request(MediaType.APPLICATION_JSON)
448             .header("USER_ID", USER_ID).post(Entity.entity(inputs, MediaType.APPLICATION_JSON));
449         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
450     }
451
452     @Test
453     void testUpdateResourceInstancePropertiesFailure() {
454
455         String containerComponentType = "services";
456         String resourceInstanceId = "resourceInstanceId";
457         ComponentInstanceProperty[] properties = new ComponentInstanceProperty[1];
458         ComponentInstanceProperty property = new ComponentInstanceProperty();
459         property.setName("property");
460         property.setValue("value");
461         property.setType("String");
462         properties[0] = (property);
463         ObjectMapper mapper = new ObjectMapper();
464         String propertyJson = null;
465         try {
466             propertyJson = mapper.writeValueAsString(properties);
467         } catch (JsonProcessingException e) {
468             e.printStackTrace();
469         }
470         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/resourceInstance" + "/" +
471             resourceInstanceId + "/properties";
472         when(componentsUtils
473             .convertJsonToObjectUsingObjectMapper(propertyJson, new User(), ComponentInstanceProperty[].class,
474                 null, ComponentTypeEnum.RESOURCE_INSTANCE)).thenReturn(Either.left(properties));
475         when(componentInstanceBusinessLogic
476             .createOrUpdatePropertiesValues(eq(ComponentTypeEnum.findByParamName(SERVICE_PARAM_NAME)),
477                 eq(componentId), eq(resourceInstanceId), eq(Arrays.asList(properties)), eq(USER_ID)))
478             .thenReturn(Either.right(new ResponseFormat(404)));
479         when(responseFormat.getStatus()).thenReturn(HttpStatus.NOT_FOUND_404);
480         when(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND)).thenReturn(responseFormat);
481         Response response = target()
482             .path(path)
483             .request(MediaType.APPLICATION_JSON)
484             .header("USER_ID", USER_ID).post(Entity.entity(properties, MediaType.APPLICATION_JSON));
485         assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND_404);
486     }
487
488     @Test
489     void testUpdateResourceInstanceInputsFailure() {
490
491         String containerComponentType = "services";
492         String resourceInstanceId = "resourceInstanceId";
493         ComponentInstanceInput[] inputs = new ComponentInstanceInput[1];
494         ComponentInstanceInput input = new ComponentInstanceInput();
495         input.setName("input");
496         input.setValue("value");
497         input.setType("String");
498         inputs[0] = (input);
499         ObjectMapper mapper = new ObjectMapper();
500         String inputJson = null;
501         try {
502             inputJson = mapper.writeValueAsString(inputs);
503         } catch (JsonProcessingException e) {
504             e.printStackTrace();
505         }
506         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/resourceInstance" + "/" +
507             resourceInstanceId + "/inputs";
508         when(componentsUtils.convertJsonToObjectUsingObjectMapper(inputJson, new User(), ComponentInstanceInput[].class,
509             null, ComponentTypeEnum.RESOURCE_INSTANCE)).thenReturn(Either.left(inputs));
510         when(componentInstanceBusinessLogic
511             .createOrUpdateInstanceInputValues(eq(ComponentTypeEnum.findByParamName(SERVICE_PARAM_NAME)),
512                 eq(componentId), eq(resourceInstanceId), eq(Arrays.asList(inputs)), eq(USER_ID)))
513             .thenReturn(Either.right(new ResponseFormat(404)));
514         when(responseFormat.getStatus()).thenReturn(HttpStatus.NOT_FOUND_404);
515         when(componentsUtils.getResponseFormat(ActionStatus.RESOURCE_NOT_FOUND)).thenReturn(responseFormat);
516         Response response = target()
517             .path(path)
518             .request(MediaType.APPLICATION_JSON)
519             .header("USER_ID", USER_ID).post(Entity.entity(inputs, MediaType.APPLICATION_JSON));
520         assertThat(response.getStatus()).isEqualTo(HttpStatus.NOT_FOUND_404);
521     }
522
523     @Test
524     void testUpdateInstanceRequirement() {
525         String containerComponentType = "services";
526         String capabilityType = "capabilityType";
527         String requirementName = "requirementName";
528         RequirementDefinition requirementDefinition = new RequirementDefinition();
529         ObjectMapper mapper = new ObjectMapper();
530         String requirementJson = null;
531         try {
532             requirementJson = mapper.writeValueAsString(requirementDefinition);
533         } catch (JsonProcessingException e) {
534             e.printStackTrace();
535         }
536         String path = "/v1/catalog/" + containerComponentType + "/" + componentId + "/componentInstances/" +
537             componentInstanceId + "/requirement/" + capabilityType + "/requirementName/" + requirementName;
538         when(componentsUtils
539             .convertJsonToObjectUsingObjectMapper(eq(requirementJson), any(User.class), eq(RequirementDefinition.class),
540                 eq(AuditingActionEnum.GET_TOSCA_MODEL), eq(ComponentTypeEnum.SERVICE)))
541             .thenReturn(Either.left(requirementDefinition));
542         when(componentInstanceBusinessLogic.updateInstanceRequirement(ComponentTypeEnum.SERVICE,
543             componentId, componentInstanceId, requirementDefinition, USER_ID))
544             .thenReturn(Either.left(requirementDefinition));
545         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
546         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
547
548         Response response = target()
549             .path(path)
550             .request(MediaType.APPLICATION_JSON)
551             .header("USER_ID", USER_ID).put(Entity.entity(requirementDefinition, MediaType.APPLICATION_JSON));
552         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
553     }
554
555     @Test
556     void createComponentInstanceSuccessTest() {
557         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance";
558         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId);
559         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
560         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
561         when(componentInstanceBusinessLogic.
562             createComponentInstance(anyString(), anyString(), anyString(), any(ComponentInstance.class)))
563             .thenReturn(componentInstance);
564         when(responseFormat.getStatus()).thenReturn(HttpStatus.CREATED_201);
565         when(componentsUtils.getResponseFormat(ActionStatus.CREATED)).thenReturn(responseFormat);
566         final Response response = target()
567             .path(path)
568             .request(MediaType.APPLICATION_JSON)
569             .header("USER_ID", USER_ID).post(Entity.entity(inputData, MediaType.APPLICATION_JSON));
570         assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED_201);
571     }
572
573     @Test
574     void createComponentInstanceFailWithEmptyContentTest() {
575         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance";
576         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId);
577         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
578         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
579         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
580         when(componentsUtils.getResponseFormat(ActionStatus.MISSING_BODY)).thenReturn(responseFormat);
581         final Response response = target()
582             .path(path)
583             .request(MediaType.APPLICATION_JSON)
584             .header("USER_ID", USER_ID).post(Entity.entity("", MediaType.APPLICATION_JSON));
585         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
586     }
587
588     @Test
589     void createComponentInstanceFailWithInvalidContentTest() {
590         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance";
591         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId);
592         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
593         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
594         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
595         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
596         final Response response = target()
597             .path(path)
598             .request(MediaType.APPLICATION_JSON)
599             .header("USER_ID", USER_ID).post(Entity.entity(INVALID_CONTENT, MediaType.APPLICATION_JSON));
600         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
601     }
602
603     @Test
604     void updateComponentInstanceMetadataSuccessTest() throws IOException {
605         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/%s";
606         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId, componentInstanceId);
607         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
608         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
609         when(request.getInputStream()).thenReturn(new TestServletInputStream(inputData));
610         when(componentsUtils
611             .convertJsonToObjectUsingObjectMapper(inputData, new User(), ComponentInstance.class,
612                 null, ComponentTypeEnum.RESOURCE_INSTANCE)).thenReturn(Either.left(componentInstance));
613         when(componentInstanceBusinessLogic
614             .updateComponentInstanceMetadata(anyString(), anyString(), anyString(), anyString(),
615                 any(ComponentInstance.class))).thenReturn(Either.left(componentInstance));
616         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
617         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
618         final Response response = target()
619             .path(path)
620             .request(MediaType.APPLICATION_JSON)
621             .header("USER_ID", USER_ID).post(Entity.entity(inputData, MediaType.APPLICATION_JSON));
622         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
623     }
624
625     @Test
626     void updateComponentInstanceMetadataFailWithInvalidContentTest() throws IOException {
627         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/%s";
628         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId, componentInstanceId);
629         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
630         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
631         when(request.getInputStream()).thenReturn(new TestServletInputStream(""));
632         when(componentsUtils
633             .convertJsonToObjectUsingObjectMapper(INVALID_CONTENT, new User(), ComponentInstance.class,
634                 null, ComponentTypeEnum.RESOURCE_INSTANCE)).thenReturn(Either.left(componentInstance));
635         when(responseFormat.getStatus()).thenReturn(HttpStatus.NO_CONTENT_204);
636         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
637         final Response response = target()
638             .path(path)
639             .request(MediaType.APPLICATION_JSON)
640             .header("USER_ID", USER_ID).post(Entity.entity(INVALID_CONTENT, MediaType.APPLICATION_JSON));
641         assertThat(response.getStatus()).isEqualTo(HttpStatus.NO_CONTENT_204);
642     }
643
644     @Test
645     void updateComponentInstanceMetadataFailConvertJsonDataTest() throws IOException {
646         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/%s";
647         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId, componentInstanceId);
648         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
649         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
650         when(request.getInputStream()).thenReturn(new TestServletInputStream(inputData));
651         when(componentsUtils
652             .convertJsonToObjectUsingObjectMapper(inputData, new User(), ComponentInstance.class,
653                 null, ComponentTypeEnum.RESOURCE_INSTANCE)).thenReturn(Either.right(new ResponseFormat()));
654         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
655         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(responseFormat);
656         final Response response = target()
657             .path(path)
658             .request(MediaType.APPLICATION_JSON)
659             .header("USER_ID", USER_ID).post(Entity.entity(inputData, MediaType.APPLICATION_JSON));
660         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
661     }
662
663     @Test
664     void updateComponentInstanceMetadataAndCreateNodeFilterSuccessTest() throws IOException, BusinessLogicException {
665         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/%s";
666         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId, componentInstanceId);
667         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
668         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
669         componentInstance.setDirectives(singletonList("substitutable"));
670         when(request.getInputStream()).thenReturn(new TestServletInputStream(inputData));
671         when(componentsUtils
672             .convertJsonToObjectUsingObjectMapper(inputData, new User(), ComponentInstance.class,
673                 null, ComponentTypeEnum.RESOURCE_INSTANCE)).thenReturn(Either.left(componentInstance));
674         when(componentInstanceBusinessLogic
675             .updateComponentInstanceMetadata(anyString(), anyString(), anyString(), anyString(),
676                 any(ComponentInstance.class))).thenReturn(Either.left(componentInstance));
677
678         when(componentNodeFilterBusinessLogic.createNodeFilterIfNotExist(componentId, componentInstanceId,
679             true, ComponentTypeEnum.SERVICE)).thenReturn(Optional.of(ciNodeFilterDataDefinition));
680         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
681         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
682         final Response response = target()
683             .path(path)
684             .request(MediaType.APPLICATION_JSON)
685             .header("USER_ID", USER_ID).post(Entity.entity(inputData, MediaType.APPLICATION_JSON));
686         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
687     }
688
689     @Test
690     void updateComponentInstanceMetadataAndCreateNodeFilterFailTest() throws IOException, BusinessLogicException {
691         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/%s";
692         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId, componentInstanceId);
693         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
694         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
695         componentInstance.setDirectives(singletonList("substitutable"));
696         when(request.getInputStream()).thenReturn(new TestServletInputStream(inputData));
697         when(componentsUtils
698             .convertJsonToObjectUsingObjectMapper(inputData, new User(), ComponentInstance.class,
699                 null, ComponentTypeEnum.RESOURCE_INSTANCE)).thenReturn(Either.left(componentInstance));
700         when(componentInstanceBusinessLogic
701             .updateComponentInstanceMetadata(anyString(), anyString(), anyString(), anyString(),
702                 any(ComponentInstance.class))).thenReturn(Either.left(componentInstance));
703         when(componentNodeFilterBusinessLogic.createNodeFilterIfNotExist(componentId, componentInstanceId,
704             true, ComponentTypeEnum.SERVICE)).thenReturn(Optional.empty());
705         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
706         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(responseFormat);
707         final Response response = target()
708             .path(path)
709             .request(MediaType.APPLICATION_JSON)
710             .header("USER_ID", USER_ID).post(Entity.entity(inputData, MediaType.APPLICATION_JSON));
711         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
712     }
713
714     @Test
715     void updateComponentInstanceMetadataAndDeleteNodeFilterSuccessTest() throws IOException, BusinessLogicException {
716         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/%s";
717         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId, componentInstanceId);
718         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
719         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
720         when(request.getInputStream()).thenReturn(new TestServletInputStream(inputData));
721         when(componentsUtils
722             .convertJsonToObjectUsingObjectMapper(inputData, new User(), ComponentInstance.class,
723                 null, ComponentTypeEnum.RESOURCE_INSTANCE)).thenReturn(Either.left(componentInstance));
724         when(componentInstanceBusinessLogic
725             .updateComponentInstanceMetadata(anyString(), anyString(), anyString(), anyString(),
726                 any(ComponentInstance.class))).thenReturn(Either.left(componentInstance));
727         when(componentNodeFilterBusinessLogic
728             .deleteNodeFilterIfExists(componentId, componentInstanceId, true, ComponentTypeEnum.SERVICE))
729             .thenReturn(Optional.of(ciNodeFilterDataDefinition.getName()));
730         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
731         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
732         final Response response = target()
733             .path(path)
734             .request(MediaType.APPLICATION_JSON)
735             .header("USER_ID", USER_ID).post(Entity.entity(inputData, MediaType.APPLICATION_JSON));
736         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
737     }
738
739     @Test
740     void updateComponentInstanceMetadataAndDeleteNodeFilterFailTest() throws IOException, BusinessLogicException {
741         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/%s";
742         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId, componentInstanceId);
743         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
744         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
745         when(request.getInputStream()).thenReturn(new TestServletInputStream(inputData));
746         when(componentsUtils
747             .convertJsonToObjectUsingObjectMapper(inputData, new User(), ComponentInstance.class,
748                 null, ComponentTypeEnum.RESOURCE_INSTANCE)).thenReturn(Either.left(componentInstance));
749         when(componentInstanceBusinessLogic
750             .updateComponentInstanceMetadata(anyString(), anyString(), anyString(), anyString(),
751                 any(ComponentInstance.class))).thenReturn(Either.left(componentInstance));
752         when(componentNodeFilterBusinessLogic
753             .deleteNodeFilterIfExists(componentId, componentInstanceId, true, ComponentTypeEnum.SERVICE))
754             .thenReturn(Optional.empty());
755         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
756         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(responseFormat);
757         final Response response = target()
758             .path(path)
759             .request(MediaType.APPLICATION_JSON)
760             .header("USER_ID", USER_ID).post(Entity.entity(inputData, MediaType.APPLICATION_JSON));
761         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
762     }
763
764     @Test
765     void updateMultipleComponentInstanceSuccessTest() throws IOException {
766         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/multipleComponentInstance";
767         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId);
768         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
769         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
770         final ComponentInstance[] componentInstances = new ComponentInstance[1];
771         componentInstances[0] = componentInstance;
772         when(request.getInputStream()).thenReturn(new TestServletInputStream(inputData));
773         when(componentsUtils
774             .convertJsonToObjectUsingObjectMapper(inputData, new User(), ComponentInstance[].class,
775                 null, ComponentTypeEnum.RESOURCE_INSTANCE)).thenReturn(Either.left(componentInstances));
776         when(componentInstanceBusinessLogic
777             .updateComponentInstance(anyString(), any(Component.class), anyString(), anyString(), anyList(),
778                 anyBoolean())).thenReturn(
779             singletonList(componentInstance));
780         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
781         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
782         final Response response = target()
783             .path(path)
784             .request(MediaType.APPLICATION_JSON)
785             .header("USER_ID", USER_ID).post(Entity.entity(inputData, MediaType.APPLICATION_JSON));
786         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
787     }
788
789     @Test
790     void updateMultipleComponentInstanceFailTest() throws IOException {
791         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/multipleComponentInstance";
792         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId);
793         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
794         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
795         when(request.getInputStream()).thenReturn(new TestServletInputStream(inputData));
796         when(componentsUtils
797             .convertJsonToObjectUsingObjectMapper(inputData, new User(), ComponentInstance[].class,
798                 null, ComponentTypeEnum.RESOURCE_INSTANCE)).thenReturn(Either.right(new ResponseFormat()));
799         when(componentInstanceBusinessLogic
800             .updateComponentInstance(anyString(), any(Component.class), anyString(), anyString(), anyList(),
801                 anyBoolean())).thenReturn(
802             singletonList(componentInstance));
803         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
804         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(responseFormat);
805         final Response response = target()
806             .path(path)
807             .request(MediaType.APPLICATION_JSON)
808             .header("USER_ID", USER_ID).post(Entity.entity(inputData, MediaType.APPLICATION_JSON));
809         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
810     }
811
812     @Test
813     void deleteResourceInstanceSuccessTest() throws BusinessLogicException {
814         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/%s";
815         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId, componentInstanceId);
816         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
817         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
818         when(componentInstanceBusinessLogic.deleteComponentInstance(anyString(), anyString(), anyString(), anyString()))
819             .thenReturn(componentInstance);
820         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
821         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
822         final Response response = target()
823             .path(path)
824             .request(MediaType.APPLICATION_JSON)
825             .header("USER_ID", USER_ID).delete();
826         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
827     }
828
829     @Test
830     void associateRIToRISuccessTest() {
831         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/associate";
832         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId);
833         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
834         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
835         when(componentInstanceBusinessLogic
836             .associateRIToRI(anyString(), anyString(), any(RequirementCapabilityRelDef.class),
837                 any(ComponentTypeEnum.class))).thenReturn(requirementCapabilityRelDef);
838         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
839         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
840
841         final Response response = target()
842             .path(path)
843             .request(MediaType.APPLICATION_JSON)
844             .header("USER_ID", USER_ID).post(Entity.entity(getInputData(requirementCapabilityRelDef), MediaType.APPLICATION_JSON));
845         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
846     }
847
848     @Test
849     void associateRIToRIFailWithInvalidContentTest() {
850         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/associate";
851         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId);
852         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
853         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(responseFormat);
854
855         final Response response = target()
856             .path(path)
857             .request(MediaType.APPLICATION_JSON)
858             .header("USER_ID", USER_ID).post(Entity.entity(inputData, MediaType.APPLICATION_JSON));
859         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
860     }
861
862     @Test
863     void dissociateRIFromRISuccessTest() {
864         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/dissociate";
865         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId);
866         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
867         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
868         when(componentInstanceBusinessLogic
869             .dissociateRIFromRI(anyString(), anyString(), any(RequirementCapabilityRelDef.class),
870                 any(ComponentTypeEnum.class))).thenReturn(requirementCapabilityRelDef);
871         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
872         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
873
874         final Response response = target()
875             .path(path)
876             .request(MediaType.APPLICATION_JSON)
877             .header("USER_ID", USER_ID).put(Entity.entity(getInputData(requirementCapabilityRelDef), MediaType.APPLICATION_JSON));
878         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
879     }
880
881     @Test
882     void dissociateRIFromRIFailWithInvalidContentTest() {
883         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/dissociate";
884         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId);
885         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
886         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
887         when(componentInstanceBusinessLogic
888             .dissociateRIFromRI(anyString(), anyString(), any(RequirementCapabilityRelDef.class),
889                 any(ComponentTypeEnum.class))).thenReturn(requirementCapabilityRelDef);
890         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
891         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(responseFormat);
892
893         final Response response = target()
894             .path(path)
895             .request(MediaType.APPLICATION_JSON)
896             .header("USER_ID", USER_ID).put(Entity.entity(inputData, MediaType.APPLICATION_JSON));
897         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
898     }
899
900     @Test
901     void createAndAssociateRIToRISuccessTest() throws IOException {
902         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/createAndAssociate";
903         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId);
904         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
905         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
906         final CreateAndAssotiateInfo createAndAssociateInfo = new CreateAndAssotiateInfo(componentInstance, requirementCapabilityRelDef);
907         final String data = getInputData(createAndAssociateInfo);
908         when(request.getInputStream()).thenReturn(new TestServletInputStream(data));
909         when(componentInstanceBusinessLogic.createAndAssociateRIToRI(anyString(), anyString(), anyString(), any(
910             CreateAndAssotiateInfo.class))).thenReturn(Either.left(createAndAssociateInfo));
911         when(responseFormat.getStatus()).thenReturn(HttpStatus.CREATED_201);
912         when(componentsUtils.getResponseFormat(ActionStatus.CREATED)).thenReturn(responseFormat);
913
914         final Response response = target()
915             .path(path)
916             .request(MediaType.APPLICATION_JSON)
917             .header("USER_ID", USER_ID).post(Entity.entity(data, MediaType.APPLICATION_JSON));
918         assertThat(response.getStatus()).isEqualTo(HttpStatus.CREATED_201);
919     }
920
921     @Test
922     void createAndAssociateRIToRIFailTest() throws IOException {
923         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/createAndAssociate";
924         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId);
925         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
926         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
927         final String data = getInputData(new CreateAndAssotiateInfo(componentInstance, requirementCapabilityRelDef));
928         when(request.getInputStream()).thenReturn(new TestServletInputStream(data));
929         when(componentInstanceBusinessLogic.createAndAssociateRIToRI(anyString(), anyString(), anyString(), any(
930             CreateAndAssotiateInfo.class))).thenReturn(Either.right(new ResponseFormat()));
931         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
932         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(responseFormat);
933
934         final Response response = target()
935             .path(path)
936             .request(MediaType.APPLICATION_JSON)
937             .header("USER_ID", USER_ID).post(Entity.entity(data, MediaType.APPLICATION_JSON));
938         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
939     }
940
941     @Test
942     void createAndAssociateRIToRIFailWithInvalidContentTest() throws IOException {
943         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/createAndAssociate";
944         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId);
945         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
946         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
947         when(request.getInputStream()).thenReturn(new TestServletInputStream(INVALID_CONTENT));
948         when(componentInstanceBusinessLogic.createAndAssociateRIToRI(anyString(), anyString(), anyString(), any(
949             CreateAndAssotiateInfo.class))).thenReturn(Either.right(new ResponseFormat()));
950         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
951         when(componentsUtils.getResponseFormat(ActionStatus.GENERAL_ERROR)).thenReturn(responseFormat);
952
953         final Response response = target()
954             .path(path)
955             .request(MediaType.APPLICATION_JSON)
956             .header("USER_ID", USER_ID).post(Entity.entity(INVALID_CONTENT, MediaType.APPLICATION_JSON));
957         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
958     }
959
960     @Test
961     void createAndAssociateRIToRIFailWithEmptyContentTest() throws IOException {
962         final String pathFormat = "/v1/catalog/%s/%s/resourceInstance/createAndAssociate";
963         final String path = String.format(pathFormat, SERVICE_PARAM_NAME, componentId);
964         when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
965         when(userBusinessLogic.getUser(USER_ID)).thenReturn(user);
966         final String data = StringUtils.EMPTY;
967         when(request.getInputStream()).thenReturn(new TestServletInputStream(data));
968         when(responseFormat.getStatus()).thenReturn(HttpStatus.INTERNAL_SERVER_ERROR_500);
969         when(componentsUtils.getResponseFormat(ActionStatus.INVALID_CONTENT)).thenReturn(responseFormat);
970
971         final Response response = target()
972             .path(path)
973             .request(MediaType.APPLICATION_JSON)
974             .header("USER_ID", USER_ID).post(Entity.entity(data, MediaType.APPLICATION_JSON));
975         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
976     }
977
978     private CINodeFilterDataDefinition getCiNodeFilterDataDefinition() {
979         final CINodeFilterDataDefinition ciNodeFilterDataDefinition = new CINodeFilterDataDefinition();
980         ciNodeFilterDataDefinition.setProperties(new ListDataDefinition<>());
981         ciNodeFilterDataDefinition.setID("MyNodeFilter");
982         ciNodeFilterDataDefinition.setName("NODE_FILTER_UID");
983         return ciNodeFilterDataDefinition;
984     }
985
986     private ComponentInstance getComponentInstance() {
987         final ComponentInstance componentInstance = new ComponentInstance();
988         componentInstance.setName("InstanceName");
989         componentInstance.setUniqueId(componentInstanceId);
990         componentInstance.setComponentUid("ComponentInstanceUUID");
991         componentInstance.setInvariantName("ComponentInstanceInvariantName");
992         return componentInstance;
993     }
994
995     private <T> String getInputData(final T elementToRepresent) {
996         try {
997             return new ObjectMapper().writeValueAsString(elementToRepresent);
998         } catch (final JsonProcessingException e) {
999             e.printStackTrace();
1000         }
1001         return StringUtils.EMPTY;
1002     }
1003
1004     class TestServletInputStream extends ServletInputStream {
1005
1006         private InputStream inputStream;
1007
1008         TestServletInputStream(String testJson) {
1009             inputStream = new ByteArrayInputStream(testJson.getBytes());
1010         }
1011
1012         @Override
1013         public boolean isFinished() {
1014             return false;
1015         }
1016
1017         @Override
1018         public boolean isReady() {
1019             return false;
1020         }
1021
1022         @Override
1023         public void setReadListener(ReadListener readListener) {
1024         }
1025
1026         @Override
1027         public int read() throws IOException {
1028             return inputStream.read();
1029         }
1030     }
1031 }