re base code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / product / ProductCheckoutTest.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.ci.tests.execute.product;
22
23 import org.junit.Rule;
24 import org.junit.rules.TestName;
25 import org.openecomp.sdc.be.dao.api.ActionStatus;
26 import org.openecomp.sdc.be.model.LifecycleStateEnum;
27 import org.openecomp.sdc.be.model.Product;
28 import org.openecomp.sdc.be.model.User;
29 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
30 import org.openecomp.sdc.ci.tests.datatypes.expected.ExpectedProductAudit;
31 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
32 import org.openecomp.sdc.ci.tests.utils.DbUtils;
33 import org.openecomp.sdc.ci.tests.utils.general.Convertor;
34 import org.openecomp.sdc.ci.tests.utils.rest.LifecycleRestUtils;
35 import org.openecomp.sdc.ci.tests.utils.rest.ProductRestUtils;
36 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
37 import org.openecomp.sdc.ci.tests.utils.validation.AuditValidationUtils;
38 import org.openecomp.sdc.ci.tests.utils.validation.ProductValidationUtils;
39 import org.testng.annotations.BeforeClass;
40 import org.testng.annotations.Test;
41
42 import java.io.FileNotFoundException;
43
44 import static org.testng.AssertJUnit.assertEquals;
45
46 public class ProductCheckoutTest extends ProductLifecycleTest {
47
48         @Rule
49         public static TestName name = new TestName();
50
51         @BeforeClass
52         public static void staticInit() {
53                 auditAction = CHECKOUT_ACTION;
54                 operation = ComponentOperationEnum.CHANGE_STATE_CHECKOUT;
55         }
56
57         public ProductCheckoutTest() {
58                 super(name, ProductCheckoutTest.class.getName());
59         }
60
61         @Test
62         public void checkOutProductByPmNotInContacts() throws Exception {
63                 checkOutProductSuccess(productManager2);
64         }
65
66         @Test
67         public void checkOutProductByPmInContacts() throws Exception {
68                 checkOutProductSuccess(productManager1);
69         }
70
71         @Test
72         public void checkOutProductByAdmin() throws Exception {
73                 checkOutProductSuccess(adminUser);
74         }
75
76         @Test
77         public void checkOutProductByPs() throws Exception {
78                 // Changed in 1604 patch - now it's restricted
79                 checkOutProductRestricted(productStrategistUser3);
80                 // checkOutProductSuccess(productStrategistUser3);
81         }
82
83         @Test
84         public void checkOutProductByDesignerRoleNotAllowed() throws Exception {
85                 checkOutProductRestricted(designerUser);
86         }
87
88         @Test
89         public void checkOutProductAlreadyCheckedOut() throws Exception {
90                 RestResponse lcsResponse = LifecycleRestUtils.changeProductState(expectedProduct, productManager1, LifeCycleStatesEnum.CHECKIN);
91                 assertEquals("Check response code after checkin resource", 200, lcsResponse.getErrorCode().intValue());
92
93                 lcsResponse = LifecycleRestUtils.changeProductState(expectedProduct, productManager1, LifeCycleStatesEnum.CHECKOUT);
94                 assertEquals("Check response code after checkin resource", 200, lcsResponse.getErrorCode().intValue());
95                 Product checkedOutProduct = ResponseParser.parseToObjectUsingMapper(lcsResponse.getResponse(), Product.class);
96
97                 DbUtils.cleanAllAudits();
98
99                 lcsResponse = LifecycleRestUtils.changeProductState(checkedOutProduct, productManager2, LifeCycleStatesEnum.CHECKOUT);
100                 assertEquals("Check response code after checkin resource", 403, lcsResponse.getErrorCode().intValue());
101                 String[] auditParameters = new String[] { checkedOutProduct.getName(), "product", productManager1.getFirstName(), productManager1.getLastName(), productManager1.getUserId() };
102
103                 ExpectedProductAudit expectedProductAudit = Convertor.constructFieldsForAuditValidation(checkedOutProduct, auditAction, productManager2, ActionStatus.COMPONENT_IN_CHECKOUT_STATE, "0.2", "0.2", LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT,
104                                 LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT, checkedOutProduct.getUUID(), auditParameters);
105                 AuditValidationUtils.validateAuditProduct(expectedProductAudit, auditAction);
106         }
107
108         private void checkOutProductSuccess(User checkoutUser) throws Exception, FileNotFoundException {
109                 RestResponse lcsResponse = LifecycleRestUtils.changeProductState(expectedProduct, productManager1, LifeCycleStatesEnum.CHECKIN);
110                 assertEquals("Check response code after checkin resource", 200, lcsResponse.getErrorCode().intValue());
111
112                 lcsResponse = LifecycleRestUtils.changeProductState(expectedProduct, checkoutUser, LifeCycleStatesEnum.CHECKOUT);
113                 assertEquals("Check response code after checkin resource", 200, lcsResponse.getErrorCode().intValue());
114
115                 // 0.1 is not highest now
116                 RestResponse prevVersionProductResp = ProductRestUtils.getProduct(expectedProduct.getUniqueId(), productStrategistUser1.getUserId());
117                 Product prevVersionProduct = ResponseParser.parseToObjectUsingMapper(prevVersionProductResp.getResponse(), Product.class);
118                 Boolean falseParam = false;
119                 assertEquals(falseParam, prevVersionProduct.isHighestVersion());
120
121                 Product checkedOutProduct = ResponseParser.parseToObjectUsingMapper(lcsResponse.getResponse(), Product.class);
122
123                 expectedProduct.setLifecycleState(LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT);
124                 expectedProduct.setVersion("0.2");
125                 expectedProduct.setLastUpdaterUserId(checkoutUser.getUserId());
126                 expectedProduct.setLastUpdaterFullName(checkoutUser.getFullName());
127                 ProductValidationUtils.compareExpectedAndActualProducts(expectedProduct, checkedOutProduct, operation);
128
129                 ExpectedProductAudit expectedProductAudit = Convertor.constructFieldsForAuditValidation(checkedOutProduct, auditAction, checkoutUser, ActionStatus.OK, "0.1", "0.2", LifecycleStateEnum.NOT_CERTIFIED_CHECKIN,
130                                 LifecycleStateEnum.NOT_CERTIFIED_CHECKOUT, checkedOutProduct.getUUID());
131                 AuditValidationUtils.validateAuditProduct(expectedProductAudit, auditAction);
132         }
133
134         private void checkOutProductRestricted(User checkoutUser) throws Exception, FileNotFoundException {
135                 RestResponse lcsResponse = LifecycleRestUtils.changeProductState(expectedProduct, productManager1, LifeCycleStatesEnum.CHECKIN);
136                 assertEquals("Check response code after checkin resource", 200, lcsResponse.getErrorCode().intValue());
137
138                 lcsResponse = LifecycleRestUtils.changeProductState(expectedProduct, checkoutUser, LifeCycleStatesEnum.CHECKOUT);
139                 assertEquals("Check response code after checkin resource", 409, lcsResponse.getErrorCode().intValue());
140
141                 ExpectedProductAudit expectedProductAudit = Convertor.constructFieldsForAuditValidation(expectedProduct, auditAction, checkoutUser, ActionStatus.RESTRICTED_OPERATION, "0.1", "0.1", LifecycleStateEnum.NOT_CERTIFIED_CHECKIN,
142                                 LifecycleStateEnum.NOT_CERTIFIED_CHECKIN, expectedProduct.getUUID());
143                 AuditValidationUtils.validateAuditProduct(expectedProductAudit, auditAction);
144         }
145
146 }