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