Policies import when import 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_EXSISTS = "uniqueIdExists";
126     private final static String UNIQUE_ID_DOESNT_EXSISTS = "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_EXSISTS);
201         componentInstance.setName(UNIQUE_ID_EXSISTS);
202         instanceList.add(componentInstance);
203
204         Resource newResource = buildResource();
205         newResource.setPolicies(policies);
206         newResource.setComponentInstances(instanceList);
207         
208         when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME))).thenReturn(getPolicyTypeSuccessEither);
209         when(toscaOperationFacade.associatePolicyToComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), eq(0))).thenReturn(Either.left(policy));
210         when(toscaOperationFacade.getToscaFullElement(eq(COMPONENT_ID))).thenReturn(Either.left(newResource));
211         when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), any(PromoteVersionEnum.class))).thenReturn(Either.left(policy));
212         when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>()));
213         when(propertyOperation.validateAndUpdatePropertyValue(eq(null), eq(prop1), anyBoolean(), eq(null), anyMap())).thenReturn(Either.left(prop1));
214         when(propertyOperation.validateAndUpdatePropertyValue(eq(null), eq(prop2), anyBoolean(), eq(null), anyMap())).thenReturn(Either.left(prop2));
215
216         Map<String, PolicyDefinition> createdPolicy = businessLogic.createPoliciesFromParsedCsar(newResource, policies);
217
218         assertFalse(createdPolicy.isEmpty());
219         PolicyDefinition newPolicy = createdPolicy.get(POLICY_NAME);
220         assertNotNull(newPolicy);
221         assertNotNull(newPolicy.getTargets());
222         assertNotNull(newPolicy.getProperties());
223         assertEquals(2, newPolicy.getProperties().size());
224         assertEquals(1, newPolicy.getTargets().size());
225     }
226     
227     @Test
228     public void createPolicyUserFailureTest(){
229         ByActionStatusComponentException userNotFoundException = new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND);
230         when(userValidations.validateUserExists(eq(USER_ID))).thenThrow(userNotFoundException);
231         stubRollback();
232         try{
233             businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
234         } catch(ByActionStatusComponentException e){
235             assertEquals(e.getActionStatus(), userNotFoundException.getActionStatus());
236         }
237     }
238
239     private void assertNotFound(Either<PolicyDefinition, ResponseFormat> response) {
240         assertTrue(response.isRight() && response.right().value().getStatus().equals(404));
241     }
242
243     @Test(expected = ComponentException.class)
244     public void createPolicyComponentFailureTest(){
245         when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
246         Either<Component, StorageOperationStatus> componentNotFoundResponse = Either.right(StorageOperationStatus.NOT_FOUND);
247         when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND), eq(ComponentTypeEnum.RESOURCE))).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
248         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(componentNotFoundResponse);
249         businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
250     }
251     
252     @Test(expected = ComponentException.class)
253     public void createPolicyPolicyTypeFailureTest(){
254         stubValidateAndLockSuccess(CREATE_POLICY);
255         Either<PolicyTypeDefinition, StorageOperationStatus> getPolicyTypeFailed = Either.right(StorageOperationStatus.NOT_FOUND);
256         when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME))).thenReturn(getPolicyTypeFailed);
257         when(componentsUtils.convertFromStorageResponse(eq(getPolicyTypeFailed.right().value()))).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
258         stubUnlockAndRollback();
259         businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
260     }
261     
262     @Test(expected = ComponentException.class)
263     public void createPolicyComponentTypeFailureTest(){
264         stubValidateAndLockSuccess(CREATE_POLICY);
265         when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME))).thenReturn(getPolicyTypeSuccessEither);
266         Either<PolicyDefinition, StorageOperationStatus> addPolicyRes = Either.right(StorageOperationStatus.BAD_REQUEST);
267         when(toscaOperationFacade.associatePolicyToComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), eq(0))).thenReturn(addPolicyRes);
268         when(componentsUtils.convertFromStorageResponse(eq(addPolicyRes.right().value()))).thenReturn(ActionStatus.INVALID_CONTENT);
269
270         stubUnlockAndRollback();
271         businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
272     }
273
274     @Test
275     public void updatePolicySuccessTest(){
276         stubValidateAndLockSuccess(CREATE_POLICY);
277         when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), any(PromoteVersionEnum.class))).thenReturn(policySuccessEither);
278         stubUnlockAndCommit();
279         PolicyDefinition  response = businessLogic.updatePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, otherPolicy, USER_ID, true);
280         assertTrue(!response.isEmpty());
281     }
282     
283     @Test(expected = ComponentException.class)
284     public void updatePolicyNameFailureTest(){
285         stubValidateAndLockSuccess(CREATE_POLICY);
286         stubUnlockAndRollback();
287         businessLogic.updatePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, policy, USER_ID, true);
288     }
289     
290     @Test
291     public void getPolicySuccessTest(){
292         stubValidationSuccess(CREATE_POLICY);
293         stubCommit();
294         PolicyDefinition response = businessLogic.getPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, USER_ID);
295         assertTrue(!response.isEmpty());
296     }
297     
298     @Test(expected = ComponentException.class)
299     public void getPolicyFailureTest(){
300         stubValidationSuccess(CREATE_POLICY);
301         stubRollback();
302         businessLogic.getPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, INVALID_POLICY_ID, USER_ID);
303     }
304     
305     @Test
306     public void deletePolicySuccessTest(){
307         stubValidateAndLockSuccess(CREATE_POLICY);
308         stubCommit();
309         when(toscaOperationFacade.removePolicyFromComponent(eq(COMPONENT_ID),eq(POLICY_ID))).thenReturn(StorageOperationStatus.OK);
310         PolicyDefinition  response = businessLogic.deletePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, USER_ID, true);
311         assertTrue(!response.isEmpty());
312     }
313     
314     @Test(expected = ComponentException.class)
315     public void deletePolicyFailureTest(){
316         stubValidateAndLockSuccess(CREATE_POLICY);
317         stubCommit();
318         businessLogic.deletePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, INVALID_POLICY_ID, USER_ID, true);
319     }
320
321
322     @Test
323     public void updatePolicyPropertiesSuccessTest(){
324         stubValidateAndLockSuccess(CREATE_POLICY);
325         when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>()));
326         String prop1 = "Name";
327         String prop2 = "Type";
328         when(propertyOperation.validateAndUpdatePropertyValue(eq(null), eq(prop1), anyBoolean(), eq(null), anyMap())).thenReturn(Either.left(prop1));
329         when(propertyOperation.validateAndUpdatePropertyValue(eq(null), eq(prop2), anyBoolean(), eq(null), anyMap())).thenReturn(Either.left(prop2));
330         when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), any(PromoteVersionEnum.class))).thenReturn(policySuccessEither);
331         stubUnlockAndCommit();
332         PropertyDataDefinition[] properties = getProperties(prop1, prop2);
333         policy.setProperties(Arrays.asList(properties));
334         List<PropertyDataDefinition>  response = businessLogic.updatePolicyProperties(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, properties , USER_ID, true);
335         List<PropertyDataDefinition> updatedProperties = response;
336         assertThat(updatedProperties.size()).isEqualTo(2);
337     }
338
339     @Test
340     public void updatePolicyTargetsSuccessTest(){
341         stubValidateAndLockSuccess(CREATE_POLICY);
342         stubGetToscaFullElementSuccess();
343         stubUpdatePolicyOfComponentSuccess();
344         stubGetToscaElementSuccess();
345         PolicyDefinition policyResult = businessLogic.updatePolicyTargets(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, getTargets(), USER_ID);
346         Map<PolicyTargetType, List<String>> targets = getTargets();
347         assertThat(policyResult.getTargets().values()).usingFieldByFieldElementComparator().containsExactlyInAnyOrder(targets.get(PolicyTargetType.GROUPS), targets.get(PolicyTargetType.COMPONENT_INSTANCES));
348
349     }
350
351     @Test(expected = ComponentException.class)
352     public void updatePolicyTargetsTargetIDFailureTest(){
353         stubValidateAndLockSuccess(CREATE_POLICY);
354         stubGetToscaFullElementSuccess();
355         stubGetToscaElementSuccess();
356         stubUpdatePolicyOfComponentSuccess();
357         stubRollback();
358         businessLogic.updatePolicyTargets(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, getTargetListFakeId(), USER_ID);
359
360     }
361
362
363     @Test(expected = ComponentException.class)
364     public void updatePolicyTargetsTypeFailureTest(){
365         stubValidateAndLockSuccess(CREATE_POLICY);
366         stubGetToscaFullElementSuccess();
367         stubGetToscaElementSuccess();
368         stubUpdatePolicyOfComponentSuccess();
369         stubRollback();
370         businessLogic.updatePolicyTargets(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, getTargetListFakeType(), USER_ID);
371
372     }
373
374     private void stubUpdatePolicyOfComponentSuccess() {
375         when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), eq(policy), any(PromoteVersionEnum.class))).thenReturn(policySuccessEither);
376     }
377
378
379     @Test(expected = ComponentException.class)
380     public void updatePolicyPropertiesFailureTest(){
381         stubValidateAndLockSuccess(CREATE_POLICY);
382         stubUnlockAndRollback();
383         policy.setProperties(null);
384         businessLogic.updatePolicyProperties(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, getProperties("Name", "Type") , USER_ID, true);
385     }
386
387     @Test
388     public void testDeclarePropertiesAsPoliciesSuccess() {
389         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(resource));
390         when(graphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
391         when(graphLockOperation.unlockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
392
393         when(propertyDeclarationOrchestrator.declarePropertiesToPolicies(any(), any())).thenReturn(Either.left(getDeclaredPolicies()));
394
395         Either<List<PolicyDefinition>, ResponseFormat> declaredPoliciesEither = businessLogic
396                                                                                           .declareProperties(USER_ID,
397                                                                                                   resource.getUniqueId(),
398                                                                                                   ComponentTypeEnum.RESOURCE,
399                                                                                                   getInputForPropertyToPolicyDeclaration());
400
401         assertTrue(declaredPoliciesEither.isLeft());
402
403         List<PolicyDefinition> declaredPolicies = declaredPoliciesEither.left().value();
404         assertTrue(CollectionUtils.isNotEmpty(declaredPolicies));
405         assertEquals(1, declaredPolicies.size());
406     }
407
408     @Test
409     public void testDeclarePropertiesAsPoliciesFailure() {
410         when(toscaOperationFacade.getToscaElement(eq(NON_EXIST_COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.right(StorageOperationStatus.NOT_FOUND));
411         when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND), eq(ComponentTypeEnum.RESOURCE))).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
412         try {
413             businessLogic
414                     .declareProperties(USER_ID,
415                             NON_EXIST_COMPONENT_ID,
416                             ComponentTypeEnum.RESOURCE,
417                             getInputForPropertyToPolicyDeclaration());
418         } catch (ComponentException e) {
419             assertEquals(ActionStatus.RESOURCE_NOT_FOUND, e.getActionStatus());
420             return;
421         }
422         fail();
423     }
424
425     private ComponentInstInputsMap getInputForPropertyToPolicyDeclaration() {
426         PropertyDefinition propertyDefinition = getPropertyDefinitionForDeclaration();
427
428         ComponentInstancePropInput componentInstancePropInput = new ComponentInstancePropInput();
429         componentInstancePropInput.setInput(propertyDefinition);
430         componentInstancePropInput.setPropertiesName(PROPERTY_NAME);
431
432         Map<String, List<ComponentInstancePropInput>> componentPropertiesToPolicies = new HashMap<>();
433         componentPropertiesToPolicies.put(resource.getUniqueId(), Collections.singletonList(componentInstancePropInput));
434
435         ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
436         componentInstInputsMap.setComponentInstancePropertiesToPolicies(componentPropertiesToPolicies);
437         return componentInstInputsMap;
438     }
439
440     private List<PolicyDefinition> getDeclaredPolicies() {
441         return Collections.singletonList(new PolicyDefinition(getPropertyDefinitionForDeclaration()));
442     }
443
444     private PropertyDefinition getPropertyDefinitionForDeclaration() {
445         PropertyDefinition propertyDefinition = new PropertyDefinition();
446         propertyDefinition.setUniqueId(PROPERTY_NAME);
447         propertyDefinition.setName(PROPERTY_NAME);
448         return propertyDefinition;
449     }
450
451     private PropertyDataDefinition[] getProperties(String prop1, String prop2) {
452         PropertyDataDefinition property1 = new PropertyDataDefinition();
453         property1.setName(prop1);
454         property1.setValue(prop1);
455         PropertyDataDefinition property2 = new PropertyDataDefinition();
456         property2.setName(prop2);
457         property2.setValue(prop2);
458         return new PropertyDataDefinition[]{property1, property2};
459     }
460     
461     
462     private void stubUnlockAndRollback() {
463         when(graphLockOperation.unlockComponent(eq(COMPONENT_ID), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
464         stubRollback();
465     }
466
467     private void stubCommit() {
468         when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
469     }
470
471     private void stubRollback() {
472         when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
473     }
474
475     private void stubUnlockAndCommit() {
476         when(graphLockOperation.unlockComponent(eq(COMPONENT_ID), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
477         stubCommit();
478     }
479     
480     private void stubValidateAndLockSuccess(String methodName) {
481         stubValidationSuccess(methodName);
482         when(graphLockOperation.lockComponent(eq(COMPONENT_ID), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
483    }
484
485     private void stubValidationSuccess(String methodName) {
486         when(userValidations.validateUserExists(eq(USER_ID))).thenReturn(user);
487         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(componentSuccessEither);
488     }
489
490     private void stubGetToscaFullElementSuccess() {
491         when(toscaOperationFacade.getToscaFullElement(eq(COMPONENT_ID))).thenReturn(buildElementEither());
492     }
493
494     private void stubGetToscaElementSuccess() {
495         when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID))).thenReturn(componentSuccessEither);
496     }
497
498     private Either<Component, StorageOperationStatus> buildElementEither() {
499         ResourceBuilder builder = new ResourceBuilder();
500         GroupDefinition groupDefinition = GroupDefinitionBuilder.create().setUniqueId(UNIQUE_ID_EXSISTS).build();
501         ComponentInstanceBuilder componentInstanceBuilder = new ComponentInstanceBuilder();
502         ComponentInstance componentInstance = componentInstanceBuilder.setUniqueId(UNIQUE_ID_EXSISTS).build();
503         return Either.left(builder.addGroup(groupDefinition).addComponentInstance(componentInstance).build());
504     }
505
506     private Map<PolicyTargetType, List<String>> getTargets() {
507         Map<PolicyTargetType, List<String>> targets = new HashMap<>();
508         targets.put(PolicyTargetType.COMPONENT_INSTANCES, Collections.singletonList(UNIQUE_ID_EXSISTS));
509         targets.put(PolicyTargetType.GROUPS, Collections.singletonList(UNIQUE_ID_EXSISTS));
510         return targets;
511     }
512
513
514
515     private static PolicyTypeDefinition buildPolicyType() {
516         PolicyTypeDefinition policyType = new PolicyTypeDefinition();
517         policyType.setType(POLICY_TYPE_NAME);
518         return policyType;
519     }
520
521     private static PolicyDefinition buildPolicy(String policyName) {
522         PolicyDefinition policy = new PolicyDefinition();
523         policy.setUniqueId(POLICY_ID);
524         policy.setPolicyTypeName(POLICY_TYPE_NAME);
525         policy.setComponentName(COMPONENT_NAME);
526         policy.setName(policyName);
527         return policy;
528     }
529
530     private static Resource buildResource() {
531         Resource resource = new Resource();
532         resource.setUniqueId(COMPONENT_ID);
533         resource.setName(COMPONENT_NAME);
534         resource.setCreatorUserId(USER_ID);
535         resource.setLastUpdaterUserId(USER_ID);
536         resource.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
537         resource.setIsDeleted(false);
538         resource.setResourceType(ResourceTypeEnum.VF);
539         resource.setToscaType(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
540         resource.setComponentType(ComponentTypeEnum.RESOURCE);
541         Map<String, PolicyDefinition> policies = new HashMap<>();
542         policies.put(POLICY_ID, policy);
543         resource.setPolicies(policies);
544         return resource;
545     }
546
547     private static User buildUser() {
548         User user = new User();
549         user.setUserId(USER_ID);
550         return user;
551     }
552
553     private Map<PolicyTargetType, List<String>> getTargetListFakeType() {
554         Map<PolicyTargetType, List<String>> targets = new HashMap<>();
555         targets.put(PolicyTargetType.TYPE_DOES_NOT_EXIST, Collections.singletonList(UNIQUE_ID_EXSISTS));
556         return targets;
557     }
558
559     private Map<PolicyTargetType, List<String>> getTargetListFakeId() {
560         Map<PolicyTargetType, List<String>> targets = new HashMap<>();
561         targets.put(PolicyTargetType.COMPONENT_INSTANCES, Collections.singletonList(UNIQUE_ID_DOESNT_EXSISTS));
562         targets.put(PolicyTargetType.GROUPS, Collections.singletonList(UNIQUE_ID_DOESNT_EXSISTS));
563         return targets;
564     }
565
566 }