Initial OpenECOMP SDC commit
[sdc.git] / catalog-be / src / test / java / org / openecomp / sdc / be / components / lifecycle / CheckinTest.java
1 /*-
2  * ============LICENSE_START=======================================================
3  * SDC
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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=========================================================
19  */
20
21 package org.openecomp.sdc.be.components.lifecycle;
22
23 import static org.junit.Assert.assertEquals;
24 import static org.junit.Assert.assertTrue;
25
26 import org.junit.Before;
27 import org.junit.Test;
28 import org.openecomp.sdc.be.components.lifecycle.CheckinTransition;
29 import org.openecomp.sdc.be.dao.api.ActionStatus;
30 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
31 import org.openecomp.sdc.be.impl.ComponentsUtils;
32 import org.openecomp.sdc.be.model.LifecycleStateEnum;
33 import org.openecomp.sdc.be.model.Resource;
34 import org.openecomp.sdc.be.model.Service;
35 import org.openecomp.sdc.be.model.User;
36 import org.openecomp.sdc.be.user.Role;
37 import org.openecomp.sdc.exception.ResponseFormat;
38
39 import fj.data.Either;
40
41 public class CheckinTest extends LifecycleTestBase {
42
43         private CheckinTransition checkinObj = null;
44         private ComponentsUtils componentsUtils = new ComponentsUtils();
45
46         @Before
47         public void setup() {
48
49                 super.setup();
50
51                 // checkout transition object
52                 checkinObj = new CheckinTransition(componentsUtils, lcOperation);
53                 checkinObj.setLifeCycleOperation(lcOperation);
54                 checkinObj.setConfigurationManager(configurationManager);
55                 componentsUtils.Init();
56         }
57
58         @Test
59         public void testSimpleCheckin() {
60                 Either<Boolean, ResponseFormat> changeStateResult;
61                 Resource resource = createResourceObject(false);
62
63                 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
64                 Either<User, ResponseFormat> ownerResponse = checkinObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
65                 assertTrue(ownerResponse.isLeft());
66                 User owner = ownerResponse.left().value();
67                 changeStateResult = checkinObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, user, owner, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
68                 assertEquals(changeStateResult.isLeft(), true);
69
70         }
71
72         @Test
73         public void testSimpleServiceCheckin() {
74                 Either<Boolean, ResponseFormat> changeStateResult;
75                 Service service = createServiceObject(false);
76
77                 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
78                 Either<User, ResponseFormat> ownerResponse = checkinObj.getComponentOwner(service, ComponentTypeEnum.SERVICE);
79                 assertTrue(ownerResponse.isLeft());
80                 User owner = ownerResponse.left().value();
81                 changeStateResult = checkinObj.validateBeforeTransition(service, ComponentTypeEnum.SERVICE, user, owner, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
82                 assertEquals(changeStateResult.isLeft(), true);
83
84         }
85
86         @Test
87         public void testCheckinTwiceValidation() {
88                 Either<Resource, ResponseFormat> changeStateResult;
89                 Resource resource = createResourceObject(false);
90
91                 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
92                 Either<User, ResponseFormat> owner = checkinObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
93                 assertTrue(owner.isLeft());
94                 // changeStateResult = checkinObj.changeStateOperation(resource, user,
95                 // owner.left().value());
96                 Either<Boolean, ResponseFormat> validateBeforeTransition = checkinObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, user, owner.left().value(), LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
97                 assertEquals(validateBeforeTransition.isRight(), true);
98                 changeStateResult = Either.right(validateBeforeTransition.right().value());
99
100                 assertResponse(changeStateResult, ActionStatus.COMPONENT_ALREADY_CHECKED_IN, resource.getName(), ComponentTypeEnum.RESOURCE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());
101
102         }
103
104         @Test
105         public void testServiceCheckinTwiceValidation() {
106                 Either<Service, ResponseFormat> changeStateResult;
107                 Service service = createServiceObject(false);
108
109                 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
110                 Either<User, ResponseFormat> owner = checkinObj.getComponentOwner(service, ComponentTypeEnum.SERVICE);
111                 assertTrue(owner.isLeft());
112
113                 Either<Boolean, ResponseFormat> validateBeforeTransition = checkinObj.validateBeforeTransition(service, ComponentTypeEnum.SERVICE, user, owner.left().value(), LifecycleStateEnum.NOT_CERTIFIED_CHECKIN);
114                 assertEquals(validateBeforeTransition.isRight(), true);
115                 changeStateResult = Either.right(validateBeforeTransition.right().value());
116
117                 assertServiceResponse(changeStateResult, ActionStatus.COMPONENT_ALREADY_CHECKED_IN, service.getName(), ComponentTypeEnum.SERVICE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());
118
119         }
120
121         @Test
122         public void testCheckoutByAnotherUserValidation() {
123                 Either<Resource, ResponseFormat> changeStateResult;
124                 Resource resource = createResourceObject(false);
125
126                 User modifier = new User();
127                 modifier.setUserId("modifier");
128                 modifier.setFirstName("Albert");
129                 modifier.setLastName("Einstein");
130                 modifier.setRole(Role.DESIGNER.name());
131
132                 resource.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
133                 Either<User, ResponseFormat> ownerResponse = checkinObj.getComponentOwner(resource, ComponentTypeEnum.RESOURCE);
134                 assertTrue(ownerResponse.isLeft());
135                 User owner = ownerResponse.left().value();
136                 // changeStateResult = checkinObj.changeStateOperation(resource,
137                 // modifier, owner);
138                 Either<Boolean, ResponseFormat> validateBeforeTransition = checkinObj.validateBeforeTransition(resource, ComponentTypeEnum.RESOURCE, modifier, owner, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
139                 assertEquals(validateBeforeTransition.isRight(), true);
140                 changeStateResult = Either.right(validateBeforeTransition.right().value());
141                 assertEquals(changeStateResult.isRight(), true);
142
143                 assertResponse(changeStateResult, ActionStatus.COMPONENT_CHECKOUT_BY_ANOTHER_USER, resource.getName(), ComponentTypeEnum.RESOURCE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());
144
145         }
146
147         @Test
148         public void testServiceCheckoutByAnotherUserValidation() {
149                 Either<Service, ResponseFormat> changeStateResult;
150                 Service service = createServiceObject(false);
151
152                 User modifier = new User();
153                 modifier.setUserId("modifier");
154                 modifier.setFirstName("Albert");
155                 modifier.setLastName("Einstein");
156                 modifier.setRole(Role.DESIGNER.name());
157
158                 service.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
159                 Either<User, ResponseFormat> ownerResponse = checkinObj.getComponentOwner(service, ComponentTypeEnum.SERVICE);
160                 assertTrue(ownerResponse.isLeft());
161                 User owner = ownerResponse.left().value();
162                 Either<Boolean, ResponseFormat> validateBeforeTransition = checkinObj.validateBeforeTransition(service, ComponentTypeEnum.RESOURCE, modifier, owner, LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
163                 assertEquals(validateBeforeTransition.isRight(), true);
164                 changeStateResult = Either.right(validateBeforeTransition.right().value());
165                 assertEquals(changeStateResult.isRight(), true);
166
167                 assertServiceResponse(changeStateResult, ActionStatus.COMPONENT_CHECKOUT_BY_ANOTHER_USER, service.getName(), ComponentTypeEnum.RESOURCE.name().toLowerCase(), user.getFirstName(), user.getLastName(), user.getUserId());
168
169         }
170 }