2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.utils.validation;
23 import org.apache.log4j.Logger;
24 import org.json.simple.JSONArray;
25 import org.json.simple.JSONObject;
26 import org.json.simple.JSONValue;
27 import org.openecomp.sdc.be.model.Product;
28 import org.openecomp.sdc.be.model.User;
29 import org.openecomp.sdc.be.model.category.CategoryDefinition;
30 import org.openecomp.sdc.be.model.category.GroupingDefinition;
31 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
32 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest.ComponentOperationEnum;
33 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
34 import org.openecomp.sdc.ci.tests.utils.rest.ProductRestUtils;
35 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
37 import java.util.Arrays;
38 import java.util.List;
40 import java.util.UUID;
42 import static org.testng.AssertJUnit.*;
44 public class ProductValidationUtils {
46 static Logger logger = Logger.getLogger(ProductValidationUtils.class.getName());
48 public static void compareExpectedAndActualProducts(Product expectedProduct, Product actualProduct) {
49 compareExpectedAndActualProducts(expectedProduct, actualProduct, null);
52 public static void compareExpectedAndActualProducts(Product expectedProduct, Product actualProduct,
53 ComponentOperationEnum operation) {
55 assertEquals(expectedProduct.getName(), actualProduct.getName());
56 assertEquals(expectedProduct.getFullName(), actualProduct.getFullName());
57 assertEquals(expectedProduct.getDescription(), actualProduct.getDescription());
59 List<String> expectedContacts = expectedProduct.getContacts();
60 List<String> actualContacts = actualProduct.getContacts();
62 "Expected contacts:" + Arrays.toString(expectedContacts.toArray()) + ", actual contacts:"
63 + Arrays.toString(actualContacts.toArray()),
64 expectedContacts.size() == actualContacts.size() && expectedContacts.containsAll(actualContacts)
65 && actualContacts.containsAll(expectedContacts));
67 List<String> expectedTags = expectedProduct.getTags();
68 List<String> actualTags = actualProduct.getTags();
70 "Expected tags:" + Arrays.toString(expectedTags.toArray()) + ", actual tags:"
71 + Arrays.toString(actualTags.toArray()),
72 expectedTags.size() == actualTags.size() && expectedTags.containsAll(actualTags)
73 && actualTags.containsAll(expectedTags));
75 assertEquals(expectedProduct.getLifecycleState(), actualProduct.getLifecycleState());
76 assertEquals(expectedProduct.getVersion(), actualProduct.getVersion());
77 assertEquals(expectedProduct.isHighestVersion(), actualProduct.isHighestVersion());
78 assertEquals(expectedProduct.getNormalizedName(), actualProduct.getNormalizedName());
80 compareCategories(expectedProduct, actualProduct);
81 assertEquals(expectedProduct.getLastUpdaterUserId(), actualProduct.getLastUpdaterUserId());
82 if (operation != null) {
83 assertEquals(expectedProduct.getCreatorUserId(), actualProduct.getCreatorUserId());
86 Long lastUpdateDate = actualProduct.getLastUpdateDate();
87 Long creationDate = actualProduct.getCreationDate();
88 Map<String, String> allVersions = actualProduct.getAllVersions();
90 if (operation != null) {
91 if (operation == ComponentOperationEnum.UPDATE_COMPONENT
92 || operation == ComponentOperationEnum.CHANGE_STATE_CHECKOUT
93 || operation == ComponentOperationEnum.CHANGE_STATE_CHECKIN
94 || operation == ComponentOperationEnum.CHANGE_STATE_UNDO_CHECKOUT) {
95 assertTrue("Last update date:" + lastUpdateDate + ", creation date: " + creationDate,
96 lastUpdateDate > 0 && creationDate > 0 && lastUpdateDate > creationDate);
98 assertTrue("Last update date:" + lastUpdateDate + ", creation date: " + creationDate,
99 lastUpdateDate > 0 && lastUpdateDate.equals(creationDate));
104 // If just created, no way to test the UUIDs themselves
105 // If updated, we expect the UUIDs of actual to match the expected
106 String uniqueId = actualProduct.getUniqueId();
107 if (operation == ComponentOperationEnum.CREATE_COMPONENT) {
108 UUID.fromString(uniqueId);
109 UUID.fromString(actualProduct.getUUID());
110 UUID.fromString(actualProduct.getInvariantUUID());
111 assertTrue(allVersions.size() == 1);
112 assertTrue(allVersions.get("0.1").equals(uniqueId));
114 if (operation == ComponentOperationEnum.CHANGE_STATE_CHECKOUT) {
115 assertFalse(expectedProduct.getUniqueId().equals(uniqueId));
116 // Assigning the updated uniqueId to expected so that it can be
117 // passed to further logic
118 expectedProduct.setUniqueId(uniqueId);
119 } else if (operation != null) {
120 assertTrue(expectedProduct.getUniqueId().equals(uniqueId));
122 assertEquals(expectedProduct.getUUID(), actualProduct.getUUID());
123 assertEquals(expectedProduct.getInvariantUUID(), actualProduct.getInvariantUUID());
127 private static void compareCategories(Product expectedProduct, Product actualProduct) {
128 List<CategoryDefinition> expectedCategories = expectedProduct.getCategories();
129 List<CategoryDefinition> actualCategories = actualProduct.getCategories();
130 if (expectedCategories != null && actualCategories != null) {
131 int expSize = expectedCategories.size();
132 int actSize = actualCategories.size();
134 assertTrue("Expected size:" + expSize + ", actual size:" + actSize, expSize == actSize);
136 for (CategoryDefinition actualDefinition : actualCategories) {
137 int lastIndexOfCat = expectedCategories.lastIndexOf(actualDefinition);
138 assertTrue("Actual category " + actualDefinition + " not found in expected.", lastIndexOfCat != -1);
139 CategoryDefinition expectedDefinition = expectedCategories.get(lastIndexOfCat);
140 List<SubCategoryDefinition> actualSubcategories = actualDefinition.getSubcategories();
141 List<SubCategoryDefinition> expectedSubcategories = expectedDefinition.getSubcategories();
142 for (SubCategoryDefinition actualSub : actualSubcategories) {
143 lastIndexOfCat = expectedSubcategories.lastIndexOf(actualSub);
144 assertTrue("Actual subcategory " + actualSub + " not found in expected.", lastIndexOfCat != -1);
145 SubCategoryDefinition expectedSub = expectedSubcategories.get(lastIndexOfCat);
146 List<GroupingDefinition> actualGroupings = actualSub.getGroupings();
147 List<GroupingDefinition> expectedGroupings = expectedSub.getGroupings();
148 for (GroupingDefinition actualGrouping : actualGroupings) {
149 lastIndexOfCat = expectedGroupings.lastIndexOf(actualGrouping);
150 assertTrue("Actual grouping " + actualSub + " not found in expected.", lastIndexOfCat != -1);
155 for (CategoryDefinition expectedDefinition : expectedCategories) {
156 int lastIndexOfCat = actualCategories.lastIndexOf(expectedDefinition);
157 assertTrue("Expected category " + expectedDefinition + " not found in actual.", lastIndexOfCat != -1);
158 CategoryDefinition actualDefinition = actualCategories.get(lastIndexOfCat);
159 List<SubCategoryDefinition> actualSubcategories = actualDefinition.getSubcategories();
160 List<SubCategoryDefinition> expectedSubcategories = expectedDefinition.getSubcategories();
161 for (SubCategoryDefinition expectedSub : expectedSubcategories) {
162 lastIndexOfCat = actualSubcategories.lastIndexOf(expectedSub);
163 assertTrue("Expected subcategory " + expectedSub + " not found in actual.", lastIndexOfCat != -1);
164 SubCategoryDefinition actualSub = actualSubcategories.get(lastIndexOfCat);
165 List<GroupingDefinition> actualGroupings = actualSub.getGroupings();
166 List<GroupingDefinition> expectedGroupings = expectedSub.getGroupings();
167 for (GroupingDefinition expectedGrouping : expectedGroupings) {
168 lastIndexOfCat = actualGroupings.lastIndexOf(expectedGrouping);
169 assertTrue("Expected grouping " + expectedGrouping + " not found in actual.",
170 lastIndexOfCat != -1);
177 public static void verifyProductsNotExistInUserFollowedPage(User user, Product... nonExpectedProducts)
179 String component = "products";
181 Product nonExpectedProduct;
182 RestResponse getFollowedPage = ProductRestUtils.getFollowed(user.getUserId());
183 JSONArray followedProductes = getListArrayFromRestResponse(getFollowedPage, component);
184 if (followedProductes != null) { // if any product exist in followed
186 for (int i = 0; i < nonExpectedProducts.length; i++) {
187 nonExpectedProduct = nonExpectedProducts[i];
189 for (int k = 0; k < followedProductes.size(); k++) {
190 JSONObject jobject = (JSONObject) followedProductes.get(k);
191 if (jobject.get("uuid").toString().equals(nonExpectedProduct.getUUID())) {
193 k = followedProductes.size();
196 assertFalse(isExist);
202 public static void checkUserFollowedPage(User user, Product... expectedProducts) throws Exception {
203 String component = "products";
205 Product expectedProduct;
206 RestResponse getFollowedPage = ProductRestUtils.getFollowed(user.getUserId());
207 JSONArray followedProductes = getListArrayFromRestResponse(getFollowedPage, component);
208 assertTrue("check if any followedProductes received ", followedProductes != null);
209 assertTrue("check if any expectedProducts and followedProductes are the same size",
210 expectedProducts.length == followedProductes.size());
211 for (int i = 0; i < expectedProducts.length; i++) {
212 expectedProduct = expectedProducts[i];
214 for (int k = 0; k < followedProductes.size(); k++) {
215 JSONObject jobject = (JSONObject) followedProductes.get(k);
216 // if(jobject.get("uuid").toString().equals(expectedProduct.getUUID()))
217 if (jobject.get("uniqueId").toString().equals(expectedProduct.getUniqueId())) {
219 String productString = jobject.toJSONString();
220 Product actualProduct = ResponseParser.parseToObjectUsingMapper(productString, Product.class);
221 ProductValidationUtils.compareExpectedAndActualProducts(expectedProduct, actualProduct, null);
223 k = followedProductes.size();
230 private static JSONArray getListArrayFromRestResponse(RestResponse restResponse, String lst) {
231 String json = restResponse.getResponse();
232 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
233 JSONArray resources = (JSONArray) jsonResp.get(lst);