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