aboutsummaryrefslogtreecommitdiffstats
path: root/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/utils/validation/ProductValidationUtils.java
blob: f2ef0826f2172aa810d939e8fc19f59add4dcfeb (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
/*-
 * ============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.utils.validation;

import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.json.simple.JSONArray;
import org.json.simple.JSONObject;
import org.json.simple.JSONValue;
import org.openecomp.sdc.be.model.Product;
import org.openecomp.sdc.be.model.User;
import org.openecomp.sdc.be.model.category.CategoryDefinition;
import org.openecomp.sdc.be.model.category.GroupingDefinition;
import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
import org.openecomp.sdc.ci.tests.api.ComponentBaseTest.ComponentOperationEnum;
import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
import org.openecomp.sdc.ci.tests.utils.rest.ProductRestUtils;
import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.UUID;

import static org.testng.AssertJUnit.*;

public class ProductValidationUtils {

	static Logger logger = LogManager.getLogger(ProductValidationUtils.class);

	public static void compareExpectedAndActualProducts(Product expectedProduct, Product actualProduct) {
		compareExpectedAndActualProducts(expectedProduct, actualProduct, null);
	}

	public static void compareExpectedAndActualProducts(Product expectedProduct, Product actualProduct,
			ComponentOperationEnum operation) {

		assertEquals(expectedProduct.getName(), actualProduct.getName());
		assertEquals(expectedProduct.getFullName(), actualProduct.getFullName());
		assertEquals(expectedProduct.getDescription(), actualProduct.getDescription());

		List<String> expectedContacts = expectedProduct.getContacts();
		List<String> actualContacts = actualProduct.getContacts();
		assertTrue(
				"Expected contacts:" + Arrays.toString(expectedContacts.toArray()) + ", actual contacts:"
						+ Arrays.toString(actualContacts.toArray()),
				expectedContacts.size() == actualContacts.size() && expectedContacts.containsAll(actualContacts)
						&& actualContacts.containsAll(expectedContacts));

		List<String> expectedTags = expectedProduct.getTags();
		List<String> actualTags = actualProduct.getTags();
		assertTrue(
				"Expected tags:" + Arrays.toString(expectedTags.toArray()) + ", actual tags:"
						+ Arrays.toString(actualTags.toArray()),
				expectedTags.size() == actualTags.size() && expectedTags.containsAll(actualTags)
						&& actualTags.containsAll(expectedTags));

		assertEquals(expectedProduct.getLifecycleState(), actualProduct.getLifecycleState());
		assertEquals(expectedProduct.getVersion(), actualProduct.getVersion());
		assertEquals(expectedProduct.isHighestVersion(), actualProduct.isHighestVersion());
		assertEquals(expectedProduct.getNormalizedName(), actualProduct.getNormalizedName());

		compareCategories(expectedProduct, actualProduct);
		assertEquals(expectedProduct.getLastUpdaterUserId(), actualProduct.getLastUpdaterUserId());
		if (operation != null) {
			assertEquals(expectedProduct.getCreatorUserId(), actualProduct.getCreatorUserId());
		}

		Long lastUpdateDate = actualProduct.getLastUpdateDate();
		Long creationDate = actualProduct.getCreationDate();
		Map<String, String> allVersions = actualProduct.getAllVersions();

		if (operation != null) {
			if (operation == ComponentOperationEnum.UPDATE_COMPONENT
					|| operation == ComponentOperationEnum.CHANGE_STATE_CHECKOUT
					|| operation == ComponentOperationEnum.CHANGE_STATE_CHECKIN
					|| operation == ComponentOperationEnum.CHANGE_STATE_UNDO_CHECKOUT) {
				assertTrue("Last update date:" + lastUpdateDate + ", creation date: " + creationDate,
						lastUpdateDate > 0 && creationDate > 0 && lastUpdateDate > creationDate);
			} else {
				assertTrue("Last update date:" + lastUpdateDate + ", creation date: " + creationDate,
						lastUpdateDate > 0 && lastUpdateDate.equals(creationDate));
			}
		}

		// Check UUIDs
		// If just created, no way to test the UUIDs themselves
		// If updated, we expect the UUIDs of actual to match the expected
		String uniqueId = actualProduct.getUniqueId();
		if (operation == ComponentOperationEnum.CREATE_COMPONENT) {
			UUID.fromString(uniqueId);
			UUID.fromString(actualProduct.getUUID());
			UUID.fromString(actualProduct.getInvariantUUID());
			assertTrue(allVersions.size() == 1);
			assertTrue(allVersions.get("0.1").equals(uniqueId));
		} else {
			if (operation == ComponentOperationEnum.CHANGE_STATE_CHECKOUT) {
				assertFalse(expectedProduct.getUniqueId().equals(uniqueId));
				// Assigning the updated uniqueId to expected so that it can be
				// passed to further logic
				expectedProduct.setUniqueId(uniqueId);
			} else if (operation != null) {
				assertTrue(expectedProduct.getUniqueId().equals(uniqueId));
			}
			assertEquals(expectedProduct.getUUID(), actualProduct.getUUID());
			assertEquals(expectedProduct.getInvariantUUID(), actualProduct.getInvariantUUID());
		}
	}

	private static void compareCategories(Product expectedProduct, Product actualProduct) {
		List<CategoryDefinition> expectedCategories = expectedProduct.getCategories();
		List<CategoryDefinition> actualCategories = actualProduct.getCategories();
		if (expectedCategories != null && actualCategories != null) {
			int expSize = expectedCategories.size();
			int actSize = actualCategories.size();

			assertTrue("Expected size:" + expSize + ", actual size:" + actSize, expSize == actSize);

			for (CategoryDefinition actualDefinition : actualCategories) {
				int lastIndexOfCat = expectedCategories.lastIndexOf(actualDefinition);
				assertTrue("Actual category " + actualDefinition + " not found in expected.", lastIndexOfCat != -1);
				CategoryDefinition expectedDefinition = expectedCategories.get(lastIndexOfCat);
				List<SubCategoryDefinition> actualSubcategories = actualDefinition.getSubcategories();
				List<SubCategoryDefinition> expectedSubcategories = expectedDefinition.getSubcategories();
				for (SubCategoryDefinition actualSub : actualSubcategories) {
					lastIndexOfCat = expectedSubcategories.lastIndexOf(actualSub);
					assertTrue("Actual subcategory " + actualSub + " not found in expected.", lastIndexOfCat != -1);
					SubCategoryDefinition expectedSub = expectedSubcategories.get(lastIndexOfCat);
					List<GroupingDefinition> actualGroupings = actualSub.getGroupings();
					List<GroupingDefinition> expectedGroupings = expectedSub.getGroupings();
					for (GroupingDefinition actualGrouping : actualGroupings) {
						lastIndexOfCat = expectedGroupings.lastIndexOf(actualGrouping);
						assertTrue("Actual grouping " + actualSub + " not found in expected.", lastIndexOfCat != -1);
					}
				}
			}

			for (CategoryDefinition expectedDefinition : expectedCategories) {
				int lastIndexOfCat = actualCategories.lastIndexOf(expectedDefinition);
				assertTrue("Expected category " + expectedDefinition + " not found in actual.", lastIndexOfCat != -1);
				CategoryDefinition actualDefinition = actualCategories.get(lastIndexOfCat);
				List<SubCategoryDefinition> actualSubcategories = actualDefinition.getSubcategories();
				List<SubCategoryDefinition> expectedSubcategories = expectedDefinition.getSubcategories();
				for (SubCategoryDefinition expectedSub : expectedSubcategories) {
					lastIndexOfCat = actualSubcategories.lastIndexOf(expectedSub);
					assertTrue("Expected subcategory " + expectedSub + " not found in actual.", lastIndexOfCat != -1);
					SubCategoryDefinition actualSub = actualSubcategories.get(lastIndexOfCat);
					List<GroupingDefinition> actualGroupings = actualSub.getGroupings();
					List<GroupingDefinition> expectedGroupings = expectedSub.getGroupings();
					for (GroupingDefinition expectedGrouping : expectedGroupings) {
						lastIndexOfCat = actualGroupings.lastIndexOf(expectedGrouping);
						assertTrue("Expected grouping " + expectedGrouping + " not found in actual.",
								lastIndexOfCat != -1);
					}
				}
			}
		}
	}

	public static void verifyProductsNotExistInUserFollowedPage(User user, Product... nonExpectedProducts)
			throws Exception {
		String component = "products";
		Boolean isExist;
		Product nonExpectedProduct;
		RestResponse getFollowedPage = ProductRestUtils.getFollowed(user.getUserId());
		JSONArray followedProductes = getListArrayFromRestResponse(getFollowedPage, component);
		if (followedProductes != null) { // if any product exist in followed
											// page
			for (int i = 0; i < nonExpectedProducts.length; i++) {
				nonExpectedProduct = nonExpectedProducts[i];
				isExist = false;
				for (int k = 0; k < followedProductes.size(); k++) {
					JSONObject jobject = (JSONObject) followedProductes.get(k);
					if (jobject.get("uuid").toString().equals(nonExpectedProduct.getUUID())) {
						isExist = true;
						k = followedProductes.size();
					}
				}
				assertFalse(isExist);
			}
		}

	}

	public static void checkUserFollowedPage(User user, Product... expectedProducts) throws Exception {
		String component = "products";
		Boolean isExist;
		Product expectedProduct;
		RestResponse getFollowedPage = ProductRestUtils.getFollowed(user.getUserId());
		JSONArray followedProductes = getListArrayFromRestResponse(getFollowedPage, component);
		assertTrue("check if any followedProductes received ", followedProductes != null);
		assertTrue("check if any expectedProducts and followedProductes are the same size",
				expectedProducts.length == followedProductes.size());
		for (int i = 0; i < expectedProducts.length; i++) {
			expectedProduct = expectedProducts[i];
			isExist = false;
			for (int k = 0; k < followedProductes.size(); k++) {
				JSONObject jobject = (JSONObject) followedProductes.get(k);
				// if(jobject.get("uuid").toString().equals(expectedProduct.getUUID()))
				if (jobject.get("uniqueId").toString().equals(expectedProduct.getUniqueId())) {

					String productString = jobject.toJSONString();
					Product actualProduct = ResponseParser.parseToObjectUsingMapper(productString, Product.class);
					ProductValidationUtils.compareExpectedAndActualProducts(expectedProduct, actualProduct, null);
					isExist = true;
					k = followedProductes.size();
				}
			}
			assertTrue(isExist);
		}
	}

	private static JSONArray getListArrayFromRestResponse(RestResponse restResponse, String lst) {
		String json = restResponse.getResponse();
		JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
		JSONArray resources = (JSONArray) jsonResp.get(lst);
		return resources;
	}

}