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