Can't create VF via a newly created VSP
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / impl / PolicyBusinessLogicTest.java
1 /*
2  * Copyright © 2016-2019 European Support Limited
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
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
17 package org.openecomp.sdc.be.components.impl;
18
19
20 import fj.data.Either;
21 import java.util.ArrayList;
22 import java.util.Optional;
23 import org.apache.commons.collections.CollectionUtils;
24 import org.junit.Before;
25 import org.junit.BeforeClass;
26 import org.junit.Test;
27 import org.junit.runner.RunWith;
28 import org.mockito.InjectMocks;
29 import org.mockito.Mock;
30 import org.mockito.Mockito;
31 import org.mockito.junit.MockitoJUnitRunner;
32 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
33 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
34 import org.openecomp.sdc.be.components.property.PropertyDeclarationOrchestrator;
35 import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
36 import org.openecomp.sdc.be.components.utils.GroupDefinitionBuilder;
37 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
38 import org.openecomp.sdc.be.components.validation.UserValidations;
39 import org.openecomp.sdc.be.config.ConfigurationManager;
40 import org.openecomp.sdc.be.dao.api.ActionStatus;
41 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
42 import org.openecomp.sdc.be.dao.jsongraph.JanusGraphDao;
43 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
44 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
45 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
46 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
47 import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
48 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
49 import org.openecomp.sdc.be.impl.ComponentsUtils;
50 import org.openecomp.sdc.be.model.Component;
51 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
52 import org.openecomp.sdc.be.model.ComponentInstance;
53 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
54 import org.openecomp.sdc.be.model.ComponentParametersView;
55 import org.openecomp.sdc.be.model.GroupDefinition;
56 import org.openecomp.sdc.be.model.LifecycleStateEnum;
57 import org.openecomp.sdc.be.model.PolicyDefinition;
58 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
59 import org.openecomp.sdc.be.model.PropertyDefinition;
60 import org.openecomp.sdc.be.model.Resource;
61 import org.openecomp.sdc.be.model.User;
62 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
63 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
64 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
65 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
66 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
67 import org.openecomp.sdc.be.model.operations.impl.PolicyTypeOperation;
68 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
69 import org.openecomp.sdc.common.impl.ExternalConfiguration;
70 import org.openecomp.sdc.common.impl.FSConfigurationSource;
71 import org.openecomp.sdc.exception.ResponseFormat;
72
73 import java.util.Arrays;
74 import java.util.Collections;
75 import java.util.HashMap;
76 import java.util.List;
77 import java.util.Map;
78
79 import static org.assertj.core.api.Java6Assertions.assertThat;
80 import static org.junit.Assert.assertEquals;
81 import static org.junit.Assert.assertFalse;
82 import static org.junit.Assert.assertNotNull;
83 import static org.junit.Assert.assertTrue;
84 import static org.junit.Assert.fail;
85 import static org.mockito.ArgumentMatchers.any;
86 import static org.mockito.ArgumentMatchers.anyBoolean;
87 import static org.mockito.ArgumentMatchers.anyMap;
88 import static org.mockito.ArgumentMatchers.anyString;
89 import static org.mockito.ArgumentMatchers.eq;
90 import static org.mockito.Mockito.when;
91
92 @RunWith(MockitoJUnitRunner.class)
93 public class PolicyBusinessLogicTest {
94
95     @InjectMocks
96     private PolicyBusinessLogic businessLogic;
97     @Mock
98     private ComponentsUtils componentsUtils;
99     @Mock
100     private ToscaOperationFacade toscaOperationFacade;
101     @Mock
102     private IGraphLockOperation graphLockOperation;
103     @Mock
104     private PolicyTypeOperation policyTypeOperation;
105     @Mock
106     private UserValidations userValidations;
107     @Mock
108     private JanusGraphDao janusGraphDao;
109     @Mock
110     private ApplicationDataTypeCache dataTypeCache;
111     @Mock
112     private PropertyOperation propertyOperation;
113     @Mock
114     PropertyDeclarationOrchestrator propertyDeclarationOrchestrator;
115
116     private final static String COMPONENT_ID = "componentId";
117     private final static String NON_EXIST_COMPONENT_ID = "nonExistComponentId";
118     private final static String COMPONENT_NAME = "componentName";
119     private final static String POLICY_TYPE_NAME = "policyTypeName";
120     private final static String POLICY_ID = "policyId";
121     private final static String INVALID_POLICY_ID = "invalidPolicyId";
122     private final static String POLICY_NAME = "policyName";
123     private final static String OTHER_POLICY_NAME = "otherPolicyName";
124     private final static String USER_ID = "jh0003";
125     private final static String UNIQUE_ID_EXISTS = "uniqueIdExists";
126     private final static String UNIQUE_ID_DOESNT_EXISTS = "uniqueIdDoesntExists";
127     private final static String CREATE_POLICY = "create Policy";
128     private final static String PROPERTY_NAME = "propDefinition";
129     private final static User user = buildUser();
130     private final static PolicyDefinition policy = buildPolicy(POLICY_NAME);
131     private final static PolicyDefinition otherPolicy = buildPolicy(OTHER_POLICY_NAME);
132     private final static Resource resource = buildResource();
133     private final static PolicyTypeDefinition policyType = buildPolicyType();
134
135     private static Either<Component, StorageOperationStatus> componentSuccessEither;
136     private static Either<PolicyTypeDefinition, StorageOperationStatus> getPolicyTypeSuccessEither;
137     private static Either<PolicyDefinition, StorageOperationStatus> policySuccessEither;
138     private static ResponseFormat notFoundResponse;
139     private static ResponseFormat invalidContentResponse;
140     private static ResponseFormat nameExistsResponse;
141
142     @BeforeClass
143     public static void setup() {
144         String appConfigDir = "src/test/resources/config/catalog-be";
145         new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir));
146         createResponses();
147         new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir));
148     }
149
150     @Before
151     public void initBl() {
152         businessLogic.setComponentsUtils(componentsUtils);
153         businessLogic.setToscaOperationFacade(toscaOperationFacade);
154         businessLogic.setJanusGraphDao(janusGraphDao);
155         businessLogic.setUserValidations(userValidations);
156         businessLogic.setGraphLockOperation(graphLockOperation);
157         businessLogic.setPolicyTypeOperation(policyTypeOperation);
158         businessLogic.setDataTypeCache(dataTypeCache);
159         businessLogic.setPropertyOperation(propertyOperation);
160         businessLogic.setPropertyDeclarationOrchestrator(propertyDeclarationOrchestrator);
161     }
162
163
164     private static void createResponses() {
165         componentSuccessEither = Either.left(resource);
166         getPolicyTypeSuccessEither = Either.left(policyType);
167         policySuccessEither = Either.left(policy);
168         notFoundResponse = new ResponseFormat();
169         notFoundResponse.setStatus(404);
170         invalidContentResponse = new ResponseFormat();
171         invalidContentResponse.setStatus(400);
172         nameExistsResponse = new ResponseFormat();
173         nameExistsResponse.setStatus(409);
174     }
175
176     @Test
177     public void createPolicySuccessTest(){
178         stubValidateAndLockSuccess(CREATE_POLICY);
179         when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME))).thenReturn(getPolicyTypeSuccessEither);
180         when(toscaOperationFacade.associatePolicyToComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), eq(0))).thenReturn(policySuccessEither);
181         stubUnlockAndCommit();
182         PolicyDefinition response = businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
183         assertTrue(!response.isEmpty());
184     }
185
186     @Test
187     public void createPolicyFromCsarDefinitionTest() {
188         String prop1 = "Prop_1";
189         String prop2 = "Prop_2";
190         Map<String, PolicyDefinition> policies = new HashMap<>();
191         PolicyDefinition policy = buildPolicy(POLICY_NAME);
192         Map<PolicyTargetType, List<String>> targets = getTargets();
193         PropertyDataDefinition[] properties = getProperties(prop1, prop2);
194         policy.setTargets(targets);
195         policy.setProperties(Arrays.asList(properties));
196         policies.put(POLICY_NAME, policy);
197
198         List<ComponentInstance> instanceList = new ArrayList<>();
199         ComponentInstance componentInstance = new ComponentInstance();
200         componentInstance.setUniqueId(UNIQUE_ID_EXISTS);
201         componentInstance.setName(UNIQUE_ID_EXISTS);
202         instanceList.add(componentInstance);
203
204         GroupDefinition groupDefinition = new GroupDefinition();
205         groupDefinition.setUniqueId(UNIQUE_ID_EXISTS);
206         groupDefinition.setName(UNIQUE_ID_EXISTS);
207
208         Resource newResource = buildResource();
209         newResource.setPolicies(policies);
210         newResource.setComponentInstances(instanceList);
211         newResource.addGroups(Collections.singletonList(groupDefinition));
212
213         when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME))).thenReturn(getPolicyTypeSuccessEither);
214         when(toscaOperationFacade.associatePolicyToComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), eq(0))).thenReturn(Either.left(policy));
215         when(toscaOperationFacade.getToscaFullElement(eq(COMPONENT_ID))).thenReturn(Either.left(newResource));
216         when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), any(PromoteVersionEnum.class))).thenReturn(Either.left(policy));
217         when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>()));
218         when(propertyOperation.validateAndUpdatePropertyValue(eq(null), eq(prop1), anyBoolean(), eq(null), anyMap())).thenReturn(Either.left(prop1));
219         when(propertyOperation.validateAndUpdatePropertyValue(eq(null), eq(prop2), anyBoolean(), eq(null), anyMap())).thenReturn(Either.left(prop2));
220
221         Map<String, PolicyDefinition> createdPolicy = businessLogic.createPoliciesFromParsedCsar(newResource, policies);
222
223         assertFalse(createdPolicy.isEmpty());
224         PolicyDefinition newPolicy = createdPolicy.get(POLICY_NAME);
225         assertNotNull(newPolicy);
226         assertNotNull(newPolicy.getTargets());
227         assertNotNull(newPolicy.getProperties());
228         assertEquals(2, newPolicy.getProperties().size());
229         assertEquals(2, newPolicy.getTargets().size());
230     }
231     
232     @Test
233     public void createPolicyUserFailureTest(){
234         ByActionStatusComponentException userNotFoundException = new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND);
235         when(userValidations.validateUserExists(eq(USER_ID))).thenThrow(userNotFoundException);
236         stubRollback();
237         try{
238             businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
239         } catch(ByActionStatusComponentException e){
240             assertEquals(e.getActionStatus(), userNotFoundException.getActionStatus());
241         }
242     }
243
244     private void assertNotFound(Either<PolicyDefinition, ResponseFormat> response) {
245         assertTrue(response.isRight() && response.right().value().getStatus().equals(404));
246     }
247
248     @Test(expected = ComponentException.class)
249     public void createPolicyComponentFailureTest(){
250         when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
251         Either<Component, StorageOperationStatus> componentNotFoundResponse = Either.right(StorageOperationStatus.NOT_FOUND);
252         when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND), eq(ComponentTypeEnum.RESOURCE))).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
253         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(componentNotFoundResponse);
254         businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
255     }
256     
257     @Test(expected = ComponentException.class)
258     public void createPolicyPolicyTypeFailureTest(){
259         stubValidateAndLockSuccess(CREATE_POLICY);
260         Either<PolicyTypeDefinition, StorageOperationStatus> getPolicyTypeFailed = Either.right(StorageOperationStatus.NOT_FOUND);
261         when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME))).thenReturn(getPolicyTypeFailed);
262         when(componentsUtils.convertFromStorageResponse(eq(getPolicyTypeFailed.right().value()))).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
263         stubUnlockAndRollback();
264         businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
265     }
266     
267     @Test(expected = ComponentException.class)
268     public void createPolicyComponentTypeFailureTest(){
269         stubValidateAndLockSuccess(CREATE_POLICY);
270         when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME))).thenReturn(getPolicyTypeSuccessEither);
271         Either<PolicyDefinition, StorageOperationStatus> addPolicyRes = Either.right(StorageOperationStatus.BAD_REQUEST);
272         when(toscaOperationFacade.associatePolicyToComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), eq(0))).thenReturn(addPolicyRes);
273         when(componentsUtils.convertFromStorageResponse(eq(addPolicyRes.right().value()))).thenReturn(ActionStatus.INVALID_CONTENT);
274
275         stubUnlockAndRollback();
276         businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
277     }
278
279     @Test
280     public void updatePolicySuccessTest(){
281         stubValidateAndLockSuccess(CREATE_POLICY);
282         when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), any(PromoteVersionEnum.class))).thenReturn(policySuccessEither);
283         stubUnlockAndCommit();
284         PolicyDefinition  response = businessLogic.updatePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, otherPolicy, USER_ID, true);
285         assertTrue(!response.isEmpty());
286     }
287     
288     @Test(expected = ComponentException.class)
289     public void updatePolicyNameFailureTest(){
290         stubValidateAndLockSuccess(CREATE_POLICY);
291         stubUnlockAndRollback();
292         businessLogic.updatePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, policy, USER_ID, true);
293     }
294     
295     @Test
296     public void getPolicySuccessTest(){
297         stubValidationSuccess(CREATE_POLICY);
298         stubCommit();
299         PolicyDefinition response = businessLogic.getPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, USER_ID);
300         assertTrue(!response.isEmpty());
301     }
302     
303     @Test(expected = ComponentException.class)
304     public void getPolicyFailureTest(){
305         stubValidationSuccess(CREATE_POLICY);
306         stubRollback();
307         businessLogic.getPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, INVALID_POLICY_ID, USER_ID);
308     }
309     
310     @Test
311     public void deletePolicySuccessTest(){
312         stubValidateAndLockSuccess(CREATE_POLICY);
313         stubCommit();
314         when(toscaOperationFacade.removePolicyFromComponent(eq(COMPONENT_ID),eq(POLICY_ID))).thenReturn(StorageOperationStatus.OK);
315         PolicyDefinition  response = businessLogic.deletePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, USER_ID, true);
316         assertTrue(!response.isEmpty());
317     }
318     
319     @Test(expected = ComponentException.class)
320     public void deletePolicyFailureTest(){
321         stubValidateAndLockSuccess(CREATE_POLICY);
322         stubCommit();
323         businessLogic.deletePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, INVALID_POLICY_ID, USER_ID, true);
324     }
325
326
327     @Test
328     public void updatePolicyPropertiesSuccessTest(){
329         stubValidateAndLockSuccess(CREATE_POLICY);
330         when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>()));
331         String prop1 = "Name";
332         String prop2 = "Type";
333         when(propertyOperation.validateAndUpdatePropertyValue(eq(null), eq(prop1), anyBoolean(), eq(null), anyMap())).thenReturn(Either.left(prop1));
334         when(propertyOperation.validateAndUpdatePropertyValue(eq(null), eq(prop2), anyBoolean(), eq(null), anyMap())).thenReturn(Either.left(prop2));
335         when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), any(PromoteVersionEnum.class))).thenReturn(policySuccessEither);
336         stubUnlockAndCommit();
337         PropertyDataDefinition[] properties = getProperties(prop1, prop2);
338         policy.setProperties(Arrays.asList(properties));
339         List<PropertyDataDefinition>  response = businessLogic.updatePolicyProperties(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, properties , USER_ID, true);
340         List<PropertyDataDefinition> updatedProperties = response;
341         assertThat(updatedProperties.size()).isEqualTo(2);
342     }
343
344     @Test
345     public void updatePolicyTargetsSuccessTest(){
346         stubValidateAndLockSuccess(CREATE_POLICY);
347         stubGetToscaFullElementSuccess();
348         stubUpdatePolicyOfComponentSuccess();
349         stubGetToscaElementSuccess();
350         PolicyDefinition policyResult = businessLogic.updatePolicyTargets(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, getTargets(), USER_ID);
351         Map<PolicyTargetType, List<String>> targets = getTargets();
352         assertThat(policyResult.getTargets().values()).usingFieldByFieldElementComparator().containsExactlyInAnyOrder(targets.get(PolicyTargetType.GROUPS), targets.get(PolicyTargetType.COMPONENT_INSTANCES));
353
354     }
355
356     @Test(expected = ComponentException.class)
357     public void updatePolicyTargetsTargetIDFailureTest(){
358         stubValidateAndLockSuccess(CREATE_POLICY);
359         stubGetToscaFullElementSuccess();
360         stubGetToscaElementSuccess();
361         stubUpdatePolicyOfComponentSuccess();
362         stubRollback();
363         businessLogic.updatePolicyTargets(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, getTargetListFakeId(), USER_ID);
364
365     }
366
367
368     @Test(expected = ComponentException.class)
369     public void updatePolicyTargetsTypeFailureTest(){
370         stubValidateAndLockSuccess(CREATE_POLICY);
371         stubGetToscaFullElementSuccess();
372         stubGetToscaElementSuccess();
373         stubUpdatePolicyOfComponentSuccess();
374         stubRollback();
375         businessLogic.updatePolicyTargets(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, getTargetListFakeType(), USER_ID);
376
377     }
378
379     private void stubUpdatePolicyOfComponentSuccess() {
380         when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), eq(policy), any(PromoteVersionEnum.class))).thenReturn(policySuccessEither);
381     }
382
383
384     @Test(expected = ComponentException.class)
385     public void updatePolicyPropertiesFailureTest(){
386         stubValidateAndLockSuccess(CREATE_POLICY);
387         stubUnlockAndRollback();
388         policy.setProperties(null);
389         businessLogic.updatePolicyProperties(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, getProperties("Name", "Type") , USER_ID, true);
390     }
391
392     @Test
393     public void testDeclarePropertiesAsPoliciesSuccess() {
394         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(resource));
395         when(graphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
396         when(graphLockOperation.unlockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
397
398         when(propertyDeclarationOrchestrator.declarePropertiesToPolicies(any(), any())).thenReturn(Either.left(getDeclaredPolicies()));
399
400         Either<List<PolicyDefinition>, ResponseFormat> declaredPoliciesEither = businessLogic
401                                                                                           .declareProperties(USER_ID,
402                                                                                                   resource.getUniqueId(),
403                                                                                                   ComponentTypeEnum.RESOURCE,
404                                                                                                   getInputForPropertyToPolicyDeclaration());
405
406         assertTrue(declaredPoliciesEither.isLeft());
407
408         List<PolicyDefinition> declaredPolicies = declaredPoliciesEither.left().value();
409         assertTrue(CollectionUtils.isNotEmpty(declaredPolicies));
410         assertEquals(1, declaredPolicies.size());
411     }
412
413     @Test
414     public void testDeclarePropertiesAsPoliciesFailure() {
415         when(toscaOperationFacade.getToscaElement(eq(NON_EXIST_COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
416         when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND), eq(ComponentTypeEnum.RESOURCE))).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
417         try {
418             businessLogic
419                     .declareProperties(USER_ID,
420                             NON_EXIST_COMPONENT_ID,
421                             ComponentTypeEnum.RESOURCE,
422                             getInputForPropertyToPolicyDeclaration());
423         } catch (ComponentException e) {
424             assertEquals(ActionStatus.RESOURCE_NOT_FOUND, e.getActionStatus());
425             return;
426         }
427         fail();
428     }
429
430     private ComponentInstInputsMap getInputForPropertyToPolicyDeclaration() {
431         PropertyDefinition propertyDefinition = getPropertyDefinitionForDeclaration();
432
433         ComponentInstancePropInput componentInstancePropInput = new ComponentInstancePropInput();
434         componentInstancePropInput.setInput(propertyDefinition);
435         componentInstancePropInput.setPropertiesName(PROPERTY_NAME);
436
437         Map<String, List<ComponentInstancePropInput>> componentPropertiesToPolicies = new HashMap<>();
438         componentPropertiesToPolicies.put(resource.getUniqueId(), Collections.singletonList(componentInstancePropInput));
439
440         ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
441         componentInstInputsMap.setComponentInstancePropertiesToPolicies(componentPropertiesToPolicies);
442         return componentInstInputsMap;
443     }
444
445     private List<PolicyDefinition> getDeclaredPolicies() {
446         return Collections.singletonList(new PolicyDefinition(getPropertyDefinitionForDeclaration()));
447     }
448
449     private PropertyDefinition getPropertyDefinitionForDeclaration() {
450         PropertyDefinition propertyDefinition = new PropertyDefinition();
451         propertyDefinition.setUniqueId(PROPERTY_NAME);
452         propertyDefinition.setName(PROPERTY_NAME);
453         return propertyDefinition;
454     }
455
456     private PropertyDataDefinition[] getProperties(String prop1, String prop2) {
457         PropertyDataDefinition property1 = new PropertyDataDefinition();
458         property1.setName(prop1);
459         property1.setValue(prop1);
460         PropertyDataDefinition property2 = new PropertyDataDefinition();
461         property2.setName(prop2);
462         property2.setValue(prop2);
463         return new PropertyDataDefinition[]{property1, property2};
464     }
465     
466     
467     private void stubUnlockAndRollback() {
468         when(graphLockOperation.unlockComponent(eq(COMPONENT_ID), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
469         stubRollback();
470     }
471
472     private void stubCommit() {
473         when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
474     }
475
476     private void stubRollback() {
477         when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
478     }
479
480     private void stubUnlockAndCommit() {
481         when(graphLockOperation.unlockComponent(eq(COMPONENT_ID), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
482         stubCommit();
483     }
484     
485     private void stubValidateAndLockSuccess(String methodName) {
486         stubValidationSuccess(methodName);
487         when(graphLockOperation.lockComponent(eq(COMPONENT_ID), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
488    }
489
490     private void stubValidationSuccess(String methodName) {
491         when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
492         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(componentSuccessEither);
493     }
494
495     private void stubGetToscaFullElementSuccess() {
496         when(toscaOperationFacade.getToscaFullElement(eq(COMPONENT_ID))).thenReturn(buildElementEither());
497     }
498
499     private void stubGetToscaElementSuccess() {
500         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID))).thenReturn(componentSuccessEither);
501     }
502
503     private Either<Component, StorageOperationStatus> buildElementEither() {
504         ResourceBuilder builder = new ResourceBuilder();
505         GroupDefinition groupDefinition = GroupDefinitionBuilder.create().setUniqueId(UNIQUE_ID_EXISTS).build();
506         ComponentInstanceBuilder componentInstanceBuilder = new ComponentInstanceBuilder();
507         ComponentInstance componentInstance = componentInstanceBuilder.setUniqueId(UNIQUE_ID_EXISTS).build();
508         return Either.left(builder.addGroup(groupDefinition).addComponentInstance(componentInstance).build());
509     }
510
511     private Map<PolicyTargetType, List<String>> getTargets() {
512         Map<PolicyTargetType, List<String>> targets = new HashMap<>();
513         targets.put(PolicyTargetType.COMPONENT_INSTANCES, Collections.singletonList(UNIQUE_ID_EXISTS));
514         targets.put(PolicyTargetType.GROUPS, Collections.singletonList(UNIQUE_ID_EXISTS));
515         return targets;
516     }
517
518
519
520     private static PolicyTypeDefinition buildPolicyType() {
521         PolicyTypeDefinition policyType = new PolicyTypeDefinition();
522         policyType.setType(POLICY_TYPE_NAME);
523         return policyType;
524     }
525
526     private static PolicyDefinition buildPolicy(String policyName) {
527         PolicyDefinition policy = new PolicyDefinition();
528         policy.setUniqueId(POLICY_ID);
529         policy.setPolicyTypeName(POLICY_TYPE_NAME);
530         policy.setComponentName(COMPONENT_NAME);
531         policy.setName(policyName);
532         return policy;
533     }
534
535     private static Resource buildResource() {
536         Resource resource = new Resource();
537         resource.setUniqueId(COMPONENT_ID);
538         resource.setName(COMPONENT_NAME);
539         resource.setCreatorUserId(USER_ID);
540         resource.setLastUpdaterUserId(USER_ID);
541         resource.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
542         resource.setIsDeleted(false);
543         resource.setResourceType(ResourceTypeEnum.VF);
544         resource.setToscaType(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
545         resource.setComponentType(ComponentTypeEnum.RESOURCE);
546         Map<String, PolicyDefinition> policies = new HashMap<>();
547         policies.put(POLICY_ID, policy);
548         resource.setPolicies(policies);
549         return resource;
550     }
551
552     private static User buildUser() {
553         User user = new User();
554         user.setUserId(USER_ID);
555         return user;
556     }
557
558     private Map<PolicyTargetType, List<String>> getTargetListFakeType() {
559         Map<PolicyTargetType, List<String>> targets = new HashMap<>();
560         targets.put(PolicyTargetType.TYPE_DOES_NOT_EXIST, Collections.singletonList(UNIQUE_ID_EXISTS));
561         return targets;
562     }
563
564     private Map<PolicyTargetType, List<String>> getTargetListFakeId() {
565         Map<PolicyTargetType, List<String>> targets = new HashMap<>();
566         targets.put(PolicyTargetType.COMPONENT_INSTANCES, Collections.singletonList(UNIQUE_ID_DOESNT_EXISTS));
567         targets.put(PolicyTargetType.GROUPS, Collections.singletonList(UNIQUE_ID_DOESNT_EXISTS));
568         return targets;
569     }
570
571 }