2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
21 package org.openecomp.sdc.be.components.lifecycle;
23 import fj.data.Either;
24 import org.junit.Before;
25 import org.junit.Test;
26 import org.mockito.InjectMocks;
27 import org.openecomp.sdc.be.components.impl.ResourceBusinessLogic;
28 import org.openecomp.sdc.be.dao.api.ActionStatus;
29 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
30 import org.openecomp.sdc.be.model.Component;
31 import org.openecomp.sdc.be.model.LifecycleStateEnum;
32 import org.openecomp.sdc.be.model.Resource;
33 import org.openecomp.sdc.be.model.User;
34 import org.openecomp.sdc.be.user.Role;
35 import org.openecomp.sdc.exception.ResponseFormat;
37 import static org.junit.Assert.assertEquals;
38 import static org.junit.Assert.assertTrue;
40 public class CheckoutTest extends LifecycleTestBase {
42 private CheckoutTransition checkoutObj = null;
44 ResourceBusinessLogic bl = new ResourceBusinessLogic();
50 // checkout transition object
51 checkoutObj = new CheckoutTransition(componentsUtils, toscaElementLifecycleOperation, toscaOperationFacade, titanDao);
52 checkoutObj.setLifeCycleOperation(toscaElementLifecycleOperation);
53 checkoutObj.setConfigurationManager(configurationManager);
54 bl.setToscaOperationFacade(toscaOperationFacade);
55 bl.setComponentsUtils(componentsUtils);
60 public void testCheckoutStateValidation() {
61 Either<? extends Component, ResponseFormat> changeStateResult;
62 Resource resource = createResourceObject();
64 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
65 Either<User, ResponseFormat> ownerResponse = checkoutObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
66 assertTrue(ownerResponse.isLeft());
67 User owner = ownerResponse.left().value();
68 changeStateResult = checkoutObj.changeState(ComponentTypeEnum.RESOURCE, resource, bl, user, owner, false, false);
69 assertTrue(changeStateResult.isLeft());
71 resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
72 changeStateResult = checkoutObj.changeState(ComponentTypeEnum.RESOURCE, resource, bl, user, owner, false, false);
73 assertTrue(changeStateResult.isLeft());
78 public void testAlreadyCheckout() {
79 Either<Resource, ResponseFormat> changeStateResult;
80 Resource resource = createResourceObject();
82 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
83 Either<User, ResponseFormat> ownerResponse = checkoutObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
84 assertTrue(ownerResponse.isLeft());
85 User owner = ownerResponse.left().value();
86 Either<Boolean, ResponseFormat> validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, user, owner, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
87 assertTrue(validateBeforeTransition.isRight());
88 changeStateResult = Either.right(validateBeforeTransition.right().value());
90 assertTrue(changeStateResult.isRight());
91 assertResponse(changeStateResult, ActionStatus.COMPONENT_IN_CHECKOUT_STATE, resource.getName(), ComponentTypeEnum.RESOURCE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());
96 public void testCertificationInProgress() {
97 Either<? extends Component, ResponseFormat> changeStateResult;
98 Resource resource = createResourceObject();
100 resource.setLifecycleState(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
101 Either<User, ResponseFormat> ownerResponse = checkoutObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
102 assertTrue(ownerResponse.isLeft());
103 User owner = ownerResponse.left().value();
104 changeStateResult = checkoutObj.changeState(ComponentTypeEnum.RESOURCE, resource, bl, user, owner, false, false);
106 Either<Boolean, ResponseFormat> validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, user, owner, LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
107 assertTrue(validateBeforeTransition.isRight());
108 changeStateResult = Either.right(validateBeforeTransition.right().value());
109 assertTrue(changeStateResult.isRight());
111 assertResponse(changeStateResult, ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, resource.getName(), ComponentTypeEnum.RESOURCE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());
116 public void testReadyForCertification() {
117 Either<Resource, ResponseFormat> changeStateResult;
118 Resource resource = createResourceObject();
120 resource.setLifecycleState(LifecycleStateEnum.READY_FOR_CERTIFICATION);
122 // if modifier = owner
123 Either<User, ResponseFormat> ownerResponse = checkoutObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
124 assertTrue(ownerResponse.isLeft());
125 User owner = ownerResponse.left().value();
126 Either<Boolean, ResponseFormat> validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, user, owner, LifecycleStateEnum.READY_FOR_CERTIFICATION);
127 assertTrue(validateBeforeTransition.isLeft());
130 User modifier = new User();
131 modifier.setUserId("modifier");
132 modifier.setFirstName("Albert");
133 modifier.setLastName("Einstein");
136 modifier.setRole(Role.ADMIN.name());
137 validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, modifier, owner, LifecycleStateEnum.READY_FOR_CERTIFICATION);
138 assertTrue(validateBeforeTransition.isLeft());
141 modifier.setRole(Role.TESTER.name());
142 validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, modifier, owner, LifecycleStateEnum.READY_FOR_CERTIFICATION);
143 assertTrue(validateBeforeTransition.isRight());
144 changeStateResult = Either.right(validateBeforeTransition.right().value());
146 assertTrue(changeStateResult.isRight());
147 assertResponse(changeStateResult, ActionStatus.RESTRICTED_OPERATION, resource.getName(), ComponentTypeEnum.RESOURCE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());
152 public void testRoles() {
153 Either<Resource, ResponseFormat> changeStateResult;
154 Resource resource = createResourceObject();
156 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
158 User modifier = new User();
159 modifier.setUserId("modifier");
160 modifier.setFirstName("Albert");
161 modifier.setLastName("Einstein");
162 modifier.setRole(Role.DESIGNER.name());
163 Either<User, ResponseFormat> ownerResponse = checkoutObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
164 assertTrue(ownerResponse.isLeft());
165 User owner = ownerResponse.left().value();
166 Either<Boolean, ResponseFormat> validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, modifier, owner, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
167 assertTrue(validateBeforeTransition.isLeft());
169 modifier.setRole(Role.TESTER.name());
170 validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, modifier, owner, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
171 assertTrue(validateBeforeTransition.isRight());
172 changeStateResult = Either.right(validateBeforeTransition.right().value());
173 assertResponse(changeStateResult, ActionStatus.RESTRICTED_OPERATION);