Fix locally failing TCs in catalog-be
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / ComponentNodeFilterServletTest.java
1 /*
2  * ============LICENSE_START=======================================================
3  *  Copyright (C) 2020 Nordix Foundation
4  *  ================================================================================
5  *  Licensed under the Apache License, Version 2.0 (the "License");
6  *  you may not use this file except in compliance with the License.
7  *  You may obtain a copy of the License at
8  *
9  *        http://www.apache.org/licenses/LICENSE-2.0
10  *  Unless required by applicable law or agreed to in writing, software
11  *  distributed under the License is distributed on an "AS IS" BASIS,
12  *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  *  See the License for the specific language governing permissions and
14  *  limitations under the License.
15  *
16  *  SPDX-License-Identifier: Apache-2.0
17  *  ============LICENSE_END=========================================================
18  */
19
20 package org.openecomp.sdc.be.servlets;
21
22 import static org.assertj.core.api.Assertions.assertThat;
23 import static org.junit.Assert.assertNotNull;
24 import static org.mockito.ArgumentMatchers.any;
25 import static org.mockito.ArgumentMatchers.anyBoolean;
26 import static org.mockito.ArgumentMatchers.anyInt;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.ArgumentMatchers.nullable;
30 import static org.mockito.Mockito.doReturn;
31 import static org.mockito.Mockito.times;
32 import static org.mockito.Mockito.verify;
33 import static org.mockito.Mockito.when;
34 import static org.openecomp.sdc.common.api.Constants.USER_ID_HEADER;
35
36 import com.fasterxml.jackson.core.JsonProcessingException;
37 import com.fasterxml.jackson.databind.ObjectMapper;
38 import java.util.Arrays;
39 import java.util.LinkedList;
40 import java.util.Optional;
41 import javax.servlet.ServletContext;
42 import javax.servlet.http.HttpServletRequest;
43 import javax.servlet.http.HttpSession;
44 import javax.ws.rs.client.Entity;
45 import javax.ws.rs.core.MediaType;
46 import javax.ws.rs.core.Response;
47 import org.eclipse.jetty.http.HttpStatus;
48 import org.glassfish.hk2.utilities.binding.AbstractBinder;
49 import org.glassfish.jersey.server.ResourceConfig;
50 import org.glassfish.jersey.test.JerseyTest;
51 import org.glassfish.jersey.test.TestProperties;
52 import org.junit.jupiter.api.AfterAll;
53 import org.junit.jupiter.api.AfterEach;
54 import org.junit.jupiter.api.BeforeAll;
55 import org.junit.jupiter.api.BeforeEach;
56 import org.junit.jupiter.api.Test;
57 import org.junit.jupiter.api.TestInstance;
58 import org.junit.jupiter.api.TestInstance.Lifecycle;
59 import org.junit.jupiter.api.extension.ExtendWith;
60 import org.mockito.ArgumentMatchers;
61 import org.mockito.Mock;
62 import org.mockito.MockitoAnnotations;
63 import org.mockito.junit.jupiter.MockitoExtension;
64 import org.openecomp.sdc.be.components.impl.ComponentInstanceBusinessLogic;
65 import org.openecomp.sdc.be.components.impl.ComponentNodeFilterBusinessLogic;
66 import org.openecomp.sdc.be.components.impl.ResourceImportManager;
67 import org.openecomp.sdc.be.components.impl.exceptions.BusinessLogicException;
68 import org.openecomp.sdc.be.components.impl.utils.NodeFilterConstraintAction;
69 import org.openecomp.sdc.be.components.validation.UserValidations;
70 import org.openecomp.sdc.be.config.ConfigurationManager;
71 import org.openecomp.sdc.be.config.SpringConfig;
72 import org.openecomp.sdc.be.dao.api.ActionStatus;
73 import org.openecomp.sdc.be.datamodel.utils.ConstraintConvertor;
74 import org.openecomp.sdc.be.datatypes.elements.CINodeFilterDataDefinition;
75 import org.openecomp.sdc.be.datatypes.elements.ListDataDefinition;
76 import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterCapabilityDataDefinition;
77 import org.openecomp.sdc.be.datatypes.elements.RequirementNodeFilterPropertyDataDefinition;
78 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
79 import org.openecomp.sdc.be.datatypes.enums.NodeFilterConstraintType;
80 import org.openecomp.sdc.be.impl.ComponentsUtils;
81 import org.openecomp.sdc.be.impl.ServletUtils;
82 import org.openecomp.sdc.be.impl.WebAppContextWrapper;
83 import org.openecomp.sdc.be.model.User;
84 import org.openecomp.sdc.be.ui.model.UIConstraint;
85 import org.openecomp.sdc.be.user.Role;
86 import org.openecomp.sdc.be.user.UserBusinessLogic;
87 import org.openecomp.sdc.common.api.ConfigurationSource;
88 import org.openecomp.sdc.common.api.Constants;
89 import org.openecomp.sdc.common.impl.ExternalConfiguration;
90 import org.openecomp.sdc.common.impl.FSConfigurationSource;
91 import org.openecomp.sdc.exception.ResponseFormat;
92 import org.springframework.context.ApplicationContext;
93 import org.springframework.context.annotation.AnnotationConfigApplicationContext;
94 import org.springframework.web.context.WebApplicationContext;
95
96 @ExtendWith(MockitoExtension.class)
97 @TestInstance(Lifecycle.PER_CLASS)
98 class ComponentNodeFilterServletTest extends JerseyTest {
99
100     private static final String USER_ID = "jh0003";
101     private static final String servicePropertyName = "resourceType";
102     private static final String constraintOperator = "equal";
103     private static final String sourceType = "static";
104     private static final String sourceName = sourceType;
105     private static final String propertyValue = "resourceTypeValue";
106     private static final String componentId = "dac65869-dfb4-40d2-aa20-084324659ec1";
107     private static final String componentInstance = "dac65869-dfb4-40d2-aa20-084324659ec1.resource0";
108     private static final String componentType = "resources";
109     private static final String capabilityName = "MyCapabilityName";
110     private static final String V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER = "/v1/catalog/%s/%s/componentInstance/%s/%s/%s/nodeFilter";
111     private static final String V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_NODE_FILTER = "/v1/catalog/%s/%s/componentInstance/%s/%s/nodeFilter";
112     private final UIConstraint uiConstraint = new UIConstraint("resourceType", "equal", "static", "static", "resourceTypeValue");
113     private final String constraint = new ConstraintConvertor().convert(uiConstraint);
114     private final String inputJson = buildConstraintDataJson(uiConstraint);
115     private final User user = new User("", "", USER_ID, "", Role.ADMIN.name(), null);
116     @Mock
117     private HttpServletRequest request;
118     @Mock
119     private HttpSession session;
120     @Mock
121     private ServletContext servletContext;
122     @Mock
123     private WebAppContextWrapper webAppContextWrapper;
124     @Mock
125     private WebApplicationContext webApplicationContext;
126     @Mock
127     private UserBusinessLogic userBusinessLogic;
128     @Mock
129     private ComponentInstanceBusinessLogic componentInstanceBusinessLogic;
130     @Mock
131     private ComponentsUtils componentsUtils;
132     @Mock
133     private ServletUtils servletUtils;
134     @Mock
135     private ResourceImportManager resourceImportManager;
136     @Mock
137     private ComponentNodeFilterBusinessLogic componentNodeFilterBusinessLogic;
138     @Mock
139     private ResponseFormat responseFormat;
140     @Mock
141     private UserValidations userValidations;
142
143     private CINodeFilterDataDefinition ciNodeFilterDataDefinition;
144
145     public ComponentNodeFilterServletTest() throws JsonProcessingException {
146     }
147
148     @BeforeAll
149     public void initClass() {
150         MockitoAnnotations.initMocks(this);
151         when(request.getSession()).thenReturn(session);
152         when(session.getServletContext()).thenReturn(servletContext);
153         when(servletContext.getAttribute(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR)).thenReturn(webAppContextWrapper);
154         when(webAppContextWrapper.getWebAppContext(servletContext)).thenReturn(webApplicationContext);
155         when(webApplicationContext.getBean(ComponentNodeFilterBusinessLogic.class)).thenReturn(componentNodeFilterBusinessLogic);
156         when(request.getHeader("USER_ID")).thenReturn(USER_ID);
157         when(webApplicationContext.getBean(ServletUtils.class)).thenReturn(servletUtils);
158         when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils);
159         final String appConfigDir = "src/test/resources/config/catalog-be";
160         final ConfigurationSource configurationSource = new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir);
161         final ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
162         final org.openecomp.sdc.be.config.Configuration configuration = new org.openecomp.sdc.be.config.Configuration();
163         configuration.setJanusGraphInMemoryGraph(true);
164         configurationManager.setConfiguration(configuration);
165         ExternalConfiguration.setAppName("catalog-be");
166     }
167
168     @BeforeEach
169     public void resetMock() throws Exception {
170         super.setUp();
171     }
172
173     @AfterEach
174     void after() throws Exception {
175         super.tearDown();
176     }
177
178     @Test
179     void addNodeFilterPropertiesSuccessTest() throws BusinessLogicException, JsonProcessingException {
180         initComponentData();
181         final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_NODE_FILTER;
182         final String path = String.format(pathFormat, componentType, componentId, componentInstance,
183             NodeFilterConstraintType.PROPERTIES_PARAM_NAME);
184
185         doReturn(user).when(componentNodeFilterBusinessLogic).validateUser(USER_ID);
186         doReturn(HttpStatus.OK_200).when(responseFormat).getStatus();
187         doReturn(responseFormat).when(componentsUtils).getResponseFormat(ActionStatus.OK);
188         doReturn(componentsUtils).when(servletUtils).getComponentsUtils();
189         assertNotNull(uiConstraint);
190         assertThat(servicePropertyName).isEqualToIgnoringCase(uiConstraint.getServicePropertyName());
191         assertThat(constraintOperator).isEqualToIgnoringCase(uiConstraint.getConstraintOperator());
192         assertThat(sourceType).isEqualToIgnoringCase(uiConstraint.getSourceType());
193         assertThat(sourceName).isEqualToIgnoringCase(uiConstraint.getSourceName());
194         assertThat(propertyValue).isEqualToIgnoringCase(uiConstraint.getValue().toString());
195
196         doReturn(Optional.of(uiConstraint)).when(componentsUtils)
197             .parseToConstraint(anyString(), any(User.class), ArgumentMatchers.any(ComponentTypeEnum.class));
198
199         assertNotNull(constraint);
200         assertNotNull(ciNodeFilterDataDefinition);
201         assertThat(ciNodeFilterDataDefinition.getProperties().getListToscaDataDefinition()).hasSize(1);
202         assertThat("resourceType: {equal: resourceTypeValue}\n").isEqualToIgnoringCase(constraint);
203
204         doReturn(Optional.of(ciNodeFilterDataDefinition)).when(componentNodeFilterBusinessLogic)
205             .addNodeFilter(componentId, componentInstance, NodeFilterConstraintAction.ADD,
206                 uiConstraint.getServicePropertyName(), constraint, true, ComponentTypeEnum.RESOURCE,
207                 NodeFilterConstraintType.PROPERTIES, "");
208
209         final Response response = target()
210             .path(path)
211             .request(MediaType.APPLICATION_JSON)
212             .header(USER_ID_HEADER, USER_ID)
213             .post(Entity.entity(inputJson, MediaType.APPLICATION_JSON));
214
215         verify(componentNodeFilterBusinessLogic, times(1))
216             .addNodeFilter(anyString(), anyString(), ArgumentMatchers.any(NodeFilterConstraintAction.class), anyString(),
217                 anyString(), anyBoolean(), ArgumentMatchers.any(ComponentTypeEnum.class),
218                 ArgumentMatchers.any(NodeFilterConstraintType.class), anyString());
219
220         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
221         verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID);
222     }
223
224     @Test
225     void addNodeFilterCapabilitiesSuccessTest() throws BusinessLogicException, JsonProcessingException {
226         initComponentData();
227         final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_NODE_FILTER;
228         final String path = String.format(pathFormat, componentType, componentId, componentInstance,
229             NodeFilterConstraintType.CAPABILITIES_PARAM_NAME);
230         final UIConstraint uiConstraint1 = new UIConstraint(uiConstraint.getServicePropertyName(), uiConstraint.getConstraintOperator(),
231             uiConstraint.getSourceType(), uiConstraint.getSourceName(), uiConstraint.getValue());
232         when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user);
233         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
234         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
235         doReturn(componentsUtils).when(servletUtils).getComponentsUtils();
236         uiConstraint1.setCapabilityName(capabilityName);
237
238         when(componentsUtils.parseToConstraint(anyString(), any(User.class), ArgumentMatchers.any(ComponentTypeEnum.class)))
239             .thenReturn(Optional.of(uiConstraint1));
240
241         assertThat(ciNodeFilterDataDefinition.getProperties().getListToscaDataDefinition()).hasSize(1);
242         when(componentNodeFilterBusinessLogic
243             .addNodeFilter(componentId, componentInstance, NodeFilterConstraintAction.ADD,
244                 uiConstraint1.getServicePropertyName(), constraint, true, ComponentTypeEnum.RESOURCE,
245                 NodeFilterConstraintType.CAPABILITIES, capabilityName))
246             .thenReturn(Optional.of(ciNodeFilterDataDefinition));
247
248         final Response response = target()
249             .path(path)
250             .request(MediaType.APPLICATION_JSON)
251             .header(USER_ID_HEADER, USER_ID)
252             .post(Entity.entity(inputJson, MediaType.APPLICATION_JSON));
253
254         verify(componentNodeFilterBusinessLogic, times(1))
255             .addNodeFilter(anyString(), anyString(), ArgumentMatchers.any(NodeFilterConstraintAction.class), anyString(),
256                 anyString(), anyBoolean(), ArgumentMatchers.any(ComponentTypeEnum.class),
257                 ArgumentMatchers.any(NodeFilterConstraintType.class), anyString());
258
259         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
260         verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID);
261     }
262
263     @Test
264     void addNodeFilterFailTest() throws BusinessLogicException, JsonProcessingException {
265         initComponentData();
266         final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_NODE_FILTER;
267         final String path = String.format(pathFormat, componentType, componentId, componentInstance,
268             NodeFilterConstraintType.PROPERTIES_PARAM_NAME);
269
270         when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user);
271
272         final Response response = target()
273             .path(path)
274             .request(MediaType.APPLICATION_JSON)
275             .header(USER_ID_HEADER, USER_ID)
276             .post(Entity.entity(inputJson, MediaType.APPLICATION_JSON));
277
278         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
279         verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID);
280     }
281
282     @Test
283     void addNodeFilterFailConstraintParseTest() throws JsonProcessingException {
284         initComponentData();
285         final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_NODE_FILTER;
286         final String path = String.format(pathFormat, componentType, componentId, componentInstance, NodeFilterConstraintType.PROPERTIES_PARAM_NAME);
287
288         when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user);
289
290         final Response response = target()
291             .path(path)
292             .request(MediaType.APPLICATION_JSON)
293             .header(USER_ID_HEADER, USER_ID)
294             .post(Entity.entity(inputJson, MediaType.APPLICATION_JSON));
295
296         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
297         verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID);
298     }
299
300     @Test
301     void addNodeFilterFailConvertTest() throws JsonProcessingException {
302         initComponentData();
303         final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_NODE_FILTER;
304         final String path = String.format(pathFormat, componentType, componentId, componentInstance,
305             NodeFilterConstraintType.PROPERTIES.getType());
306
307         when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user);
308
309         final Response response = target()
310             .path(path)
311             .request(MediaType.APPLICATION_JSON)
312             .header(USER_ID_HEADER, USER_ID)
313             .post(Entity.entity(inputJson, MediaType.APPLICATION_JSON));
314
315         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
316         verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID);
317     }
318
319     @Test
320     void updateNodeFilterPropertiesSuccessTest() throws BusinessLogicException, JsonProcessingException {
321         initComponentData();
322         final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER;
323         final String path = String.format(pathFormat, componentType, componentId, componentInstance,
324             NodeFilterConstraintType.PROPERTIES_PARAM_NAME, 0);
325
326         when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user);
327
328         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
329         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
330         doReturn(componentsUtils).when(servletUtils).getComponentsUtils();
331         doReturn(Optional.of(uiConstraint)).when(componentsUtils)
332             .parseToConstraint(anyString(), any(User.class), eq(ComponentTypeEnum.RESOURCE));
333         when(componentNodeFilterBusinessLogic
334             .updateNodeFilter(componentId, componentInstance, uiConstraint, ComponentTypeEnum.RESOURCE,
335                 NodeFilterConstraintType.PROPERTIES, 0)).thenReturn(Optional.of(ciNodeFilterDataDefinition));
336         final Response response = target()
337             .path(path)
338             .request(MediaType.APPLICATION_JSON)
339             .header(USER_ID_HEADER, USER_ID)
340             .put(Entity.entity(inputJson, MediaType.APPLICATION_JSON));
341
342         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
343         verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID);
344     }
345
346     @Test
347     void updateNodeFilterCapabilitiesSuccessTest() throws BusinessLogicException, JsonProcessingException {
348         initComponentData();
349         final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER;
350         final String path = String.format(pathFormat, componentType, componentId, componentInstance,
351             NodeFilterConstraintType.CAPABILITIES_PARAM_NAME, 0);
352
353         when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user);
354
355         when(componentsUtils.parseToConstraint(anyString(), any(User.class), ArgumentMatchers.any(ComponentTypeEnum.class)))
356             .thenReturn(Optional.of(uiConstraint));
357         doReturn(componentsUtils).when(servletUtils).getComponentsUtils();
358         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
359         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
360
361         when(componentNodeFilterBusinessLogic
362             .updateNodeFilter(componentId, componentInstance, uiConstraint,
363                 ComponentTypeEnum.RESOURCE, NodeFilterConstraintType.CAPABILITIES, 0))
364             .thenReturn(Optional.of(ciNodeFilterDataDefinition));
365
366         final Response response = target()
367             .path(path)
368             .request(MediaType.APPLICATION_JSON)
369             .header(USER_ID_HEADER, USER_ID)
370             .put(Entity.entity(inputJson, MediaType.APPLICATION_JSON));
371
372         verify(componentNodeFilterBusinessLogic, times(1))
373             .updateNodeFilter(anyString(), anyString(), ArgumentMatchers.any(UIConstraint.class),
374                 ArgumentMatchers.any(ComponentTypeEnum.class), ArgumentMatchers.any(NodeFilterConstraintType.class),
375                 anyInt());
376
377         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
378         verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID);
379     }
380
381     @Test
382     void updateNodeFilterFailTest() throws BusinessLogicException, JsonProcessingException {
383         initComponentData();
384         final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER;
385         final String path = String.format(pathFormat, componentType, componentId, componentInstance,
386             NodeFilterConstraintType.PROPERTIES_PARAM_NAME, 0);
387
388         when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user);
389
390         final Response response = target()
391             .path(path)
392             .request(MediaType.APPLICATION_JSON)
393             .header(USER_ID_HEADER, USER_ID)
394             .put(Entity.entity(inputJson, MediaType.APPLICATION_JSON));
395
396         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
397         verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID);
398     }
399
400     @Test
401     void updateNodeFilterFailConstraintParseTest() throws JsonProcessingException {
402         initComponentData();
403         final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER;
404         final String path = String.format(pathFormat, componentType, componentId, componentInstance,
405             NodeFilterConstraintType.PROPERTIES_PARAM_NAME, 0);
406
407         when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user);
408
409         final Response response = target()
410             .path(path)
411             .request(MediaType.APPLICATION_JSON)
412             .header(USER_ID_HEADER, USER_ID)
413             .put(Entity.entity(inputJson, MediaType.APPLICATION_JSON));
414
415         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
416         verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID);
417     }
418
419     @Test
420     void updateNodeFilterFailConvertTest() throws JsonProcessingException {
421         initComponentData();
422         final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER;
423         final String path = String.format(pathFormat, componentType, componentId, componentInstance,
424             NodeFilterConstraintType.PROPERTIES_PARAM_NAME, 0);
425
426         when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user);
427
428         final Response response = target()
429             .path(path)
430             .request(MediaType.APPLICATION_JSON)
431             .header(USER_ID_HEADER, USER_ID)
432             .put(Entity.entity(inputJson, MediaType.APPLICATION_JSON));
433
434         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
435         verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID);
436     }
437
438     @Test
439     void deleteNodeFilterSuccessTest() throws BusinessLogicException, JsonProcessingException {
440         initComponentData();
441         final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER;
442         final String path = String.format(pathFormat, componentType, componentId, componentInstance,
443             NodeFilterConstraintType.PROPERTIES_PARAM_NAME, 0);
444
445         when(componentNodeFilterBusinessLogic.validateUser(USER_ID)).thenReturn(user);
446         doReturn(componentsUtils).when(servletUtils).getComponentsUtils();
447         when(responseFormat.getStatus()).thenReturn(HttpStatus.OK_200);
448         when(componentsUtils.getResponseFormat(ActionStatus.OK)).thenReturn(responseFormat);
449
450         when(componentNodeFilterBusinessLogic.deleteNodeFilter(componentId, componentInstance,
451             NodeFilterConstraintAction.DELETE, null, 0, true, ComponentTypeEnum.RESOURCE,
452             NodeFilterConstraintType.PROPERTIES))
453             .thenReturn(Optional.of(ciNodeFilterDataDefinition));
454
455         final Response response = target()
456             .path(path)
457             .request(MediaType.APPLICATION_JSON)
458             .accept(MediaType.APPLICATION_JSON)
459             .header(USER_ID_HEADER, USER_ID)
460             .delete(Response.class);
461
462         verify(componentNodeFilterBusinessLogic, times(1))
463             .deleteNodeFilter(anyString(), anyString(), ArgumentMatchers.any(NodeFilterConstraintAction.class),
464                 nullable(String.class), anyInt(), anyBoolean(), ArgumentMatchers.any(ComponentTypeEnum.class),
465                 ArgumentMatchers.any(NodeFilterConstraintType.class));
466
467         assertThat(response.getStatus()).isEqualTo(HttpStatus.OK_200);
468         verify(componentNodeFilterBusinessLogic,times(1)).validateUser(USER_ID);
469     }
470
471     @Test
472     void deleteNodeFilterFailTest() {
473         final String pathFormat = V_1_CATALOG_S_S_COMPONENT_INSTANCE_S_S_S_NODE_FILTER;
474         final String path = String.format(pathFormat, componentType, componentId, componentInstance,
475             NodeFilterConstraintType.PROPERTIES_PARAM_NAME, 0);
476         final Response response = target()
477             .path(path)
478             .request(MediaType.APPLICATION_JSON)
479             .accept(MediaType.APPLICATION_JSON)
480             .header(USER_ID_HEADER, USER_ID)
481             .delete(Response.class);
482
483         assertThat(response.getStatus()).isEqualTo(HttpStatus.INTERNAL_SERVER_ERROR_500);
484     }
485
486     @Override
487     protected ResourceConfig configure() {
488         forceSet(TestProperties.CONTAINER_PORT, "0");
489         final ApplicationContext context = new AnnotationConfigApplicationContext(SpringConfig.class);
490         return new ResourceConfig(ComponentNodeFilterServlet.class)
491             .register(new AbstractBinder() {
492                 @Override
493                 protected void configure() {
494                     bind(request).to(HttpServletRequest.class);
495                     bind(userBusinessLogic).to(UserBusinessLogic.class);
496                     bind(componentInstanceBusinessLogic).to(ComponentInstanceBusinessLogic.class);
497                     bind(componentsUtils).to(ComponentsUtils.class);
498                     bind(servletUtils).to(ServletUtils.class);
499                     bind(resourceImportManager).to(ResourceImportManager.class);
500                     bind(componentNodeFilterBusinessLogic).to(ComponentNodeFilterBusinessLogic.class);
501                 }
502             })
503             .property("contextConfig", context);
504     }
505
506     private void initComponentData() throws JsonProcessingException {
507         final RequirementNodeFilterPropertyDataDefinition requirementNodeFilterPropertyDataDefinition =
508             new RequirementNodeFilterPropertyDataDefinition();
509         requirementNodeFilterPropertyDataDefinition.setName(uiConstraint.getServicePropertyName());
510         requirementNodeFilterPropertyDataDefinition.setConstraints(new LinkedList<>(Arrays.asList(constraint)));
511
512         final ListDataDefinition<RequirementNodeFilterPropertyDataDefinition> propertyDataDefinitionList =
513             new ListDataDefinition<>(new LinkedList<>(Arrays.asList(requirementNodeFilterPropertyDataDefinition)));
514
515         final RequirementNodeFilterCapabilityDataDefinition requirementNodeFilterCapabilityDataDefinition =
516             new RequirementNodeFilterCapabilityDataDefinition();
517         requirementNodeFilterCapabilityDataDefinition.setName(uiConstraint.getServicePropertyName());
518         requirementNodeFilterCapabilityDataDefinition.setProperties(propertyDataDefinitionList);
519
520         final ListDataDefinition<RequirementNodeFilterCapabilityDataDefinition> capabilityDataDefinitionList =
521             new ListDataDefinition<>(new LinkedList<>(Arrays.asList(requirementNodeFilterCapabilityDataDefinition)));
522
523         ciNodeFilterDataDefinition = new CINodeFilterDataDefinition();
524         ciNodeFilterDataDefinition.setProperties(propertyDataDefinitionList);
525         ciNodeFilterDataDefinition.setCapabilities(capabilityDataDefinitionList);
526         ciNodeFilterDataDefinition.setID("NODE_FILTER_UID");
527
528     }
529
530     private String buildConstraintDataJson(final UIConstraint uiConstraint) throws JsonProcessingException {
531         final ObjectMapper mapper = new ObjectMapper();
532         return mapper.writeValueAsString(uiConstraint);
533     }
534
535 }