aboutsummaryrefslogtreecommitdiffstats
path: root/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/execute/user/GovernorWorkspaceApiTest.java
blob: 5a56af5d4c5c5caf8b1d6979a3fdffa932fa4d10 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.ci.tests.execute.user;

import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
import org.openecomp.sdc.be.model.DistributionStatusEnum;
import org.openecomp.sdc.be.model.Resource;
import org.openecomp.sdc.be.model.Service;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.ComponentInstanceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
import org.openecomp.sdc.ci.tests.datatypes.enums.LifeCycleStatesEnum;
import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.utils.DbUtils;
import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
import org.openecomp.sdc.ci.tests.utils.rest.*;
import org.openecomp.sdc.ci.tests.utils.validation.ServiceValidationUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.testng.annotations.BeforeMethod;
import org.testng.annotations.Test;

import java.io.IOException;
import java.util.List;

import static org.testng.AssertJUnit.*;

public class GovernorWorkspaceApiTest extends ComponentBaseTest {

	private static Logger logger = LoggerFactory.getLogger(GovernorWorkspaceApiTest.class.getName());
	@Rule
	public static TestName name = new TestName();

	public GovernorWorkspaceApiTest() {
		super(name, GovernorWorkspaceApiTest.class.getName());

	}

	protected final User admin1 = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
	protected final User governor = ElementFactory.getDefaultUser(UserRoleEnum.GOVERNOR);
	protected final User sdncDesignerDetails1 = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
	protected ResourceReqDetails resourceDetails1;
	protected ComponentInstanceReqDetails componentInstanceReqDetails;
	protected ArtifactReqDetails heatArtifactDetails;

	protected final String serviceVersion = "0.1";
	protected final String servicesString = "services";
	protected final String userRemarks = "commentTest";

	protected ServiceReqDetails serviceDetails11 = null;
	protected ServiceReqDetails serviceDetails22 = null;
	protected ServiceReqDetails serviceDetails33 = null;

	@BeforeMethod
	public void initBeforeTest() throws Exception {
		DbUtils.deleteFromEsDbByPattern("_all");
		Resource resourceObj = AtomicOperationUtils
				.createResourceByType(ResourceTypeEnum.VFC, UserRoleEnum.DESIGNER, true).left().value();
		resourceDetails1 = new ResourceReqDetails(resourceObj);
		heatArtifactDetails = ElementFactory.getDefaultDeploymentArtifactForType(ArtifactTypeEnum.HEAT.getType());
		createThreeServices(sdncDesignerDetails1);
	}

	protected void createThreeServices(User user) throws Exception {

		String checkinComment = "good checkin";
		String checkinComentJson = "{\"userRemarks\": \"" + checkinComment + "\"}";

		RestResponse addInformationalArtifactToResource = ArtifactRestUtils.addInformationalArtifactToResource(
				heatArtifactDetails, sdncDesignerDetails1, resourceDetails1.getUniqueId());
		RestResponse certifyResource = LifecycleRestUtils.certifyResource(resourceDetails1);
		componentInstanceReqDetails = ElementFactory.getDefaultComponentInstance("defaultInstance", resourceDetails1);

		serviceDetails11 = ElementFactory.getDefaultService();
		serviceDetails22 = ElementFactory.getDefaultService();
		serviceDetails33 = ElementFactory.getDefaultService();

		serviceDetails11.setName(serviceDetails11.getName() + "1");
		List<String> tags = serviceDetails11.getTags();
		tags.add(serviceDetails11.getName());
		serviceDetails11.setTags(tags);

		serviceDetails22.setName(serviceDetails11.getName() + "2");
		tags = serviceDetails22.getTags();
		tags.add(serviceDetails22.getName());
		serviceDetails22.setTags(tags);

		serviceDetails33.setName(serviceDetails11.getName() + "3");
		tags = serviceDetails33.getTags();
		tags.add(serviceDetails33.getName());
		serviceDetails33.setTags(tags);
		
		RestResponse createServiceResponse1 = createService(user, serviceDetails11);
		RestResponse createServiceResponse2 = createService(user, serviceDetails22);
		RestResponse createServiceResponse3 = createService(user, serviceDetails33);
	}

