1 package org.openecomp.sdc.be.components.impl;
5 import org.junit.Assert;
6 import org.junit.BeforeClass;
8 import org.junit.runner.RunWith;
9 import org.mockito.InjectMocks;
10 import org.mockito.Mock;
11 import org.mockito.junit.MockitoJUnitRunner;
12 import org.openecomp.sdc.be.components.impl.exceptions.ComponentException;
13 import org.openecomp.sdc.be.components.utils.ComponentInstanceBuilder;
14 import org.openecomp.sdc.be.components.utils.GroupDefinitionBuilder;
15 import org.openecomp.sdc.be.components.utils.ResourceBuilder;
16 import org.openecomp.sdc.be.components.validation.UserValidations;
17 import org.openecomp.sdc.be.config.ConfigurationManager;
18 import org.openecomp.sdc.be.dao.api.ActionStatus;
19 import org.openecomp.sdc.be.dao.jsongraph.TitanDao;
20 import org.openecomp.sdc.be.dao.titan.TitanOperationStatus;
21 import org.openecomp.sdc.be.datatypes.elements.PolicyTargetType;
22 import org.openecomp.sdc.be.datatypes.elements.PropertyDataDefinition;
23 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
24 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
25 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
26 import org.openecomp.sdc.be.impl.ComponentsUtils;
27 import org.openecomp.sdc.be.model.*;
28 import org.openecomp.sdc.be.model.cache.ApplicationDataTypeCache;
29 import org.openecomp.sdc.be.model.jsontitan.datamodel.ToscaElementTypeEnum;
30 import org.openecomp.sdc.be.model.jsontitan.operations.ToscaOperationFacade;
31 import org.openecomp.sdc.be.model.operations.api.IGraphLockOperation;
32 import org.openecomp.sdc.be.model.operations.api.StorageOperationStatus;
33 import org.openecomp.sdc.be.model.operations.impl.PolicyTypeOperation;
34 import org.openecomp.sdc.be.model.operations.impl.PropertyOperation;
35 import org.openecomp.sdc.common.impl.ExternalConfiguration;
36 import org.openecomp.sdc.common.impl.FSConfigurationSource;
37 import org.openecomp.sdc.exception.ResponseFormat;
41 import static org.assertj.core.api.Java6Assertions.assertThat;
42 import static org.junit.Assert.assertEquals;
43 import static org.junit.Assert.assertTrue;
44 import static org.mockito.ArgumentMatchers.*;
45 import static org.mockito.Mockito.when;
47 @RunWith(MockitoJUnitRunner.class)
48 public class PolicyBusinessLogicTest {
51 private PolicyBusinessLogic businessLogic;
53 private ComponentsUtils componentsUtils;
55 private ToscaOperationFacade toscaOperationFacade;
57 private IGraphLockOperation graphLockOperation;
59 private PolicyTypeOperation policyTypeOperation;
61 private UserValidations userValidations;
63 private TitanDao titanDao;
65 private ApplicationDataTypeCache dataTypeCache;
67 private PropertyOperation propertyOperation;
69 private final static String COMPONENT_ID = "componentId";
70 private final static String COMPONENT_NAME = "componentName";
71 private final static String POLICY_TYPE_NAME = "policyTypeName";
72 private final static String POLICY_ID = "policyId";
73 private final static String INVALID_POLICY_ID = "invalidPolicyId";
74 private final static String POLICY_NAME = "policyName";
75 private final static String OTHER_POLICY_NAME = "otherPolicyName";
76 private final static String USER_ID = "jh0003";
77 private final static String UNIQUE_ID_EXSISTS = "uniqueIdExists";
78 private final static String UNIQUE_ID_DOESNT_EXSISTS = "uniqueIdDoesntExists";
79 private final static String CREATE_POLICY = "create Policy";
80 private final static User user = buildUser();
81 private final static PolicyDefinition policy = buildPolicy(POLICY_NAME);
82 private final static PolicyDefinition otherPolicy = buildPolicy(OTHER_POLICY_NAME);
83 private final static Resource resource = buildResource();
84 private final static PolicyTypeDefinition policyType = buildPolicyType();
86 private static Either<Component, StorageOperationStatus> componentSuccessEither;
87 private static Either<PolicyTypeDefinition, StorageOperationStatus> getPolicyTypeSuccessEither;
88 private static Either<PolicyDefinition, StorageOperationStatus> policySuccessEither;
89 private static ResponseFormat notFoundResponse;
90 private static ResponseFormat invalidContentResponse;
91 private static ResponseFormat nameExistsResponse;
94 public static void setup() {
95 String appConfigDir = "src/test/resources/config/catalog-be";
96 new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir));
98 new ConfigurationManager(new FSConfigurationSource(ExternalConfiguration.getChangeListener(), appConfigDir));
101 private static void createResponses() {
102 componentSuccessEither = Either.left(resource);
103 getPolicyTypeSuccessEither = Either.left(policyType);
104 policySuccessEither = Either.left(policy);
105 notFoundResponse = new ResponseFormat();
106 notFoundResponse.setStatus(404);
107 invalidContentResponse = new ResponseFormat();
108 invalidContentResponse.setStatus(400);
109 nameExistsResponse = new ResponseFormat();
110 nameExistsResponse.setStatus(409);
114 public void createPolicySuccessTest(){
115 stubValidateAndLockSuccess(CREATE_POLICY);
116 when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME))).thenReturn(getPolicyTypeSuccessEither);
117 when(toscaOperationFacade.associatePolicyToComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), eq(0))).thenReturn(policySuccessEither);
118 stubUnlockAndCommit();
119 Either<PolicyDefinition, ResponseFormat> response = businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
120 assertTrue(response.isLeft());
124 public void createPolicyUserFailureTest(){
125 ComponentException userNotFoundException = new ComponentException(ActionStatus.USER_NOT_FOUND);
126 when(userValidations.validateUserExists(eq(USER_ID), eq(CREATE_POLICY), eq(false))).thenThrow(userNotFoundException);
129 businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
130 } catch(ComponentException e){
131 assertEquals(e.getActionStatus(), userNotFoundException.getActionStatus());
135 private void assertNotFound(Either<PolicyDefinition, ResponseFormat> response) {
136 assertTrue(response.isRight() && response.right().value().getStatus().equals(404));
140 public void createPolicyComponentFailureTest(){
141 when(userValidations.validateUserExists(eq(USER_ID), eq(CREATE_POLICY), eq(false))).thenReturn(user);
142 Either<Component, StorageOperationStatus> componentNotFoundResponse = Either.right(StorageOperationStatus.NOT_FOUND);
143 when(componentsUtils.convertFromStorageResponse(eq(StorageOperationStatus.NOT_FOUND), eq(ComponentTypeEnum.RESOURCE))).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
144 when(componentsUtils.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND), anyString())).thenReturn(notFoundResponse);
145 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(componentNotFoundResponse);
146 Either<PolicyDefinition, ResponseFormat> response = businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
147 assertNotFound(response);
151 public void createPolicyPolicyTypeFailureTest(){
152 stubValidateAndLockSuccess(CREATE_POLICY);
153 Either<PolicyTypeDefinition, StorageOperationStatus> getPolicyTypeFailed = Either.right(StorageOperationStatus.NOT_FOUND);
154 when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME))).thenReturn(getPolicyTypeFailed);
155 when(componentsUtils.convertFromStorageResponse(eq(getPolicyTypeFailed.right().value()))).thenReturn(ActionStatus.RESOURCE_NOT_FOUND);
156 when(componentsUtils.getResponseFormat(eq(ActionStatus.RESOURCE_NOT_FOUND))).thenReturn(notFoundResponse);
157 stubUnlockAndRollback();
158 Either<PolicyDefinition, ResponseFormat> response = businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
159 assertNotFound(response);
163 public void createPolicyComponentTypeFailureTest(){
164 stubValidateAndLockSuccess(CREATE_POLICY);
165 when(policyTypeOperation.getLatestPolicyTypeByType(eq(POLICY_TYPE_NAME))).thenReturn(getPolicyTypeSuccessEither);
166 Either<PolicyDefinition, StorageOperationStatus> addPolicyRes = Either.right(StorageOperationStatus.BAD_REQUEST);
167 when(toscaOperationFacade.associatePolicyToComponent(eq(COMPONENT_ID), any(PolicyDefinition.class), eq(0))).thenReturn(addPolicyRes);
168 when(componentsUtils.convertFromStorageResponse(eq(addPolicyRes.right().value()))).thenReturn(ActionStatus.INVALID_CONTENT);
169 when(componentsUtils.getResponseFormat(eq(ActionStatus.INVALID_CONTENT))).thenReturn(invalidContentResponse);
171 stubUnlockAndRollback();
172 Either<PolicyDefinition, ResponseFormat> response = businessLogic.createPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_TYPE_NAME, USER_ID, true);
173 assertTrue(response.isRight() && response.right().value().getStatus().equals(400));
177 public void updatePolicySuccessTest(){
178 stubValidateAndLockSuccess(CREATE_POLICY);
179 when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), any(PolicyDefinition.class))).thenReturn(policySuccessEither);
180 stubUnlockAndCommit();
181 Either<PolicyDefinition, ResponseFormat> response = businessLogic.updatePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, otherPolicy, USER_ID, true);
182 assertTrue(response.isLeft());
186 public void updatePolicyNameFailureTest(){
187 stubValidateAndLockSuccess(CREATE_POLICY);
188 when(componentsUtils.getResponseFormat(eq(ActionStatus.POLICY_NAME_ALREADY_EXIST), eq(POLICY_NAME))).thenReturn(nameExistsResponse);
189 stubUnlockAndRollback();
190 Either<PolicyDefinition, ResponseFormat> response = businessLogic.updatePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, policy, USER_ID, true);
191 assertTrue(response.isRight() && response.right().value().getStatus().equals(409));
195 public void getPolicySuccessTest(){
196 stubValidationSuccess(CREATE_POLICY);
198 Either<PolicyDefinition, ResponseFormat> response = businessLogic.getPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, USER_ID);
199 assertTrue(response.isLeft());
203 public void getPolicyFailureTest(){
204 stubValidationSuccess(CREATE_POLICY);
206 when(componentsUtils.getResponseFormat(eq(ActionStatus.POLICY_NOT_FOUND_ON_CONTAINER), eq(INVALID_POLICY_ID), eq(COMPONENT_ID))).thenReturn(notFoundResponse);
207 Either<PolicyDefinition, ResponseFormat> response = businessLogic.getPolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, INVALID_POLICY_ID, USER_ID);
208 assertTrue(response.isRight() && response.right().value().getStatus().equals(404));
212 public void deletePolicySuccessTest(){
213 stubValidateAndLockSuccess(CREATE_POLICY);
215 when(toscaOperationFacade.removePolicyFromComponent(eq(COMPONENT_ID),eq(POLICY_ID))).thenReturn(StorageOperationStatus.OK);
216 Either<PolicyDefinition, ResponseFormat> response = businessLogic.deletePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, USER_ID, true);
217 assertTrue(response.isLeft());
221 public void deletePolicyFailureTest(){
222 stubValidateAndLockSuccess(CREATE_POLICY);
224 stubComponentUtilsGetResponsePOLICY_NOT_FOUND_ON_CONTAINER();
225 Either<PolicyDefinition, ResponseFormat> response = businessLogic.deletePolicy(ComponentTypeEnum.RESOURCE, COMPONENT_ID, INVALID_POLICY_ID, USER_ID, true);
226 assertNotFound(response);
229 private void stubComponentUtilsGetResponsePOLICY_NOT_FOUND_ON_CONTAINER() {
230 when(componentsUtils.getResponseFormat(eq(ActionStatus.POLICY_NOT_FOUND_ON_CONTAINER), anyString(), anyString())).thenReturn(new ResponseFormat(404));
234 public void updatePolicyPropertiesSuccessTest(){
235 stubValidateAndLockSuccess(CREATE_POLICY);
236 when(dataTypeCache.getAll()).thenReturn(Either.left(new HashMap<>()));
237 String prop1 = "Name";
238 String prop2 = "Type";
239 when(propertyOperation.validateAndUpdatePropertyValue(eq(null), eq(prop1), anyBoolean(), eq(null), anyMap())).thenReturn(Either.left(prop1));
240 when(propertyOperation.validateAndUpdatePropertyValue(eq(null), eq(prop2), anyBoolean(), eq(null), anyMap())).thenReturn(Either.left(prop2));
241 when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), any(PolicyDefinition.class))).thenReturn(policySuccessEither);
242 stubUnlockAndCommit();
243 PropertyDataDefinition[] properties = getProperties(prop1, prop2);
244 policy.setProperties(Arrays.asList(properties));
245 Either<List<PropertyDataDefinition>, ResponseFormat> response = businessLogic.updatePolicyProperties(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, properties , USER_ID, true);
246 assertTrue(response.isLeft());
247 List<PropertyDataDefinition> updatedProperties = response.left().value();
248 assertThat(updatedProperties.size()).isEqualTo(2);
252 public void updatePolicyTargetsSuccessTest(){
253 stubValidateAndLockSuccess(CREATE_POLICY);
254 stubGetToscaFullElementSuccess();
255 stubUpdatePolicyOfComponentSuccess();
256 stubGetToscaElementSuccess();
257 Either<PolicyDefinition, ResponseFormat> result = businessLogic.updatePolicyTargets(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, getTargets(), USER_ID);
258 Assert.assertTrue(result.isLeft());
259 PolicyDefinition policyResult = result.left().value();
260 Map<PolicyTargetType, List<String>> targets = getTargets();
261 assertThat(policyResult.getTargets().values()).usingFieldByFieldElementComparator().containsExactlyInAnyOrder(targets.get(PolicyTargetType.GROUPS), targets.get(PolicyTargetType.COMPONENT_INSTANCES));
266 public void updatePolicyTargetsTargetIDFailureTest(){
267 stubValidateAndLockSuccess(CREATE_POLICY);
268 stubGetToscaFullElementSuccess();
269 stubGetToscaElementSuccess();
270 stubUpdatePolicyOfComponentSuccess();
271 stubComponentUtilsGetResponseTargetNotFound();
274 Either<PolicyDefinition, ResponseFormat> result = businessLogic.updatePolicyTargets(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, getTargetListFakeId(), USER_ID);
276 Assert.assertTrue(result.isRight());
277 ResponseFormat responseResult = result.right().value();
278 Assert.assertEquals(responseResult.getStatus().longValue(), 400L);
282 private void stubComponentUtilsGetResponseTargetNotFound() {
283 when(componentsUtils.getResponseFormat(eq(ActionStatus.POLICY_TARGET_DOES_NOT_EXIST), (anyString()))).thenReturn(new ResponseFormat(400));
287 public void updatePolicyTargetsTypeFailureTest(){
288 stubValidateAndLockSuccess(CREATE_POLICY);
289 stubGetToscaFullElementSuccess();
290 stubGetToscaElementSuccess();
291 stubUpdatePolicyOfComponentSuccess();
292 stubComponentUtilsGetResponseTargetNotFound();
295 Either<PolicyDefinition, ResponseFormat> result = businessLogic.updatePolicyTargets(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, getTargetListFakeType(), USER_ID);
297 Assert.assertTrue(result.isRight());
298 ResponseFormat responseResult = result.right().value();
299 Assert.assertEquals(400, (int) responseResult.getStatus());
303 private void stubUpdatePolicyOfComponentSuccess() {
304 when(toscaOperationFacade.updatePolicyOfComponent(eq(COMPONENT_ID), eq(policy))).thenReturn(policySuccessEither);
309 public void updatePolicyPropertiesFailureTest(){
310 stubValidateAndLockSuccess(CREATE_POLICY);
311 when(componentsUtils.getResponseFormat(eq(ActionStatus.PROPERTY_NOT_FOUND))).thenReturn(notFoundResponse);
312 stubUnlockAndRollback();
313 policy.setProperties(null);
314 Either<List<PropertyDataDefinition>, ResponseFormat> response = businessLogic.updatePolicyProperties(ComponentTypeEnum.RESOURCE, COMPONENT_ID, POLICY_ID, getProperties("Name", "Type") , USER_ID, true);
315 assertTrue(response.isRight() && response.right().value().getStatus().equals(404));
318 private PropertyDataDefinition[] getProperties(String prop1, String prop2) {
319 PropertyDataDefinition property1 = new PropertyDataDefinition();
320 property1.setName(prop1);
321 property1.setValue(prop1);
322 PropertyDataDefinition property2 = new PropertyDataDefinition();
323 property2.setName(prop2);
324 property2.setValue(prop2);
325 return new PropertyDataDefinition[]{property1, property2};
329 private void stubUnlockAndRollback() {
330 when(graphLockOperation.unlockComponent(eq(COMPONENT_ID), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
334 private void stubCommit() {
335 when(titanDao.commit()).thenReturn(TitanOperationStatus.OK);
338 private void stubRollback() {
339 when(titanDao.rollback()).thenReturn(TitanOperationStatus.OK);
342 private void stubUnlockAndCommit() {
343 when(graphLockOperation.unlockComponent(eq(COMPONENT_ID), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
347 private void stubValidateAndLockSuccess(String methodName) {
348 stubValidationSuccess(methodName);
349 when(graphLockOperation.lockComponent(eq(COMPONENT_ID), any(NodeTypeEnum.class))).thenReturn(StorageOperationStatus.OK);
352 private void stubValidationSuccess(String methodName) {
353 when(userValidations.validateUserExists(eq(USER_ID), eq(methodName), eq(false))).thenReturn(user);
354 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID), any(ComponentParametersView.class))).thenReturn(componentSuccessEither);
357 private void stubGetToscaFullElementSuccess() {
358 when(toscaOperationFacade.getToscaFullElement(eq(COMPONENT_ID))).thenReturn(buildElementEither());
361 private void stubGetToscaElementSuccess() {
362 when(toscaOperationFacade.getToscaElement(eq(COMPONENT_ID))).thenReturn(componentSuccessEither);
365 private Either<Component, StorageOperationStatus> buildElementEither() {
366 ResourceBuilder builder = new ResourceBuilder();
367 GroupDefinition groupDefinition = GroupDefinitionBuilder.create().setUniqueId(UNIQUE_ID_EXSISTS).build();
368 ComponentInstanceBuilder componentInstanceBuilder = new ComponentInstanceBuilder();
369 ComponentInstance componentInstance = componentInstanceBuilder.setUniqueId(UNIQUE_ID_EXSISTS).build();
370 return Either.left(builder.addGroup(groupDefinition).addComponentInstance(componentInstance).build());
373 private Map<PolicyTargetType, List<String>> getTargets() {
374 Map<PolicyTargetType, List<String>> targets = new HashMap<>();
375 targets.put(PolicyTargetType.COMPONENT_INSTANCES, Collections.singletonList(UNIQUE_ID_EXSISTS));
376 targets.put(PolicyTargetType.GROUPS, Collections.singletonList(UNIQUE_ID_EXSISTS));
382 private static PolicyTypeDefinition buildPolicyType() {
383 PolicyTypeDefinition policyType = new PolicyTypeDefinition();
384 policyType.setType(POLICY_TYPE_NAME);
388 private static PolicyDefinition buildPolicy(String policyName) {
389 PolicyDefinition policy = new PolicyDefinition();
390 policy.setUniqueId(POLICY_ID);
391 policy.setPolicyTypeName(POLICY_TYPE_NAME);
392 policy.setComponentName(COMPONENT_NAME);
393 policy.setName(policyName);
397 private static Resource buildResource() {
398 Resource resource = new Resource();
399 resource.setUniqueId(COMPONENT_ID);
400 resource.setName(COMPONENT_NAME);
401 resource.setCreatorUserId(USER_ID);
402 resource.setLastUpdaterUserId(USER_ID);
403 resource.setState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
404 resource.setIsDeleted(false);
405 resource.setResourceType(ResourceTypeEnum.VF);
406 resource.setToscaType(ToscaElementTypeEnum.TOPOLOGY_TEMPLATE.getValue());
407 resource.setComponentType(ComponentTypeEnum.RESOURCE);
408 Map<String, PolicyDefinition> policies = new HashMap<>();
409 policies.put(POLICY_ID, policy);
410 resource.setPolicies(policies);
414 private static User buildUser() {
415 User user = new User();
416 user.setUserId(USER_ID);
420 private Map<PolicyTargetType, List<String>> getTargetListFakeType() {
421 Map<PolicyTargetType, List<String>> targets = new HashMap<>();
422 targets.put(PolicyTargetType.TYPE_DOES_NOT_EXIST, Collections.singletonList(UNIQUE_ID_EXSISTS));
426 private Map<PolicyTargetType, List<String>> getTargetListFakeId() {
427 Map<PolicyTargetType, List<String>> targets = new HashMap<>();
428 targets.put(PolicyTargetType.COMPONENT_INSTANCES, Collections.singletonList(UNIQUE_ID_DOESNT_EXSISTS));
429 targets.put(PolicyTargetType.GROUPS, Collections.singletonList(UNIQUE_ID_DOESNT_EXSISTS));