[SDC-29] rebase continue work to align source
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / user / GovernorWorkspaceApiTest.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.user;
22
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertFalse;
25 import static org.testng.AssertJUnit.assertNotNull;
26 import static org.testng.AssertJUnit.assertTrue;
27
28 import java.io.IOException;
29 import java.util.List;
30
31 import org.json.simple.JSONArray;
32 import org.json.simple.JSONObject;
33 import org.junit.Rule;
34 import org.junit.rules.TestName;
35 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
36 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
37 import org.openecomp.sdc.be.model.DistributionStatusEnum;
38 import org.openecomp.sdc.be.model.Resource;
39 import org.openecomp.sdc.be.model.Service;
40 import org.openecomp.sdc.be.model.User;
41 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
42 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
43 import org.openecomp.sdc.ci.tests.datatypes.ComponentInstanceReqDetails;
44 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
45 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
46 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
47 import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
48 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
49 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
50 import org.openecomp.sdc.ci.tests.execute.lifecycle.LCSbaseTest;
51 import org.openecomp.sdc.ci.tests.utils.DbUtils;
52 import org.openecomp.sdc.ci.tests.utils.Utils;
53 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
54 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
55 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
56 import org.openecomp.sdc.ci.tests.utils.rest.ComponentInstanceRestUtils;
57 import org.openecomp.sdc.ci.tests.utils.rest.LifecycleRestUtils;
58 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
59 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
60 import org.openecomp.sdc.ci.tests.utils.validation.ServiceValidationUtils;
61 import org.slf4j.Logger;
62 import org.slf4j.LoggerFactory;
63 import org.testng.annotations.BeforeMethod;
64 import org.testng.annotations.Test;
65
66 public class GovernorWorkspaceApiTest extends ComponentBaseTest {
67
68         private static Logger logger = LoggerFactory.getLogger(GovernorWorkspaceApiTest.class.getName());
69         @Rule
70         public static TestName name = new TestName();
71
72         public GovernorWorkspaceApiTest() {
73                 super(name, GovernorWorkspaceApiTest.class.getName());
74
75         }
76
77         protected final User admin1 = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
78         protected final User governor = ElementFactory.getDefaultUser(UserRoleEnum.GOVERNOR);
79         protected final User sdncDesignerDetails1 = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
80         protected ResourceReqDetails resourceDetails1;
81         protected ComponentInstanceReqDetails componentInstanceReqDetails;
82         protected ArtifactReqDetails heatArtifactDetails;
83
84         protected final String serviceVersion = "0.1";
85         protected final String servicesString = "services";
86         protected final String userRemarks = "commentTest";
87
88         protected ServiceReqDetails serviceDetails11 = null;
89         protected ServiceReqDetails serviceDetails22 = null;
90         protected ServiceReqDetails serviceDetails33 = null;
91
92         @BeforeMethod
93         public void initBeforeTest() throws Exception {
94                 DbUtils.deleteFromEsDbByPattern("_all");
95                 Resource resourceObj = AtomicOperationUtils
96                                 .createResourceByType(ResourceTypeEnum.VFC, UserRoleEnum.DESIGNER, true).left().value();
97                 resourceDetails1 = new ResourceReqDetails(resourceObj);
98                 heatArtifactDetails = ElementFactory.getDefaultDeploymentArtifactForType(ArtifactTypeEnum.HEAT.getType());
99                 createThreeServices(sdncDesignerDetails1);
100         }
101
102         protected void createThreeServices(User user) throws Exception {
103
104                 String checkinComment = "good checkin";
105                 String checkinComentJson = "{\"userRemarks\": \"" + checkinComment + "\"}";
106
107                 RestResponse addInformationalArtifactToResource = ArtifactRestUtils.addInformationalArtifactToResource(
108                                 heatArtifactDetails, sdncDesignerDetails1, resourceDetails1.getUniqueId());
109                 RestResponse certifyResource = LifecycleRestUtils.certifyResource(resourceDetails1);
110                 componentInstanceReqDetails = ElementFactory.getDefaultComponentInstance("defaultInstance", resourceDetails1);
111
112                 serviceDetails11 = ElementFactory.getDefaultService();
113                 serviceDetails22 = ElementFactory.getDefaultService();
114                 serviceDetails33 = ElementFactory.getDefaultService();
115
116                 serviceDetails11.setName(serviceDetails11.getName() + "1");
117                 List<String> tags = serviceDetails11.getTags();
118                 tags.add(serviceDetails11.getName());
119                 serviceDetails11.setTags(tags);
120
121                 serviceDetails22.setName(serviceDetails11.getName() + "2");
122                 tags = serviceDetails22.getTags();
123                 tags.add(serviceDetails22.getName());
124                 serviceDetails22.setTags(tags);
125
126                 serviceDetails33.setName(serviceDetails11.getName() + "3");
127                 tags = serviceDetails33.getTags();
128                 tags.add(serviceDetails33.getName());
129                 serviceDetails33.setTags(tags);
130                 
131                 RestResponse createServiceResponse1 = createService(user, serviceDetails11);
132                 RestResponse createServiceResponse2 = createService(user, serviceDetails22);
133                 RestResponse createServiceResponse3 = createService(user, serviceDetails33);
134         }
135
136         protected RestResponse createService(User user, ServiceReqDetails serviceDetails) throws Exception, IOException {
137                 RestResponse createServiceResponse1 = ServiceRestUtils.createService(serviceDetails, user);
138                 assertNotNull("check response object is not null after creating service", createServiceResponse1);
139                 assertNotNull("check if error code exists in response after creating service",
140                                 createServiceResponse1.getErrorCode());
141                 assertEquals("Check response code after creating service", 201,
142                                 createServiceResponse1.getErrorCode().intValue());
143                 Service convertServiceResponseToJavaObject = ResponseParser
144                                 .convertServiceResponseToJavaObject(createServiceResponse1.getResponse());
145                 serviceDetails.setUniqueId(convertServiceResponseToJavaObject.getUniqueId());
146                 logger.debug("Created service1 ={}", serviceDetails);
147                 addResourceWithHeatArt(serviceDetails);
148                 return createServiceResponse1;
149         }
150
151         protected void addResourceWithHeatArt(ServiceReqDetails serviceDetails) throws Exception {
152
153                 RestResponse createResourceInstance = ComponentInstanceRestUtils.createComponentInstance(
154                                 componentInstanceReqDetails, sdncDesignerDetails1, serviceDetails.getUniqueId(),
155                                 ComponentTypeEnum.SERVICE);
156                 // System.out.println("serviceUID --->" + serviceDetails.getUniqueId());
157                 assertEquals("Check response code ", 201, createResourceInstance.getErrorCode().intValue());
158         }
159
160         protected void certifyAllServices() throws Exception {
161                 LifecycleRestUtils.certifyService(serviceDetails11);
162                 LifecycleRestUtils.certifyService(serviceDetails22);
163                 LifecycleRestUtils.certifyService(serviceDetails33);
164         }
165
166         protected boolean isElementInArray(String elementId, JSONArray jsonArray) throws Exception {
167                 for (int i = 0; i < jsonArray.size(); i++) {
168                         JSONObject jobject = (JSONObject) jsonArray.get(i);
169
170                         if (jobject.get("uniqueId").toString().equals(elementId)) {
171                                 return true;
172                         }
173                 }
174                 return false;
175         }
176
177         protected void approveDistributionStatusOfCertifiedService(ServiceReqDetails serviceDetails, User user)
178                         throws Exception {
179                 approveDistributionStatusOfService(serviceDetails, user, "1.0");
180         }
181
182         protected void approveDistributionStatusOfService(ServiceReqDetails serviceDetails, User user, String version)
183                         throws Exception {
184                 RestResponse res = LifecycleRestUtils.sendApproveDistribution(user, serviceDetails.getUniqueId(), userRemarks);
185                 assertEquals(200, res.getErrorCode().intValue());
186                 ServiceValidationUtils.validateDistrubtionStatusValue(res, DistributionStatusEnum.DISTRIBUTION_APPROVED);
187         }
188
189         protected void rejectDistributionStatusOfService(ServiceReqDetails serviceDetails, User user) throws Exception {
190                 rejectDistributionStatusOfService(serviceDetails, user, "1.0");
191         }
192
193         protected void rejectDistributionStatusOfService(ServiceReqDetails serviceDetails, User user, String version)
194                         throws Exception {
195                 RestResponse res = LifecycleRestUtils.rejectDistribution(serviceDetails, version, user, userRemarks);
196                 assertEquals(200, res.getErrorCode().intValue());
197                 ServiceValidationUtils.validateDistrubtionStatusValue(res, DistributionStatusEnum.DISTRIBUTION_REJECTED);
198         }
199
200         protected JSONArray getFollowedListAsJsonArray(User user) throws Exception {
201                 RestResponse getGovernorFollowed = ServiceRestUtils.getFollowed(user);
202                 assertNotNull(getGovernorFollowed);
203                 assertNotNull(getGovernorFollowed.getErrorCode());
204                 assertEquals(200, getGovernorFollowed.getErrorCode().intValue());
205
206                 JSONArray listArrayFromRestResponse = ServiceRestUtils.getListArrayFromRestResponse(getGovernorFollowed);
207
208                 return listArrayFromRestResponse;
209         }
210
211         protected void changeDistributionStatusOfAllService(boolean approved, User user) throws Exception {
212                 if (approved) {
213                         approveDistributionStatusOfCertifiedService(serviceDetails11, user);
214                         approveDistributionStatusOfCertifiedService(serviceDetails22, user);
215                         approveDistributionStatusOfCertifiedService(serviceDetails33, user);
216                 } else {
217                         rejectDistributionStatusOfService(serviceDetails11, user);
218                         rejectDistributionStatusOfService(serviceDetails22, user);
219                         rejectDistributionStatusOfService(serviceDetails33, user);
220                 }
221
222         }
223
224         protected JSONArray checkFollowed(User user) throws Exception {
225                 JSONArray getFollowedList = getFollowedListAsJsonArray(user);
226                 assertFalse(getFollowedList.isEmpty());
227                 assertTrue(isElementInArray(serviceDetails11.getUniqueId(), getFollowedList));
228                 assertTrue(isElementInArray(serviceDetails22.getUniqueId(), getFollowedList));
229                 assertTrue(isElementInArray(serviceDetails33.getUniqueId(), getFollowedList));
230
231                 return getFollowedList;
232         }
233
234         // -------------------------------------T E S T
235         // S------------------------------------------------------//
236
237         @Test
238         public void governorList_AllCertifiedVersionsOfService() throws Exception {
239                 certifyAllServices();
240                 String serviceUniqueIdCertified1 = serviceDetails11.getUniqueId();
241                 RestResponse res = LifecycleRestUtils.changeServiceState(serviceDetails11, sdncDesignerDetails1, "1.0",
242                                 LifeCycleStatesEnum.CHECKOUT);
243                 assertEquals(200, res.getErrorCode().intValue());
244
245                 JSONArray getFollowedList = getFollowedListAsJsonArray(governor);
246                 assertFalse(getFollowedList.isEmpty());
247                 assertFalse(isElementInArray(serviceDetails11.getUniqueId(), getFollowedList));
248                 assertTrue(isElementInArray(serviceDetails22.getUniqueId(), getFollowedList));
249                 assertTrue(isElementInArray(serviceDetails33.getUniqueId(), getFollowedList));
250                 assertTrue(isElementInArray(serviceUniqueIdCertified1, getFollowedList));
251                 assertEquals(3, getFollowedList.size());
252
253                 // certifyService(serviceDetails11, "1.1");
254                 LifecycleRestUtils.certifyService(serviceDetails11);
255
256                 JSONArray governorFollowedList2 = checkFollowed(governor);
257                 assertEquals(4, governorFollowedList2.size());
258                 assertTrue(isElementInArray(serviceDetails11.getUniqueId(), governorFollowedList2));
259                 assertTrue(isElementInArray(serviceUniqueIdCertified1, governorFollowedList2));
260
261         }
262
263         // -------------------------------------T E S T
264         // S------------------------------------------------------//
265
266         @Test
267         public void governorList_distributionNotApproved() throws Exception {
268                 certifyAllServices();
269
270                 JSONArray checkFollowed = checkFollowed(governor);
271                 assertEquals(3, checkFollowed.size());
272         }
273
274         @Test
275         public void governorGetEmptyListTest_notCertifiedServices() throws Exception {
276                 JSONArray governorFollowedList = getFollowedListAsJsonArray(governor);
277
278                 assertTrue(governorFollowedList.isEmpty());
279         }
280
281         @Test
282         public void governorList_distributionApproved() throws Exception {
283                 certifyAllServices();
284                 boolean approved = true;
285                 changeDistributionStatusOfAllService(approved, governor);
286
287                 JSONArray checkFollowed = checkFollowed(governor);
288                 assertEquals(3, checkFollowed.size());
289         }
290
291         @Test(enabled = false)
292         public void governorList_distributed() throws Exception {
293                 certifyAllServices();
294
295                 LifecycleRestUtils.changeDistributionStatus(serviceDetails11, "1.0", governor, userRemarks,
296                                 DistributionStatusEnum.DISTRIBUTED);
297                 LifecycleRestUtils.changeDistributionStatus(serviceDetails22, "1.0", governor, userRemarks,
298                                 DistributionStatusEnum.DISTRIBUTED);
299                 LifecycleRestUtils.changeDistributionStatus(serviceDetails33, "1.0", governor, userRemarks,
300                                 DistributionStatusEnum.DISTRIBUTED);
301
302                 JSONArray governorFollowedList = getFollowedListAsJsonArray(governor);
303                 assertFalse(governorFollowedList.isEmpty());
304                 assertTrue(isElementInArray(serviceDetails11.getUniqueId(), governorFollowedList));
305                 assertTrue(isElementInArray(serviceDetails22.getUniqueId(), governorFollowedList));
306                 assertTrue(isElementInArray(serviceDetails33.getUniqueId(), governorFollowedList));
307         }
308
309         @Test
310         public void governorList_distributionRejected() throws Exception {
311                 certifyAllServices();
312                 boolean distributionRejected = false;
313                 changeDistributionStatusOfAllService(distributionRejected, governor);
314
315                 JSONArray checkFollowed = checkFollowed(governor);
316                 assertEquals(3, checkFollowed.size());
317         }
318
319 }