	protected RestResponse createService(User user, ServiceReqDetails serviceDetails) throws Exception, IOException {
		RestResponse createServiceResponse1 = ServiceRestUtils.createService(serviceDetails, user);
		assertNotNull("check response object is not null after creating service", createServiceResponse1);
		assertNotNull("check if error code exists in response after creating service",
				createServiceResponse1.getErrorCode());
		assertEquals("Check response code after creating service", 201,
				createServiceResponse1.getErrorCode().intValue());
		Service convertServiceResponseToJavaObject = ResponseParser
				.convertServiceResponseToJavaObject(createServiceResponse1.getResponse());
		serviceDetails.setUniqueId(convertServiceResponseToJavaObject.getUniqueId());
		logger.debug("Created service1 ={}", serviceDetails);
		addResourceWithHeatArt(serviceDetails);
		return createServiceResponse1;
	}

	protected void addResourceWithHeatArt(ServiceReqDetails serviceDetails) throws Exception {

		RestResponse createResourceInstance = ComponentInstanceRestUtils.createComponentInstance(
				componentInstanceReqDetails, sdncDesignerDetails1, serviceDetails.getUniqueId(),
				ComponentTypeEnum.SERVICE);
		// System.out.println("serviceUID --->" + serviceDetails.getUniqueId());
		assertEquals("Check response code ", 201, createResourceInstance.getErrorCode().intValue());
	}

	protected void certifyAllServices() throws Exception {
		LifecycleRestUtils.certifyService(serviceDetails11);
		LifecycleRestUtils.certifyService(serviceDetails22);
		LifecycleRestUtils.certifyService(serviceDetails33);
	}

	protected boolean isElementInArray(String elementId, JSONArray jsonArray) throws Exception {
		for (int i = 0; i < jsonArray.size(); i++) {
			JSONObject jobject = (JSONObject) jsonArray.get(i);

			if (jobject.get("uniqueId").toString().equals(elementId)) {
				return true;
			}
		}
		return false;
	}

	protected void approveDistributionStatusOfCertifiedService(ServiceReqDetails serviceDetails, User user)
			throws Exception {
		approveDistributionStatusOfService(serviceDetails, user, "1.0");
	}

	protected void approveDistributionStatusOfService(ServiceReqDetails serviceDetails, User user, String version)
			throws Exception {
		RestResponse res = LifecycleRestUtils.sendApproveDistribution(user, serviceDetails.getUniqueId(), userRemarks);
		assertEquals(200, res.getErrorCode().intValue());
		ServiceValidationUtils.validateDistrubtionStatusValue(res, DistributionStatusEnum.DISTRIBUTION_APPROVED);
	}

	protected void rejectDistributionStatusOfService(ServiceReqDetails serviceDetails, User user) throws Exception {
		rejectDistributionStatusOfService(serviceDetails, user, "1.0");
	}

	protected void rejectDistributionStatusOfService(ServiceReqDetails serviceDetails, User user, String version)
			throws Exception {
		RestResponse res = LifecycleRestUtils.rejectDistribution(serviceDetails, version, user, userRemarks);
		assertEquals(200, res.getErrorCode().intValue());
		ServiceValidationUtils.validateDistrubtionStatusValue(res, DistributionStatusEnum.DISTRIBUTION_REJECTED);
	}

	protected JSONArray getFollowedListAsJsonArray(User user) throws Exception {
		RestResponse getGovernorFollowed = ServiceRestUtils.getFollowed(user);
		assertNotNull(getGovernorFollowed);
		assertNotNull(getGovernorFollowed.getErrorCode());
		assertEquals(200, getGovernorFollowed.getErrorCode().intValue());

		JSONArray listArrayFromRestResponse = ServiceRestUtils.getListArrayFromRestResponse(getGovernorFollowed);

		return listArrayFromRestResponse;
	}

	protected void changeDistributionStatusOfAllService(boolean approved, User user) throws Exception {
		if (approved) {
			approveDistributionStatusOfCertifiedService(serviceDetails11, user);
			approveDistributionStatusOfCertifiedService(serviceDetails22, user);
			approveDistributionStatusOfCertifiedService(serviceDetails33, user);
		} else {
			rejectDistributionStatusOfService(serviceDetails11, user);
			rejectDistributionStatusOfService(serviceDetails22, user);
			rejectDistributionStatusOfService(serviceDetails33, user);
		}

	}

