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