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,
53 checkoutObj.setLifeCycleOperation(toscaElementLifecycleOperation);
54 checkoutObj.setConfigurationManager(configurationManager);
55 bl.setToscaOperationFacade(toscaOperationFacade);
56 bl.setComponentsUtils(componentsUtils);
61 public void testCheckoutStateValidation() {
62 Either<? extends Component, ResponseFormat> changeStateResult;
63 Resource resource = createResourceObject();
65 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
66 Either<User, ResponseFormat> ownerResponse = checkoutObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
67 assertTrue(ownerResponse.isLeft());
68 User owner = ownerResponse.left().value();
69 changeStateResult = checkoutObj.changeState(ComponentTypeEnum.RESOURCE, resource, bl, user, owner, false, false);
70 assertTrue(changeStateResult.isLeft());
72 resource.setLifecycleState(LifecycleStateEnum.CERTIFIED);
73 changeStateResult = checkoutObj.changeState(ComponentTypeEnum.RESOURCE, resource, bl, user, owner, false, false);
74 assertTrue(changeStateResult.isLeft());
79 public void testAlreadyCheckout() {
80 Either<Resource, ResponseFormat> changeStateResult;
81 Resource resource = createResourceObject();
83 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
84 Either<User, ResponseFormat> ownerResponse = checkoutObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
85 assertTrue(ownerResponse.isLeft());
86 User owner = ownerResponse.left().value();
87 Either<Boolean, ResponseFormat> validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, user, owner, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
88 assertTrue(validateBeforeTransition.isRight());
89 changeStateResult = Either.right(validateBeforeTransition.right().value());
91 assertTrue(changeStateResult.isRight());
92 assertResponse(changeStateResult, ActionStatus.COMPONENT_IN_CHECKOUT_STATE, resource.getName(), ComponentTypeEnum.RESOURCE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());
97 public void testCertificationInProgress() {
98 Either<? extends Component, ResponseFormat> changeStateResult;
99 Resource resource = createResourceObject();
101 resource.setLifecycleState(LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
102 Either<User, ResponseFormat> ownerResponse = checkoutObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
103 assertTrue(ownerResponse.isLeft());
104 User owner = ownerResponse.left().value();
105 changeStateResult = checkoutObj.changeState(ComponentTypeEnum.RESOURCE, resource, bl, user, owner, false, false);
107 Either<Boolean, ResponseFormat> validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, user, owner, LifecycleStateEnum.CERTIFICATION_IN_PROGRESS);
108 assertTrue(validateBeforeTransition.isRight());
109 changeStateResult = Either.right(validateBeforeTransition.right().value());
110 assertTrue(changeStateResult.isRight());
112 assertResponse(changeStateResult, ActionStatus.COMPONENT_IN_CERT_IN_PROGRESS_STATE, resource.getName(), ComponentTypeEnum.RESOURCE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());
117 public void testReadyForCertification() {
118 Either<Resource, ResponseFormat> changeStateResult;
119 Resource resource = createResourceObject();
121 resource.setLifecycleState(LifecycleStateEnum.READY_FOR_CERTIFICATION);
123 // if modifier = owner
124 Either<User, ResponseFormat> ownerResponse = checkoutObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
125 assertTrue(ownerResponse.isLeft());
126 User owner = ownerResponse.left().value();
127 Either<Boolean, ResponseFormat> validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, user, owner, LifecycleStateEnum.READY_FOR_CERTIFICATION);
128 assertTrue(validateBeforeTransition.isLeft());
131 User modifier = new User();
132 modifier.setUserId("modifier");
133 modifier.setFirstName("Albert");
134 modifier.setLastName("Einstein");
137 modifier.setRole(Role.ADMIN.name());
138 validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, modifier, owner, LifecycleStateEnum.READY_FOR_CERTIFICATION);
139 assertTrue(validateBeforeTransition.isLeft());
142 modifier.setRole(Role.TESTER.name());
143 validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, modifier, owner, LifecycleStateEnum.READY_FOR_CERTIFICATION);
144 assertTrue(validateBeforeTransition.isRight());
145 changeStateResult = Either.right(validateBeforeTransition.right().value());
147 assertTrue(changeStateResult.isRight());
148 assertResponse(changeStateResult, ActionStatus.RESTRICTED_OPERATION, resource.getName(), ComponentTypeEnum.RESOURCE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());
153 public void testRoles() {
154 Either<Resource, ResponseFormat> changeStateResult;
155 Resource resource = createResourceObject();
157 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
159 User modifier = new User();
160 modifier.setUserId("modifier");
161 modifier.setFirstName("Albert");
162 modifier.setLastName("Einstein");
163 modifier.setRole(Role.DESIGNER.name());
164 Either<User, ResponseFormat> ownerResponse = checkoutObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
165 assertTrue(ownerResponse.isLeft());
166 User owner = ownerResponse.left().value();
167 Either<Boolean, ResponseFormat> validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, modifier, owner, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
168 assertTrue(validateBeforeTransition.isLeft());
170 modifier.setRole(Role.TESTER.name());
171 validateBeforeTransition = checkoutObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, modifier, owner, LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
172 assertTrue(validateBeforeTransition.isRight());
173 changeStateResult = Either.right(validateBeforeTransition.right().value());
174 assertResponse(changeStateResult, ActionStatus.RESTRICTED_OPERATION);