	protected JSONArray checkFollowed(User user) throws Exception {
		JSONArray getFollowedList = getFollowedListAsJsonArray(user);
		assertFalse(getFollowedList.isEmpty());
		assertTrue(isElementInArray(serviceDetails11.getUniqueId(), getFollowedList));
		assertTrue(isElementInArray(serviceDetails22.getUniqueId(), getFollowedList));
		assertTrue(isElementInArray(serviceDetails33.getUniqueId(), getFollowedList));

		return getFollowedList;
	}

	// -------------------------------------T E S T
	// S------------------------------------------------------//

	@Test
	public void governorList_AllCertifiedVersionsOfService() throws Exception {
		certifyAllServices();
		String serviceUniqueIdCertified1 = serviceDetails11.getUniqueId();
		RestResponse res = LifecycleRestUtils.changeServiceState(serviceDetails11, sdncDesignerDetails1, "1.0",
				LifeCycleStatesEnum.CHECKOUT);
		assertEquals(200, res.getErrorCode().intValue());

		JSONArray getFollowedList = getFollowedListAsJsonArray(governor);
		assertFalse(getFollowedList.isEmpty());
		assertFalse(isElementInArray(serviceDetails11.getUniqueId(), getFollowedList));
		assertTrue(isElementInArray(serviceDetails22.getUniqueId(), getFollowedList));
		assertTrue(isElementInArray(serviceDetails33.getUniqueId(), getFollowedList));
		assertTrue(isElementInArray(serviceUniqueIdCertified1, getFollowedList));
		assertEquals(3, getFollowedList.size());

		// certifyService(serviceDetails11, "1.1");
		LifecycleRestUtils.certifyService(serviceDetails11);

		JSONArray governorFollowedList2 = checkFollowed(governor);
		assertEquals(4, governorFollowedList2.size());
		assertTrue(isElementInArray(serviceDetails11.getUniqueId(), governorFollowedList2));
		assertTrue(isElementInArray(serviceUniqueIdCertified1, governorFollowedList2));

	}

	// -------------------------------------T E S T
	// S------------------------------------------------------//

	@Test
	public void governorList_distributionNotApproved() throws Exception {
		certifyAllServices();

		JSONArray checkFollowed = checkFollowed(governor);
		assertEquals(3, checkFollowed.size());
	}

	@Test
	public void governorGetEmptyListTest_notCertifiedServices() throws Exception {
		JSONArray governorFollowedList = getFollowedListAsJsonArray(governor);

		assertTrue(governorFollowedList.isEmpty());
	}

	@Test
	public void governorList_distributionApproved() throws Exception {
		certifyAllServices();
		boolean approved = true;
		changeDistributionStatusOfAllService(approved, governor);

		JSONArray checkFollowed = checkFollowed(governor);
		assertEquals(3, checkFollowed.size());
	}

	@Test(enabled = false)
	public void governorList_distributed() throws Exception {
		certifyAllServices();

		LifecycleRestUtils.changeDistributionStatus(serviceDetails11, "1.0", governor, userRemarks,
				DistributionStatusEnum.DISTRIBUTED);
		LifecycleRestUtils.changeDistributionStatus(serviceDetails22, "1.0", governor, userRemarks,
				DistributionStatusEnum.DISTRIBUTED);
		LifecycleRestUtils.changeDistributionStatus(serviceDetails33, "1.0", governor, userRemarks,
				DistributionStatusEnum.DISTRIBUTED);

		JSONArray governorFollowedList = getFollowedListAsJsonArray(governor);
		assertFalse(governorFollowedList.isEmpty());
		assertTrue(isElementInArray(serviceDetails11.getUniqueId(), governorFollowedList));
		assertTrue(isElementInArray(serviceDetails22.getUniqueId(), governorFollowedList));
		assertTrue(isElementInArray(serviceDetails33.getUniqueId(), governorFollowedList));
	}

	@Test
	public void governorList_distributionRejected() throws Exception {
		certifyAllServices();
		boolean distributionRejected = false;
		changeDistributionStatusOfAllService(distributionRejected, governor);

		JSONArray checkFollowed = checkFollowed(governor);
		assertEquals(3, checkFollowed.size());
	}

}