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