2b790a4137a7f4af7bb01023d4473a71aa0a76e3
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / servlets / PolicyServletTest.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 org.assertj.core.api.Assertions.assertThat;
24 import static org.junit.Assert.assertEquals;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyMap;
27 import static org.mockito.ArgumentMatchers.anyString;
28 import static org.mockito.ArgumentMatchers.eq;
29 import static org.mockito.Mockito.verifyNoInteractions;
30 import static org.mockito.Mockito.when;
31 import static org.openecomp.sdc.common.api.Constants.GET_POLICY;
32
33 import com.fasterxml.jackson.databind.DeserializationFeature;
34 import fj.data.Either;
35 import java.util.Arrays;
36 import java.util.Collections;
37 import java.util.HashMap;
38 import java.util.List;
39 import java.util.Objects;
40 import javax.ws.rs.client.ClientBuilder;
41 import javax.ws.rs.client.Entity;
42 import javax.ws.rs.client.Invocation;
43 import javax.ws.rs.core.GenericType;
44 import javax.ws.rs.core.MediaType;
45 import javax.ws.rs.core.Response;
46 import org.glassfish.grizzly.http.util.HttpStatus;
47 import org.glassfish.jersey.client.ClientConfig;
48 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJaxbJsonProvider;
49 import org.glassfish.jersey.jackson.internal.jackson.jaxrs.json.JacksonJsonProvider;
50 import org.glassfish.jersey.server.ResourceConfig;
51 import org.glassfish.jersey.test.TestProperties;
52 import org.json.simple.JSONObject;
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.mockito.ArgumentCaptor;
58 import org.mockito.Captor;
59 import org.mockito.Mockito;
60 import org.mockito.Spy;
61 import org.openecomp.sdc.be.components.impl.BaseBusinessLogic;
62 import org.openecomp.sdc.be.components.impl.PolicyBusinessLogic;
63 import org.openecomp.sdc.be.components.impl.ResponseFormatManager;
64 import org.openecomp.sdc.be.components.impl.aaf.RoleAuthorizationHandler;
65 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
66 import org.openecomp.sdc.be.components.impl.exceptions.ByResponseFormatComponentException;
67 import org.openecomp.sdc.be.components.property.PropertyDeclarationOrchestrator;
68 import org.openecomp.sdc.be.components.utils.PropertyDataDefinitionBuilder;
69 import org.openecomp.sdc.be.config.ConfigurationManager;
70 import org.openecomp.sdc.be.dao.api.ActionStatus;
71 import org.openecomp.sdc.be.datatypes.elements.GetPolicyValueDataDefinition;
72 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
73 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
74 import org.openecomp.sdc.be.impl.ComponentsUtils;
75 import org.openecomp.sdc.be.impl.ServletUtils;
76 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
77 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
78 import org.openecomp.sdc.be.model.PolicyDefinition;
79 import org.openecomp.sdc.be.model.PolicyTargetDTO;
80 import org.openecomp.sdc.be.model.PropertyDefinition;
81 import org.openecomp.sdc.be.model.Service;
82 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ArtifactsOperations;
83 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.InterfaceOperation;
84 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
85 import org.openecomp.sdc.be.model.operations.api.IElementOperation;
86 import org.openecomp.sdc.be.model.operations.api.IGroupInstanceOperation;
87 import org.openecomp.sdc.be.model.operations.api.IGroupOperation;
88 import org.openecomp.sdc.be.model.operations.api.IGroupTypeOperation;
89 import org.openecomp.sdc.be.model.operations.impl.InterfaceLifecycleOperation;
90 import org.openecomp.sdc.be.model.operations.impl.UniqueIdBuilder;
91 import org.openecomp.sdc.be.resources.data.auditing.AuditingActionEnum;
92 import org.openecomp.sdc.common.api.ConfigurationSource;
93 import org.openecomp.sdc.common.api.Constants;
94 import org.openecomp.sdc.common.api.FilterDecisionEnum;
95 import org.openecomp.sdc.common.impl.ExternalConfiguration;
96 import org.openecomp.sdc.common.impl.FSConfigurationSource;
97 import org.openecomp.sdc.common.util.ThreadLocalsHolder;
98 import org.openecomp.sdc.exception.ResponseFormat;
99
100 class PolicyServletTest extends JerseySpringBaseTest {
101
102     private final static String USER_ID = "jh0003";
103     private static final String COMPONENT_ID = "componentId";
104     private static PolicyBusinessLogic businessLogic;
105     private static ComponentsUtils componentsUtils;
106     private static ServletUtils servletUtils;
107     private static PropertyDeclarationOrchestrator propertyDeclarationOrchestrator;
108     private static ToscaOperationFacade toscaOperationFacade;
109     private static RoleAuthorizationHandler roleAuthorizationHandler;
110     private static ResponseFormat responseFormat;
111     @Captor
112     private static ArgumentCaptor<PolicyDefinition> policyCaptor;
113     @Spy
114     private static BaseBusinessLogic baseBusinessLogic;
115
116     private static String validComponentType = "resources";
117     private static String unsupportedComponentType = "unsupported";
118     private static String componentId = "componentId";
119     private static String policyTypeName = "policyTypeName";
120
121     private static final String PROPS_URL = "/v1/catalog/{componentType}/{serviceId}/policies/{policyId}/properties";
122     private static final String DECLARE_URL = "v1/catalog/{componentType}/{serviceId}/create/policies";
123     private static final String DELETE_URL = "v1/catalog/{containerComponentType}/{componentId}/policies/{policyId}";
124     private static final String SERVICE_ID = "serviceId";
125     private static final String POLICY_ID = "policyId";
126     private static final String PROP_1 = "prop1";
127
128     private static final String UPDATE_TARGETS_URL = "/v1/catalog/{componentType}/{componentId}/policies/{policyId}/targets";
129     static ConfigurationSource configurationSource = new FSConfigurationSource(
130         ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be");
131     static ConfigurationManager configurationManager = new ConfigurationManager(configurationSource);
132
133     @BeforeAll
134     public static void initClass() {
135         ResponseFormatManager.getInstance();
136         createMocks();
137         when(servletUtils.getComponentsUtils()).thenReturn(componentsUtils);
138     }
139
140     @BeforeEach
141     public void before() throws Exception {
142         super.setUp();
143         Mockito.reset(businessLogic);
144         final JacksonJsonProvider jacksonJsonProvider = new JacksonJaxbJsonProvider()
145             .configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
146         setClient(ClientBuilder.newClient(new ClientConfig(jacksonJsonProvider)));
147         ThreadLocalsHolder.setApiType(FilterDecisionEnum.EXTERNAL);
148         when(request.isUserInRole(anyString())).thenReturn(true);
149
150     }
151
152     @AfterEach
153     void after() throws Exception {
154         super.tearDown();
155     }
156
157     @Test
158     void testGetPolicySuccess() {
159         String path = "/v1/catalog/" + validComponentType + "/" + componentId + "/policies/" + POLICY_ID;
160         PolicyDefinition successResponse = new PolicyDefinition();
161         when(businessLogic.getPolicy(eq(ComponentTypeEnum.RESOURCE), eq(componentId), eq(POLICY_ID), eq(USER_ID)))
162             .thenReturn(successResponse);
163         Response response = target()
164             .path(path)
165             .request(MediaType.APPLICATION_JSON)
166             .header("USER_ID", USER_ID)
167             .get(Response.class);
168
169         assertEquals(response.getStatus(), HttpStatus.OK_200.getStatusCode());
170     }
171
172     @Test
173     void testGetPolicyFailure() {
174         String path = "/v1/catalog/" + unsupportedComponentType + "/" + componentId + "/policies/" + POLICY_ID;
175         Response response = target()
176             .path(path)
177             .request(MediaType.APPLICATION_JSON)
178             .header("USER_ID", USER_ID)
179             .get(Response.class);
180
181         assertEquals(response.getStatus(), HttpStatus.BAD_REQUEST_400.getStatusCode());
182     }
183
184     @Test
185     void testPostPolicySuccess() {
186         String path = "/v1/catalog/" + validComponentType + "/" + componentId + "/policies/" + policyTypeName;
187         PolicyDefinition policy = new PolicyDefinition();
188         PolicyDefinition successResponse = policy;
189         when(businessLogic
190             .createPolicy(eq(ComponentTypeEnum.RESOURCE), eq(componentId), eq(policyTypeName), eq(USER_ID), eq(true)))
191             .thenReturn(successResponse);
192         when(responseFormat.getStatus()).thenReturn(HttpStatus.CREATED_201.getStatusCode());
193         when(componentsUtils.getResponseFormat(ActionStatus.CREATED)).thenReturn(responseFormat);
194         Response response = target()
195             .path(path)
196             .request(MediaType.APPLICATION_JSON)
197             .header("USER_ID", USER_ID)
198             .post(Entity.entity(policy, MediaType.APPLICATION_JSON), Response.class);
199
200         assertEquals(response.getStatus(), HttpStatus.CREATED_201.getStatusCode());
201     }
202
203     @Test
204     void testPostPolicyFailure() {
205         String path = "/v1/catalog/" + unsupportedComponentType + "/" + componentId + "/policies/" + policyTypeName;
206         PolicyDefinition policy = new PolicyDefinition();
207         Response response = target()
208             .path(path)
209             .request(MediaType.APPLICATION_JSON)
210             .header("USER_ID", USER_ID)
211             .post(Entity.entity(policy, MediaType.APPLICATION_JSON), Response.class);
212
213         assertEquals(response.getStatus(), HttpStatus.BAD_REQUEST_400.getStatusCode());
214     }
215
216     @Test
217     void testPutPolicySuccess() {
218         String path = "/v1/catalog/" + validComponentType + "/" + componentId + "/policies/" + POLICY_ID;
219         PolicyDefinition policy = new PolicyDefinition();
220         policy.setUniqueId(POLICY_ID);
221         PolicyDefinition successResponse = policy;
222         when(businessLogic
223             .updatePolicy(eq(ComponentTypeEnum.RESOURCE), eq(componentId), any(PolicyDefinition.class), eq(USER_ID),
224                 eq(true))).thenReturn(successResponse);
225         Response response = target()
226             .path(path)
227             .request(MediaType.APPLICATION_JSON)
228             .header("USER_ID", USER_ID)
229             .put(Entity.entity(policy, MediaType.APPLICATION_JSON), Response.class);
230
231         assertEquals(response.getStatus(), HttpStatus.OK_200.getStatusCode());
232     }
233
234     @Test
235     void testPutPolicyFailure() {
236         String path = "/v1/catalog/" + unsupportedComponentType + "/" + componentId + "/policies/" + POLICY_ID;
237         PolicyDefinition policy = new PolicyDefinition();
238         Response response = target()
239             .path(path)
240             .request(MediaType.APPLICATION_JSON)
241             .header("USER_ID", USER_ID)
242             .put(Entity.entity(policy, MediaType.APPLICATION_JSON), Response.class);
243
244         assertEquals(response.getStatus(), HttpStatus.BAD_REQUEST_400.getStatusCode());
245     }
246
247     @Test
248     void testDeletePolicySuccess() {
249         String path = "/v1/catalog/" + validComponentType + "/" + componentId + "/policies/" + POLICY_ID;
250         PolicyDefinition successResponse = new PolicyDefinition();
251         when(businessLogic
252             .deletePolicy(eq(ComponentTypeEnum.RESOURCE), eq(componentId), eq(POLICY_ID), eq(USER_ID), eq(true)))
253             .thenReturn(successResponse);
254         Response response = target()
255             .path(path)
256             .request(MediaType.APPLICATION_JSON)
257             .header("USER_ID", USER_ID)
258             .delete(Response.class);
259
260         assertEquals(response.getStatus(), HttpStatus.OK_200.getStatusCode());
261     }
262
263     @Test
264     void testDeletePolicyFailure() {
265         String path = "/v1/catalog/" + unsupportedComponentType + "/" + componentId + "/policies/" + POLICY_ID;
266         Response response = target()
267             .path(path)
268             .request(MediaType.APPLICATION_JSON)
269             .header("USER_ID", USER_ID)
270             .delete(Response.class);
271
272         assertEquals(response.getStatus(), HttpStatus.BAD_REQUEST_400.getStatusCode());
273     }
274
275     @Test
276     void getPolicyProperties_operationForbidden() {
277         // doThrow(new ComponentException(ActionStatus.GENERAL_ERROR)).when(businessLogic).getPolicyProperties(ComponentTypeEnum.SERVICE, SERVICE_ID, POLICY_ID, USER_ID);
278         when(businessLogic.getPolicyProperties(ComponentTypeEnum.SERVICE, SERVICE_ID, POLICY_ID, USER_ID))
279             .thenThrow(new ByActionStatusComponentException(ActionStatus.AUTH_FAILED, USER_ID));
280         Response response = buildGetPropertiesRequest().get();
281         assertThat(response.getStatus()).isEqualTo(Response.Status.FORBIDDEN.getStatusCode());
282     }
283
284     @Test
285 //(expected = ComponentException.class)
286     void getPolicyProperties_unHandledError_returnGeneralError() {
287         when(businessLogic.getPolicyProperties(ComponentTypeEnum.SERVICE, SERVICE_ID, POLICY_ID, USER_ID))
288             .thenThrow(new RuntimeException());
289         Response response = buildGetPropertiesRequest().get();
290         assertThat(response.getStatus()).isEqualTo(Response.Status.INTERNAL_SERVER_ERROR.getStatusCode());
291     }
292
293     @Test
294     void getPolicyProperties_wrongComponentType() {
295         Response response = buildGetPropertiesRequest("unknownType").get();
296         assertThat(response.getStatus()).isEqualTo(Response.Status.BAD_REQUEST.getStatusCode());
297         verifyNoInteractions(businessLogic);
298     }
299
300     @Test
301     void getPolicyProperties() {
302         List<PropertyDataDefinition> properties = getPropertiesList();
303         when(businessLogic.getPolicyProperties(ComponentTypeEnum.SERVICE, SERVICE_ID, POLICY_ID, USER_ID))
304             .thenReturn(properties);
305         List<PropertyDataDefinition> policyProps = buildGetPropertiesRequest()
306             .get(new GenericType<List<PropertyDataDefinition>>() {
307             });
308         assertThat(policyProps)
309             .usingElementComparatorOnFields("uniqueId")
310             .containsExactlyInAnyOrder(properties.get(0), properties.get(1));
311     }
312
313     @Test
314     void updatePolicyPropertiesSuccess() {
315         List<PropertyDataDefinition> properties = getPropertiesList();
316         when(businessLogic.updatePolicyProperties(eq(ComponentTypeEnum.SERVICE), eq(SERVICE_ID), eq(POLICY_ID),
317             any(PropertyDataDefinition[].class), eq(USER_ID), eq(true))).thenReturn(properties);
318         List<PropertyDataDefinition> policyProps = buildUpdatePropertiesRequest(ComponentTypeEnum.SERVICE_PARAM_NAME,
319             properties).invoke(new GenericType<List<PropertyDataDefinition>>() {
320         });
321         assertThat(policyProps)
322             .usingElementComparatorOnFields("uniqueId")
323             .containsExactlyInAnyOrder(properties.get(0), properties.get(1));
324     }
325
326     @Test
327     void updatePolicyTargetsSuccess() {
328         List<PolicyTargetDTO> targets = getTargetDTOList();
329         when(businessLogic
330             .updatePolicyTargets(eq(ComponentTypeEnum.RESOURCE), eq(COMPONENT_ID), eq(POLICY_ID), anyMap(),
331                 eq(USER_ID))).thenReturn(new PolicyDefinition());
332         Response policyTargets = buildUpdateTargetsRequest(ComponentTypeEnum.RESOURCE_PARAM_NAME, targets).invoke();
333         assertThat(policyTargets.getStatus()).isEqualTo(200);
334     }
335
336     @Test
337     void updatePolicyPropertiesFailure() {
338         List<PropertyDataDefinition> properties = getPropertiesList();
339         ResponseFormat notFoundResponse = new ResponseFormat(HttpStatus.NOT_FOUND_404.getStatusCode());
340         when(businessLogic.updatePolicyProperties(eq(ComponentTypeEnum.SERVICE), eq(SERVICE_ID), eq(POLICY_ID),
341             any(PropertyDataDefinition[].class), eq(USER_ID), eq(true)))
342             .thenThrow(new ByResponseFormatComponentException(notFoundResponse));
343         Response policyProps = buildUpdatePropertiesRequest(ComponentTypeEnum.SERVICE_PARAM_NAME, properties).invoke();
344         assertEquals(HttpStatus.NOT_FOUND_404.getStatusCode(), policyProps.getStatus());
345     }
346
347     @Test
348     void testDeclarePropertyToPolicySuccess() {
349         Service service = new Service();
350         service.setUniqueId(SERVICE_ID);
351         service.addProperty(new PropertyDataDefinitionBuilder().setUniqueId(PROP_1).build());
352
353         PolicyDefinition policyDefinition = new PolicyDefinition();
354         policyDefinition.setUniqueId(UniqueIdBuilder.buildPolicyUniqueId(SERVICE_ID, PROP_1));
355
356         setMocksForPropertyDeclaration(policyDefinition);
357
358         when(componentsUtils.getResponseFormat(eq(ActionStatus.OK)))
359             .thenReturn(new ResponseFormat(HttpStatus.OK_200.getStatusCode()));
360
361         Response declareResponse = buildDeclarePropertiesRequest(PROP_1).invoke();
362         assertEquals(HttpStatus.OK_200.getStatusCode(), declareResponse.getStatus());
363     }
364
365     @Test
366     void testUndeclarePolicySuccess() {
367         Service service = new Service();
368         service.setUniqueId(SERVICE_ID);
369         PropertyDefinition origProperty = new PropertyDataDefinitionBuilder().setUniqueId(PROP_1).build();
370
371         service.addProperty(origProperty);
372
373         PolicyDefinition policyDefinition = new PolicyDefinition();
374         policyDefinition.setUniqueId(UniqueIdBuilder.buildPolicyUniqueId(SERVICE_ID, PROP_1));
375         service.addPolicy(policyDefinition);
376
377         addGetPolicyValueToProperty(origProperty, policyDefinition);
378
379         when(businessLogic
380             .deletePolicy(eq(ComponentTypeEnum.SERVICE), eq(SERVICE_ID), eq(policyDefinition.getUniqueId()),
381                 eq(USER_ID), eq(true))).thenReturn(policyDefinition);
382
383         Response deleteResponse = buildDeletePolicyRequest(policyDefinition).invoke();
384         assertEquals(HttpStatus.OK_200.getStatusCode(), deleteResponse.getStatus());
385     }
386
387     private void addGetPolicyValueToProperty(PropertyDefinition propertyDefinition,
388                                              PolicyDefinition policyDefinition) {
389         JSONObject jobject = new JSONObject();
390         String origValue = Objects.isNull(propertyDefinition.getValue()) ? propertyDefinition.getDefaultValue()
391             : propertyDefinition.getValue();
392         jobject.put(GET_POLICY, null);
393         propertyDefinition.setValue(jobject.toJSONString());
394
395         GetPolicyValueDataDefinition getPolicyValueDataDefinition = new GetPolicyValueDataDefinition();
396         getPolicyValueDataDefinition.setPolicyId(policyDefinition.getUniqueId());
397         getPolicyValueDataDefinition.setPropertyName(propertyDefinition.getName());
398
399         getPolicyValueDataDefinition.setOrigPropertyValue(origValue);
400
401         propertyDefinition.setGetPolicyValues(Collections.singletonList(getPolicyValueDataDefinition));
402     }
403
404     private void setMocksForPropertyDeclaration(PolicyDefinition policyDefinition) {
405         when(request.getSession()).thenReturn(session);
406         when(session.getServletContext()).thenReturn(context);
407         when(context.getAttribute(eq(Constants.WEB_APPLICATION_CONTEXT_WRAPPER_ATTR))).thenReturn(contextWrapper);
408         when(contextWrapper.getWebAppContext(any())).thenReturn(applicationContext);
409         when(applicationContext.getBean(eq(PolicyBusinessLogic.class))).thenReturn(businessLogic);
410         when(businessLogic.declareProperties(eq(USER_ID), eq(SERVICE_ID), any(), any())).thenReturn(
411             Either.left(Collections.singletonList(policyDefinition)));
412         when(componentsUtils
413             .convertJsonToObjectUsingObjectMapper(any(), any(), eq(ComponentInstInputsMap.class), eq(
414                 AuditingActionEnum.CREATE_RESOURCE), eq(ComponentTypeEnum.SERVICE)))
415             .thenReturn(Either.left(getDeclarationBodyForProperty(PROP_1)));
416     }
417
418     private List<PropertyDataDefinition> getPropertiesList() {
419         PropertyDefinition prop1 = new PropertyDataDefinitionBuilder()
420             .setUniqueId("prop1")
421             .build();
422
423         PropertyDefinition prop2 = new PropertyDataDefinitionBuilder()
424             .setUniqueId("prop2")
425             .build();
426         return Arrays.asList(prop1, prop2);
427     }
428
429     private List<PolicyTargetDTO> getTargetDTOList() {
430         PolicyTargetDTO target1 = new PolicyTargetDTO();
431         target1.setUniqueIds(Collections.singletonList("uniqueId"));
432         target1.setType("GROUPS");
433
434         PolicyTargetDTO target2 = new PolicyTargetDTO();
435         target2.setUniqueIds(Collections.singletonList("uniqueId"));
436         target2.setType("component_Instances");
437
438         return Arrays.asList(target1, target2);
439     }
440
441     private Invocation.Builder buildGetPropertiesRequest(String componentType) {
442         return target(PROPS_URL)
443             .resolveTemplate("componentType", componentType)
444             .resolveTemplate("serviceId", SERVICE_ID)
445             .resolveTemplate("policyId", POLICY_ID)
446             .request(MediaType.APPLICATION_JSON)
447             .header(Constants.USER_ID_HEADER, USER_ID);
448
449     }
450
451     private Invocation buildUpdatePropertiesRequest(String componentType, List<PropertyDataDefinition> properties) {
452         return target(PROPS_URL)
453             .resolveTemplate("componentType", componentType)
454             .resolveTemplate("serviceId", SERVICE_ID)
455             .resolveTemplate("policyId", POLICY_ID)
456             .request(MediaType.APPLICATION_JSON)
457             .header(Constants.USER_ID_HEADER, USER_ID)
458             .buildPut(Entity.entity(properties, MediaType.APPLICATION_JSON));
459     }
460
461     private Invocation buildUpdateTargetsRequest(String componentType, List<PolicyTargetDTO> targets) {
462         return target(UPDATE_TARGETS_URL)
463             .resolveTemplate("componentType", componentType)
464             .resolveTemplate("componentId", COMPONENT_ID)
465             .resolveTemplate("policyId", POLICY_ID)
466             .request(MediaType.APPLICATION_JSON)
467             .header(Constants.USER_ID_HEADER, USER_ID)
468             .buildPost(Entity.entity(targets, MediaType.APPLICATION_JSON));
469     }
470
471     private Invocation.Builder buildGetPropertiesRequest() {
472         return target(PROPS_URL)
473             .resolveTemplate("componentType", "services")
474             .resolveTemplate("serviceId", SERVICE_ID)
475             .resolveTemplate("policyId", POLICY_ID)
476             .request(MediaType.APPLICATION_JSON)
477             .header(Constants.USER_ID_HEADER, USER_ID);
478     }
479
480     private Invocation buildDeletePolicyRequest(PolicyDefinition policyDefinition) {
481         return target(DELETE_URL)
482             .resolveTemplate("containerComponentType", "services")
483             .resolveTemplate("componentId", SERVICE_ID)
484             .resolveTemplate("policyId", policyDefinition.getUniqueId())
485             .request(MediaType.APPLICATION_JSON)
486             .header(Constants.USER_ID_HEADER, USER_ID)
487             .buildDelete();
488     }
489
490     private Invocation buildDeclarePropertiesRequest(String propertyId) {
491         return target(DECLARE_URL)
492             .resolveTemplate("componentType", "services")
493             .resolveTemplate("serviceId", SERVICE_ID)
494             .request(MediaType.APPLICATION_JSON)
495             .header(Constants.USER_ID_HEADER, USER_ID)
496             .buildPost(Entity.entity(getDeclarationBodyForProperty(propertyId), MediaType.APPLICATION_JSON));
497     }
498
499     private ComponentInstInputsMap getDeclarationBodyForProperty(String propertyId) {
500         ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
501         ComponentInstancePropInput propInput = new ComponentInstancePropInput();
502         PropertyDefinition propertyDefinition = new PropertyDefinition();
503
504         propertyDefinition.setType("string");
505         propertyDefinition.setUniqueId(SERVICE_ID + "." + propertyId);
506         propInput.setInput(propertyDefinition);
507         propInput.setPropertiesName(propertyId);
508
509         componentInstInputsMap.setComponentInstancePropertiesToPolicies(new HashMap<>());
510         componentInstInputsMap.getComponentInstancePropertiesToPolicies()
511             .put("componentInstancePropertiesToPolicies", Collections.singletonList(propInput));
512
513         return componentInstInputsMap;
514     }
515
516     @Override
517     protected ResourceConfig configure() {
518         forceSet(TestProperties.CONTAINER_PORT, "0");
519         return super.configure()
520             .register(new PolicyServlet(null, componentsUtils,
521                 servletUtils, null, businessLogic));
522     }
523
524     private static void createMocks() {
525         propertyDeclarationOrchestrator = Mockito.mock(PropertyDeclarationOrchestrator.class);
526         toscaOperationFacade = Mockito.mock(ToscaOperationFacade.class);
527         businessLogic = Mockito.mock(PolicyBusinessLogic.class);
528         businessLogic.setPropertyDeclarationOrchestrator(propertyDeclarationOrchestrator);
529         businessLogic.setToscaOperationFacade(toscaOperationFacade);
530         BaseBusinessLogic bbl = new BaseBusinessLogicTest(Mockito.mock(IElementOperation.class),
531             Mockito.mock(IGroupOperation.class),
532             Mockito.mock(IGroupInstanceOperation.class), Mockito.mock(IGroupTypeOperation.class),
533             Mockito.mock(InterfaceOperation.class), Mockito.mock(InterfaceLifecycleOperation.class), Mockito.mock(
534             ArtifactsOperations.class));
535         PolicyServletTest.baseBusinessLogic = Mockito.spy(bbl);
536         PolicyServletTest.baseBusinessLogic.setToscaOperationFacade(toscaOperationFacade);
537
538         componentsUtils = Mockito.mock(ComponentsUtils.class);
539         servletUtils = Mockito.mock(ServletUtils.class);
540         responseFormat = Mockito.mock(ResponseFormat.class);
541         roleAuthorizationHandler = Mockito.mock(RoleAuthorizationHandler.class);
542     }
543
544     private static class BaseBusinessLogicTest extends BaseBusinessLogic {
545
546         BaseBusinessLogicTest(IElementOperation elementDao,
547                               IGroupOperation groupOperation,
548                               IGroupInstanceOperation groupInstanceOperation,
549                               IGroupTypeOperation groupTypeOperation,
550                               InterfaceOperation interfaceOperation,
551                               InterfaceLifecycleOperation interfaceLifecycleTypeOperation,
552                               ArtifactsOperations artifactToscaOperation) {
553             super(elementDao, groupOperation, groupInstanceOperation, groupTypeOperation, interfaceOperation,
554                 interfaceLifecycleTypeOperation, artifactToscaOperation);
555         }
556     }
557
558 }