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