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