2 * Copyright © 2016-2019 European Support Limited
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package org.openecomp.sdc.be.components.impl;
19 import static org.junit.jupiter.api.Assertions.assertEquals;
20 import static org.junit.jupiter.api.Assertions.assertFalse;
21 import static org.junit.jupiter.api.Assertions.assertNotNull;
22 import static org.junit.jupiter.api.Assertions.assertThrows;
23 import static org.junit.jupiter.api.Assertions.assertTrue;
24 import static org.junit.jupiter.api.Assertions.fail;
25 import static org.mockito.ArgumentMatchers.any;
26 import static org.mockito.ArgumentMatchers.anyMap;
27 import static org.mockito.ArgumentMatchers.eq;
28 import static org.mockito.Mockito.when;
29 import static org.openecomp.sdc.be.dao.api.ActionStatus.POLICY_TARGET_DOES_NOT_EXIST;
30 import static org.openecomp.sdc.be.dao.api.ActionStatus.PROPERTY_NOT_FOUND;
32 import fj.data.Either;
33 import java.util.ArrayList;
34 import java.util.Arrays;
35 import java.util.Collections;
36 import java.util.HashMap;
37 import java.util.List;
39 import org.apache.commons.collections.CollectionUtils;
40 import org.junit.jupiter.api.BeforeAll;
41 import org.junit.jupiter.api.BeforeEach;
42 import org.junit.jupiter.api.Test;
43 import org.junit.jupiter.api.extension.ExtendWith;
44 import org.mockito.InjectMocks;
45 import org.mockito.Mock;
46 import org.mockito.Mockito;
47 import org.mockito.MockitoAnnotations;
48 import org.mockito.junit.jupiter.MockitoExtension;
49 import org.openecomp.sdc.be.components.impl.exceptions.ByActionStatusComponentException;
50 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
51 import org.openecomp.sdc.be.components.property.PropertyDeclarationOrchestrator;
52 import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
53 import org.openecomp.sdc.be.components.utils.GroupDefinitionBuilder;
54 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
55 import org.openecomp.sdc.be.components.validation.UserValidations;
56 import org.openecomp.sdc.be.config.ConfigurationManager;
57 import org.openecomp.sdc.be.dao.api.ActionStatus;
58 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphDao;
59 import org.openecomp.sdc.be.dao.janusgraph.JanusGraphOperationStatus;
60 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
61 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
62 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
63 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
64 import org.openecomp.sdc.be.datatypes.enums.PromoteVersionEnum;
65 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
66 import org.openecomp.sdc.be.impl.ComponentsUtils;
67 import org.openecomp.sdc.be.model.Component;
68 import org.openecomp.sdc.be.model.ComponentInstInputsMap;
69 import org.openecomp.sdc.be.model.ComponentInstance;
70 import org.openecomp.sdc.be.model.ComponentInstancePropInput;
71 import org.openecomp.sdc.be.model.ComponentParametersView;
72 import org.openecomp.sdc.be.model.GroupDefinition;
73 import org.openecomp.sdc.be.model.LifecycleStateEnum;
74 import org.openecomp.sdc.be.model.PolicyDefinition;
75 import org.openecomp.sdc.be.model.PolicyTypeDefinition;
76 import org.openecomp.sdc.be.model.PropertyDefinition;
77 import org.openecomp.sdc.be.model.Resource;
78 import org.openecomp.sdc.be.model.Service;
79 import org.openecomp.sdc.be.model.User;
80 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
81 import org.openecomp.sdc.be.model.jsonjanusgraph.datamodel.ToscaElementTypeEnum;
82 import org.openecomp.sdc.be.model.jsonjanusgraph.operations.ToscaOperationFacade;
83 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
84 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
85 import org.openecomp.sdc.be.model.operations.impl.PolicyTypeOperation;
86 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
87 import org.openecomp.sdc.common.impl.ExternalConfiguration;
88 import org.openecomp.sdc.common.impl.FSConfigurationSource;
89 import org.openecomp.sdc.exception.ResponseFormat;
91 @ExtendWith(MockitoExtension.class)
92 class PolicyBusinessLogicTest {
94 private static final String COMPONENT_ID = "componentId";
95 private static final String NON_EXIST_COMPONENT_ID = "nonExistComponentId";
96 private static final String COMPONENT_NAME = "componentName";
97 private static final String POLICY_TYPE_NAME = "policyTypeName";
98 private static final String POLICY_ID = "policyId";
99 private static final String INVALID_POLICY_ID = "invalidPolicyId";
100 private static final String POLICY_NAME = "policyName";
101 private static final String OTHER_POLICY_NAME = "otherPolicyName";
102 private static final String USER_ID = "jh0003";
103 private static final String UNIQUE_ID_EXSISTS = "uniqueIdExists";
104 private static final String UNIQUE_ID_DOESNT_EXSISTS = "uniqueIdDoesntExists";
105 private static final String CREATE_POLICY = "create Policy";
106 private static final String PROPERTY_NAME = "propDefinition";
107 private static final User user = buildUser();
108 private static final PolicyDefinition policy = buildPolicy(POLICY_NAME);
109 private static final Resource resource = buildResource();
110 private static final PolicyDefinition otherPolicy = buildPolicy(OTHER_POLICY_NAME);
111 private static final PolicyTypeDefinition policyType = buildPolicyType();
112 private static Either<Component, StorageOperationStatus> componentSuccessEither;
113 private static Either<PolicyTypeDefinition, StorageOperationStatus> getPolicyTypeSuccessEither;
114 private static Either<PolicyDefinition, StorageOperationStatus> policySuccessEither;
115 private final ConfigurationManager configurationManager = new ConfigurationManager(
116 new FSConfigurationSource(ExternalConfiguration.getChangeListener(), "src/test/resources/config/catalog-be"));
118 private PropertyDeclarationOrchestrator propertyDeclarationOrchestrator;
120 private PolicyBusinessLogic businessLogic;
122 private ComponentsUtils componentsUtils;
124 private ToscaOperationFacade toscaOperationFacade;
126 private IGraphLockOperation graphLockOperation;
128 private PolicyTypeOperation policyTypeOperation;
130 private UserValidations userValidations;
132 private JanusGraphDao janusGraphDao;
134 private ApplicationDataTypeCache applicationDataTypeCache;
136 private PropertyOperation propertyOperation;
139 static void setup() {
143 private static void createResponses() {
144 componentSuccessEither = Either.left(resource);
145 getPolicyTypeSuccessEither = Either.left(policyType);
146 policySuccessEither = Either.left(policy);
149 private static PolicyTypeDefinition buildPolicyType() {
150 PolicyTypeDefinition policyType = new PolicyTypeDefinition();
151 policyType.setType(POLICY_TYPE_NAME);
155 private static PolicyDefinition buildPolicy(String policyName) {
156 PolicyDefinition policy = new PolicyDefinition();
157 policy.setUniqueId(POLICY_ID);
158 policy.setPolicyTypeName(POLICY_TYPE_NAME);
159 policy.setComponentName(COMPONENT_NAME);
160 policy.setName(policyName);
164 private static Resource buildResource() {
165 Resource resource = new Resource();
166 resource.setUniqueId(COMPONENT_ID);
167 resource.setName(COMPONENT_NAME);
168 resource.setCreatorUserId(USER_ID);
169 resource.setLastUpdaterUserId(USER_ID);
170 resource.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
171 resource.setIsDeleted(false);
172 resource.setResourceType(ResourceTypeEnum.VF);
173 resource.setToscaType(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
174 resource.setComponentType(ComponentTypeEnum.RESOURCE);
175 Map<String, PolicyDefinition> policies = new HashMap<>();
176 policies.put(POLICY_ID, policy);
177 resource.setPolicies(policies);
181 private static Service buildService() {
182 Service service = new Service();
183 service.setUniqueId(COMPONENT_ID);
184 service.setName(COMPONENT_NAME);
185 service.setCreatorUserId(USER_ID);
186 service.setLastUpdaterUserId(USER_ID);
187 service.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
188 service.setIsDeleted(false);
189 service.setToscaType(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
190 service.setComponentType(ComponentTypeEnum.SERVICE);
191 Map<String, PolicyDefinition> policies = new HashMap<>();
192 policies.put(POLICY_ID, policy);
193 service.setPolicies(policies);
197 private static User buildUser() {
198 return new User(USER_ID);
203 MockitoAnnotations.openMocks(this);
204 businessLogic.setComponentsUtils(componentsUtils);
205 businessLogic.setToscaOperationFacade(toscaOperationFacade);
206 businessLogic.setJanusGraphDao(janusGraphDao);
207 businessLogic.setUserValidations(userValidations);
208 businessLogic.setGraphLockOperation(graphLockOperation);
209 businessLogic.setPolicyTypeOperation(policyTypeOperation);
210 businessLogic.setApplicationDataTypeCache(applicationDataTypeCache);
211 businessLogic.setPropertyOperation(propertyOperation);
212 businessLogic.setPropertyDeclarationOrchestrator(propertyDeclarationOrchestrator);
216 void createPolicySuccessTest() {
217 stubValidateAndLockSuccess();
218 when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME), any())).thenReturn(getPolicyTypeSuccessEither);
219 when(toscaOperationFacade.associatePolicyToComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), eq(0))).thenReturn(policySuccessEither);
220 stubUnlockAndCommit();
221 PolicyDefinition response = businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
222 assertFalse(response.isEmpty());
226 void createPolicyOnResourceFromCsarDefinitionTest() {
227 String prop1 = "Prop_1";
228 String prop2 = "Prop_2";
229 Map<String, PolicyDefinition> policies = new HashMap<>();
230 PolicyDefinition policy = buildPolicy(POLICY_NAME);
231 Map<PolicyTargetType, List<String>> targets = getTargets();
232 PropertyDataDefinition[] properties = getProperties(prop1, prop2);
233 policy.setTargets(targets);
234 policy.setProperties(Arrays.asList(properties));
235 policies.put(POLICY_NAME, policy);
237 List<ComponentInstance> instanceList = new ArrayList<>();
238 ComponentInstance componentInstance = new ComponentInstance();
239 componentInstance.setUniqueId(UNIQUE_ID_EXSISTS);
240 componentInstance.setName(UNIQUE_ID_EXSISTS);
241 instanceList.add(componentInstance);
243 Resource newResource = buildResource();
244 newResource.setPolicies(policies);
245 newResource.setComponentInstances(instanceList);
247 when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME), any())).thenReturn(getPolicyTypeSuccessEither);
248 when(toscaOperationFacade.associatePolicyToComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), eq(0))).thenReturn(Either.left(policy));
249 when(toscaOperationFacade.getToscaFullElement(COMPONENT_ID)).thenReturn(Either.left(newResource));
250 when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), any(PromoteVersionEnum.class))).thenReturn(
251 Either.left(policy));
252 when(propertyOperation.validateAndUpdatePropertyValue(eq(newResource), eq(properties[0]), anyMap())).thenReturn(Either.left(prop1));
253 when(propertyOperation.validateAndUpdatePropertyValue(eq(newResource), eq(properties[1]), anyMap())).thenReturn(Either.left(prop2));
255 Map<String, PolicyDefinition> createdPolicy = businessLogic.createPolicies(newResource, policies);
257 assertFalse(createdPolicy.isEmpty());
258 PolicyDefinition newPolicy = createdPolicy.get(POLICY_NAME);
259 assertNotNull(newPolicy);
260 assertTrue(newPolicy.getIsFromCsar());
261 assertNotNull(newPolicy.getTargets());
262 assertNotNull(newPolicy.getProperties());
263 assertEquals(2, newPolicy.getProperties().size());
264 assertEquals(1, newPolicy.getTargets().size());
268 void createPolicyOnServiceFromCsarDefinitionTest() {
269 String prop1 = "Prop_1";
270 String prop2 = "Prop_2";
271 Map<String, PolicyDefinition> policies = new HashMap<>();
272 PolicyDefinition policy = buildPolicy(POLICY_NAME);
273 Map<PolicyTargetType, List<String>> targets = getTargets();
274 PropertyDataDefinition[] properties = getProperties(prop1, prop2);
275 policy.setTargets(targets);
276 policy.setProperties(Arrays.asList(properties));
277 policies.put(POLICY_NAME, policy);
279 List<ComponentInstance> instanceList = new ArrayList<>();
280 ComponentInstance componentInstance = new ComponentInstance();
281 componentInstance.setUniqueId(UNIQUE_ID_EXSISTS);
282 componentInstance.setName(UNIQUE_ID_EXSISTS);
283 instanceList.add(componentInstance);
285 Service newService = buildService();
286 newService.setPolicies(policies);
287 newService.setComponentInstances(instanceList);
289 when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME), any())).thenReturn(getPolicyTypeSuccessEither);
290 when(toscaOperationFacade.associatePolicyToComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), eq(0))).thenReturn(Either.left(policy));
291 when(toscaOperationFacade.getToscaFullElement(COMPONENT_ID)).thenReturn(Either.left(newService));
292 when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), any(PromoteVersionEnum.class))).thenReturn(
293 Either.left(policy));
294 when(propertyOperation.validateAndUpdatePropertyValue(eq(newService), eq(properties[0]), anyMap())).thenReturn(Either.left(prop1));
295 when(propertyOperation.validateAndUpdatePropertyValue(eq(newService), eq(properties[1]), anyMap())).thenReturn(Either.left(prop2));
297 Map<String, PolicyDefinition> createdPolicy = businessLogic.createPolicies(newService, policies);
299 assertFalse(createdPolicy.isEmpty());
300 PolicyDefinition newPolicy = createdPolicy.get(POLICY_NAME);
301 assertNotNull(newPolicy);
302 assertTrue(newPolicy.getIsFromCsar());
303 assertNotNull(newPolicy.getTargets());
304 assertNotNull(newPolicy.getProperties());
305 assertEquals(2, newPolicy.getProperties().size());
306 assertEquals(1, newPolicy.getTargets().size());
307 assertTrue(newPolicy.getTargets().containsKey(PolicyTargetType.COMPONENT_INSTANCES));
308 assertEquals(2, newPolicy.getTargets().get(PolicyTargetType.COMPONENT_INSTANCES).size());
309 newPolicy.getTargets().get(PolicyTargetType.COMPONENT_INSTANCES).forEach(t -> assertTrue(t.equals(UNIQUE_ID_EXSISTS)));
310 newPolicy.getProperties().forEach(p -> assertTrue(p.getName().equals(prop1) || p.getName().equals(prop2)));
314 void createPolicyUserFailureTest() {
315 ByActionStatusComponentException userNotFoundException = new ByActionStatusComponentException(ActionStatus.USER_NOT_FOUND);
316 when(userValidations.validateUserExists(USER_ID)).thenThrow(userNotFoundException);
318 businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
319 } catch (ByActionStatusComponentException e) {
320 assertEquals(e.getActionStatus(), userNotFoundException.getActionStatus());
325 void createPolicyComponentFailureTest() {
326 when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
327 Either<Component, StorageOperationStatus> componentNotFoundResponse = Either.right(StorageOperationStatus.NOT_FOUND);
328 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.RESOURCE)).thenReturn(
329 ActionStatus.RESOURCE_NOT_FOUND);
330 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(componentNotFoundResponse);
331 assertThrows(ComponentException.class,
332 () -> businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true));
336 void createPolicyPolicyTypeFailureTest() {
337 stubValidateAndLockSuccess();
338 Either<PolicyTypeDefinition, StorageOperationStatus> getPolicyTypeFailed = Either.right(StorageOperationStatus.NOT_FOUND);
339 when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME), any())).thenReturn(getPolicyTypeFailed);
340 when(componentsUtils.convertFromStorageResponse(getPolicyTypeFailed.right().value())).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
341 stubUnlockAndRollback();
342 assertThrows(ComponentException.class,
343 () -> businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true));
347 void createPolicyComponentTypeFailureTest() {
348 stubValidateAndLockSuccess();
349 when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME), any())).thenReturn(getPolicyTypeSuccessEither);
350 Either<PolicyDefinition, StorageOperationStatus> addPolicyRes = Either.right(StorageOperationStatus.BAD_REQUEST);
351 when(toscaOperationFacade.associatePolicyToComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), eq(0))).thenReturn(addPolicyRes);
352 when(componentsUtils.convertFromStorageResponse(addPolicyRes.right().value())).thenReturn(ActionStatus.INVALID_CONTENT);
354 stubUnlockAndRollback();
355 assertThrows(ComponentException.class,
356 () -> businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true));
360 void updatePolicySuccessTest() {
361 stubValidateAndLockSuccess();
362 when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), any(PromoteVersionEnum.class))).thenReturn(
363 policySuccessEither);
364 stubUnlockAndCommit();
365 PolicyDefinition response = businessLogic.updatePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, otherPolicy, USER_ID, true);
366 assertFalse(response.isEmpty());
370 void updatePolicyNameFailureTest() {
371 stubValidateAndLockSuccess();
372 stubUnlockAndRollback();
373 assertThrows(ComponentException.class, () -> businessLogic.updatePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, policy, USER_ID, true));
377 void getPolicySuccessTest() {
378 stubValidationSuccess();
379 PolicyDefinition response = businessLogic.getPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, USER_ID);
380 assertFalse(response.isEmpty());
384 void getPolicyFailureTest() {
385 stubValidationSuccess();
386 assertThrows(ComponentException.class, () -> businessLogic.getPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, INVALID_POLICY_ID, USER_ID));
390 void deletePolicySuccessTest() {
391 stubValidateAndLockSuccess();
393 when(toscaOperationFacade.removePolicyFromComponent(COMPONENT_ID, POLICY_ID)).thenReturn(StorageOperationStatus.OK);
394 PolicyDefinition response = businessLogic.deletePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, USER_ID, true);
395 assertFalse(response.isEmpty());
399 void deletePolicyFailureTest() {
400 stubValidateAndLockSuccess();
401 assertThrows(ComponentException.class,
402 () -> businessLogic.deletePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, INVALID_POLICY_ID, USER_ID, true));
406 void updatePolicyPropertiesSuccessTest() {
407 stubValidateAndLockSuccess();
408 String prop1 = "Name";
409 String prop2 = "Type";
411 when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), any(PromoteVersionEnum.class))).thenReturn(
412 policySuccessEither);
413 stubUnlockAndCommit();
414 PropertyDataDefinition[] properties = getProperties(prop1, prop2);
415 when(propertyOperation.validateAndUpdatePropertyValue(any(Resource.class), eq(properties[0]), anyMap())).thenReturn(Either.left(prop1));
416 when(propertyOperation.validateAndUpdatePropertyValue(any(Resource.class), eq(properties[1]), anyMap())).thenReturn(Either.left(prop2));
417 policy.setProperties(Arrays.asList(properties));
418 List<PropertyDataDefinition> updatedProperties =
419 businessLogic.updatePolicyProperties(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, properties, USER_ID, true);
420 assertEquals(2, updatedProperties.size());
424 void updatePolicyTargetsSuccessTest() {
425 stubValidateAndLockSuccess();
426 stubGetToscaFullElementSuccess();
427 stubUpdatePolicyOfComponentSuccess();
428 stubGetToscaElementSuccess();
429 PolicyDefinition policyResult = businessLogic.updatePolicyTargets(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, getTargets(), USER_ID);
430 Map<PolicyTargetType, List<String>> targets = getTargets();
431 assertTrue(policyResult.getTargets().values()
432 .containsAll(Arrays.asList(targets.get(PolicyTargetType.GROUPS), targets.get(PolicyTargetType.COMPONENT_INSTANCES))));
437 void updatePolicyTargetsTargetIDFailureTest() {
438 stubValidateAndLockSuccess();
439 stubGetToscaFullElementSuccess();
440 stubGetToscaElementSuccess();
442 Map<PolicyTargetType, List<String>> targetListFakeId = getTargetListFakeId();
443 final var componentException = assertThrows(ComponentException.class,
444 () -> businessLogic.updatePolicyTargets(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, targetListFakeId, USER_ID));
445 assertNotNull(componentException);
446 assertEquals(POLICY_TARGET_DOES_NOT_EXIST, componentException.getActionStatus());
447 assertEquals(400, componentException.getResponseFormat().getStatus());
448 assertEquals(1, componentException.getParams().length);
449 assertEquals(UNIQUE_ID_DOESNT_EXSISTS, componentException.getParams()[0]);
453 void updatePolicyTargetsTypeFailureTest() {
454 stubValidateAndLockSuccess();
455 stubGetToscaFullElementSuccess();
456 stubGetToscaElementSuccess();
458 Map<PolicyTargetType, List<String>> targetListFakeType = getTargetListFakeType();
459 final var componentException = assertThrows(ComponentException.class,
460 () -> businessLogic.updatePolicyTargets(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, targetListFakeType, USER_ID));
461 assertNotNull(componentException);
462 assertEquals(POLICY_TARGET_DOES_NOT_EXIST, componentException.getActionStatus());
463 assertEquals(400, componentException.getResponseFormat().getStatus());
464 assertEquals(1, componentException.getParams().length);
465 assertEquals(UNIQUE_ID_EXSISTS, componentException.getParams()[0]);
468 private void stubUpdatePolicyOfComponentSuccess() {
469 when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), eq(policy), any(PromoteVersionEnum.class))).thenReturn(
470 policySuccessEither);
474 void updatePolicyPropertiesFailureTest() {
475 stubValidateAndLockSuccess();
476 when(graphLockOperation.unlockComponent(eq(COMPONENT_ID), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
477 policy.setProperties(null);
478 PropertyDataDefinition[] properties = getProperties("Name", "Type");
479 final var componentException = assertThrows(ComponentException.class,
480 () -> businessLogic.updatePolicyProperties(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, properties, USER_ID, true));
481 assertNotNull(componentException);
482 assertEquals(PROPERTY_NOT_FOUND, componentException.getActionStatus());
483 assertEquals(404, componentException.getResponseFormat().getStatus());
484 assertEquals(0, componentException.getParams().length);
488 void testDeclarePropertiesAsPoliciesSuccess() {
489 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(Either.left(resource));
490 when(graphLockOperation.lockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
491 when(graphLockOperation.unlockComponent(any(), any())).thenReturn(StorageOperationStatus.OK);
493 when(propertyDeclarationOrchestrator.declarePropertiesToPolicies(any(), any())).thenReturn(Either.left(getDeclaredPolicies()));
495 Either<List<PolicyDefinition>, ResponseFormat> declaredPoliciesEither = businessLogic
496 .declareProperties(USER_ID,
497 resource.getUniqueId(),
498 ComponentTypeEnum.RESOURCE,
499 getInputForPropertyToPolicyDeclaration());
501 assertTrue(declaredPoliciesEither.isLeft());
503 List<PolicyDefinition> declaredPolicies = declaredPoliciesEither.left().value();
504 assertTrue(CollectionUtils.isNotEmpty(declaredPolicies));
505 assertEquals(1, declaredPolicies.size());
509 void testDeclarePropertiesAsPoliciesFailure() {
510 when(toscaOperationFacade.getToscaElement(eq(NON_EXIST_COMPONENT_ID), Mockito.any(ComponentParametersView.class))).thenReturn(
511 Either.right(StorageOperationStatus.NOT_FOUND));
512 when(componentsUtils.convertFromStorageResponse(StorageOperationStatus.NOT_FOUND, ComponentTypeEnum.RESOURCE)).thenReturn(
513 ActionStatus.RESOURCE_NOT_FOUND);
516 .declareProperties(USER_ID,
517 NON_EXIST_COMPONENT_ID,
518 ComponentTypeEnum.RESOURCE,
519 getInputForPropertyToPolicyDeclaration());
520 } catch (ComponentException e) {
521 assertEquals(ActionStatus.RESOURCE_NOT_FOUND, e.getActionStatus());
527 private ComponentInstInputsMap getInputForPropertyToPolicyDeclaration() {
528 PropertyDefinition propertyDefinition = getPropertyDefinitionForDeclaration();
530 ComponentInstancePropInput componentInstancePropInput = new ComponentInstancePropInput();
531 componentInstancePropInput.setInput(propertyDefinition);
532 componentInstancePropInput.setPropertiesName(PROPERTY_NAME);
534 Map<String, List<ComponentInstancePropInput>> componentPropertiesToPolicies = new HashMap<>();
535 componentPropertiesToPolicies.put(resource.getUniqueId(), Collections.singletonList(componentInstancePropInput));
537 ComponentInstInputsMap componentInstInputsMap = new ComponentInstInputsMap();
538 componentInstInputsMap.setComponentInstancePropertiesToPolicies(componentPropertiesToPolicies);
539 return componentInstInputsMap;
542 private List<PolicyDefinition> getDeclaredPolicies() {
543 return Collections.singletonList(new PolicyDefinition(getPropertyDefinitionForDeclaration()));
546 private PropertyDefinition getPropertyDefinitionForDeclaration() {
547 PropertyDefinition propertyDefinition = new PropertyDefinition();
548 propertyDefinition.setUniqueId(PROPERTY_NAME);
549 propertyDefinition.setName(PROPERTY_NAME);
550 return propertyDefinition;
553 private PropertyDataDefinition[] getProperties(String prop1, String prop2) {
554 PropertyDataDefinition property1 = new PropertyDataDefinition();
555 property1.setName(prop1);
556 property1.setValue(prop1);
557 PropertyDataDefinition property2 = new PropertyDataDefinition();
558 property2.setName(prop2);
559 property2.setValue(prop2);
560 return new PropertyDataDefinition[]{property1, property2};
563 private void stubUnlockAndRollback() {
564 when(graphLockOperation.unlockComponent(eq(COMPONENT_ID), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
568 private void stubCommit() {
569 when(janusGraphDao.commit()).thenReturn(JanusGraphOperationStatus.OK);
572 private void stubRollback() {
573 when(janusGraphDao.rollback()).thenReturn(JanusGraphOperationStatus.OK);
576 private void stubUnlockAndCommit() {
577 when(graphLockOperation.unlockComponent(eq(COMPONENT_ID), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
581 private void stubValidateAndLockSuccess() {
582 stubValidationSuccess();
583 when(graphLockOperation.lockComponent(eq(COMPONENT_ID), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
586 private void stubValidationSuccess() {
587 when(userValidations.validateUserExists(USER_ID)).thenReturn(user);
588 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(componentSuccessEither);
591 private void stubGetToscaFullElementSuccess() {
592 when(toscaOperationFacade.getToscaFullElement(COMPONENT_ID)).thenReturn(buildElementEither());
595 private void stubGetToscaElementSuccess() {
596 when(toscaOperationFacade.getToscaElement(COMPONENT_ID)).thenReturn(componentSuccessEither);
599 private Either<Component, StorageOperationStatus> buildElementEither() {
600 ResourceBuilder builder = new ResourceBuilder();
601 GroupDefinition groupDefinition = GroupDefinitionBuilder.create().setUniqueId(UNIQUE_ID_EXSISTS).build();
602 ComponentInstanceBuilder componentInstanceBuilder = new ComponentInstanceBuilder();
603 ComponentInstance componentInstance = componentInstanceBuilder.setUniqueId(UNIQUE_ID_EXSISTS).build();
604 return Either.left(builder.addGroup(groupDefinition).addComponentInstance(componentInstance).build());
607 private Map<PolicyTargetType, List<String>> getTargets() {
608 Map<PolicyTargetType, List<String>> targets = new HashMap<>();
609 targets.put(PolicyTargetType.COMPONENT_INSTANCES, Collections.singletonList(UNIQUE_ID_EXSISTS));
610 targets.put(PolicyTargetType.GROUPS, Collections.singletonList(UNIQUE_ID_EXSISTS));
614 private Map<PolicyTargetType, List<String>> getTargetListFakeType() {
615 Map<PolicyTargetType, List<String>> targets = new HashMap<>();
616 targets.put(PolicyTargetType.TYPE_DOES_NOT_EXIST, Collections.singletonList(UNIQUE_ID_EXSISTS));
620 private Map<PolicyTargetType, List<String>> getTargetListFakeId() {
621 Map<PolicyTargetType, List<String>> targets = new HashMap<>();
622 targets.put(PolicyTargetType.COMPONENT_INSTANCES, Collections.singletonList(UNIQUE_ID_DOESNT_EXSISTS));
623 targets.put(PolicyTargetType.GROUPS, Collections.singletonList(UNIQUE_ID_DOESNT_EXSISTS));