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.onap.sdc.backend.ci.tests.utils.validation;
23 import org.openecomp.sdc.logging.api.Logger;
24 import org.openecomp.sdc.logging.api.LoggerFactory;
25 import org.json.simple.JSONArray;
26 import org.json.simple.JSONObject;
27 import org.json.simple.JSONValue;
28 import org.onap.sdc.backend.ci.tests.datatypes.http.RestResponse;
29 import org.openecomp.sdc.be.model.Product;
30 import org.openecomp.sdc.be.model.User;
31 import org.openecomp.sdc.be.model.category.CategoryDefinition;
32 import org.openecomp.sdc.be.model.category.GroupingDefinition;
33 import org.openecomp.sdc.be.model.category.SubCategoryDefinition;
34 import org.onap.sdc.backend.ci.tests.api.ComponentBaseTest.ComponentOperationEnum;
35 import org.onap.sdc.backend.ci.tests.utils.rest.ProductRestUtils;
36 import org.onap.sdc.backend.ci.tests.utils.rest.ResponseParser;
38 import java.util.Arrays;
39 import java.util.List;
41 import java.util.UUID;
43 import static org.testng.AssertJUnit.*;
45 public class ProductValidationUtils {
47 static Logger logger = LoggerFactory.getLogger(ProductValidationUtils.class);
49 public static void compareExpectedAndActualProducts(Product expectedProduct, Product actualProduct) {
50 compareExpectedAndActualProducts(expectedProduct, actualProduct, null);
53 public static void compareExpectedAndActualProducts(Product expectedProduct, Product actualProduct,
54 ComponentOperationEnum operation) {
56 assertEquals(expectedProduct.getName(), actualProduct.getName());
57 assertEquals(expectedProduct.getFullName(), actualProduct.getFullName());
58 assertEquals(expectedProduct.getDescription(), actualProduct.getDescription());
60 List<String> expectedContacts = expectedProduct.getContacts();
61 List<String> actualContacts = actualProduct.getContacts();
63 "Expected contacts:" + Arrays.toString(expectedContacts.toArray()) + ", actual contacts:"
64 + Arrays.toString(actualContacts.toArray()),
65 expectedContacts.size() == actualContacts.size() && expectedContacts.containsAll(actualContacts)
66 && actualContacts.containsAll(expectedContacts));
68 List<String> expectedTags = expectedProduct.getTags();
69 List<String> actualTags = actualProduct.getTags();
71 "Expected tags:" + Arrays.toString(expectedTags.toArray()) + ", actual tags:"
72 + Arrays.toString(actualTags.toArray()),
73 expectedTags.size() == actualTags.size() && expectedTags.containsAll(actualTags)
74 && actualTags.containsAll(expectedTags));
76 assertEquals(expectedProduct.getLifecycleState(), actualProduct.getLifecycleState());
77 assertEquals(expectedProduct.getVersion(), actualProduct.getVersion());
78 assertEquals(expectedProduct.isHighestVersion(), actualProduct.isHighestVersion());
79 assertEquals(expectedProduct.getNormalizedName(), actualProduct.getNormalizedName());
81 compareCategories(expectedProduct, actualProduct);
82 assertEquals(expectedProduct.getLastUpdaterUserId(), actualProduct.getLastUpdaterUserId());
83 if (operation != null) {
84 assertEquals(expectedProduct.getCreatorUserId(), actualProduct.getCreatorUserId());
87 Long lastUpdateDate = actualProduct.getLastUpdateDate();
88 Long creationDate = actualProduct.getCreationDate();
89 Map<String, String> allVersions = actualProduct.getAllVersions();
91 if (operation != null) {
92 if (operation == ComponentOperationEnum.UPDATE_COMPONENT
93 || operation == ComponentOperationEnum.CHANGE_STATE_CHECKOUT
94 || operation == ComponentOperationEnum.CHANGE_STATE_CHECKIN
95 || operation == ComponentOperationEnum.CHANGE_STATE_UNDO_CHECKOUT) {
96 assertTrue("Last update date:" + lastUpdateDate + ", creation date: " + creationDate,
97 lastUpdateDate > 0 && creationDate > 0 && lastUpdateDate > creationDate);
99 assertTrue("Last update date:" + lastUpdateDate + ", creation date: " + creationDate,
100 lastUpdateDate > 0 && lastUpdateDate.equals(creationDate));
105 // If just created, no way to test the UUIDs themselves
106 // If updated, we expect the UUIDs of actual to match the expected
107 String uniqueId = actualProduct.getUniqueId();
108 if (operation == ComponentOperationEnum.CREATE_COMPONENT) {
109 UUID.fromString(uniqueId);
110 UUID.fromString(actualProduct.getUUID());
111 UUID.fromString(actualProduct.getInvariantUUID());
112 assertTrue(allVersions.size() == 1);
113 assertTrue(allVersions.get("0.1").equals(uniqueId));
115 if (operation == ComponentOperationEnum.CHANGE_STATE_CHECKOUT) {
116 assertFalse(expectedProduct.getUniqueId().equals(uniqueId));
117 // Assigning the updated uniqueId to expected so that it can be
118 // passed to further logic
119 expectedProduct.setUniqueId(uniqueId);
120 } else if (operation != null) {
121 assertTrue(expectedProduct.getUniqueId().equals(uniqueId));
123 assertEquals(expectedProduct.getUUID(), actualProduct.getUUID());
124 assertEquals(expectedProduct.getInvariantUUID(), actualProduct.getInvariantUUID());
128 private static void compareCategories(Product expectedProduct, Product actualProduct) {
129 List<CategoryDefinition> expectedCategories = expectedProduct.getCategories();
130 List<CategoryDefinition> actualCategories = actualProduct.getCategories();
131 if (expectedCategories != null && actualCategories != null) {
132 int expSize = expectedCategories.size();
133 int actSize = actualCategories.size();
135 assertTrue("Expected size:" + expSize + ", actual size:" + actSize, expSize == actSize);
137 for (CategoryDefinition actualDefinition : actualCategories) {
138 int lastIndexOfCat = expectedCategories.lastIndexOf(actualDefinition);
139 assertTrue("Actual category " + actualDefinition + " not found in expected.", lastIndexOfCat != -1);
140 CategoryDefinition expectedDefinition = expectedCategories.get(lastIndexOfCat);
141 List<SubCategoryDefinition> actualSubcategories = actualDefinition.getSubcategories();
142 List<SubCategoryDefinition> expectedSubcategories = expectedDefinition.getSubcategories();
143 for (SubCategoryDefinition actualSub : actualSubcategories) {
144 lastIndexOfCat = expectedSubcategories.lastIndexOf(actualSub);
145 assertTrue("Actual subcategory " + actualSub + " not found in expected.", lastIndexOfCat != -1);
146 SubCategoryDefinition expectedSub = expectedSubcategories.get(lastIndexOfCat);
147 List<GroupingDefinition> actualGroupings = actualSub.getGroupings();
148 List<GroupingDefinition> expectedGroupings = expectedSub.getGroupings();
149 for (GroupingDefinition actualGrouping : actualGroupings) {
150 lastIndexOfCat = expectedGroupings.lastIndexOf(actualGrouping);
151 assertTrue("Actual grouping " + actualSub + " not found in expected.", lastIndexOfCat != -1);
156 for (CategoryDefinition expectedDefinition : expectedCategories) {
157 int lastIndexOfCat = actualCategories.lastIndexOf(expectedDefinition);
158 assertTrue("Expected category " + expectedDefinition + " not found in actual.", lastIndexOfCat != -1);
159 CategoryDefinition actualDefinition = actualCategories.get(lastIndexOfCat);
160 List<SubCategoryDefinition> actualSubcategories = actualDefinition.getSubcategories();
161 List<SubCategoryDefinition> expectedSubcategories = expectedDefinition.getSubcategories();
162 for (SubCategoryDefinition expectedSub : expectedSubcategories) {
163 lastIndexOfCat = actualSubcategories.lastIndexOf(expectedSub);
164 assertTrue("Expected subcategory " + expectedSub + " not found in actual.", lastIndexOfCat != -1);
165 SubCategoryDefinition actualSub = actualSubcategories.get(lastIndexOfCat);
166 List<GroupingDefinition> actualGroupings = actualSub.getGroupings();
167 List<GroupingDefinition> expectedGroupings = expectedSub.getGroupings();
168 for (GroupingDefinition expectedGrouping : expectedGroupings) {
169 lastIndexOfCat = actualGroupings.lastIndexOf(expectedGrouping);
170 assertTrue("Expected grouping " + expectedGrouping + " not found in actual.",
171 lastIndexOfCat != -1);
178 public static void verifyProductsNotExistInUserFollowedPage(User user, Product... nonExpectedProducts)
180 String component = "products";
182 Product nonExpectedProduct;
183 RestResponse getFollowedPage = ProductRestUtils.getFollowed(user.getUserId());
184 JSONArray followedProductes = getListArrayFromRestResponse(getFollowedPage, component);
185 if (followedProductes != null) { // if any product exist in followed
187 for (int i = 0; i < nonExpectedProducts.length; i++) {
188 nonExpectedProduct = nonExpectedProducts[i];
190 for (int k = 0; k < followedProductes.size(); k++) {
191 JSONObject jobject = (JSONObject) followedProductes.get(k);
192 if (jobject.get("uuid").toString().equals(nonExpectedProduct.getUUID())) {
194 k = followedProductes.size();
197 assertFalse(isExist);
203 public static void checkUserFollowedPage(User user, Product... expectedProducts) throws Exception {
204 String component = "products";
206 Product expectedProduct;
207 RestResponse getFollowedPage = ProductRestUtils.getFollowed(user.getUserId());
208 JSONArray followedProductes = getListArrayFromRestResponse(getFollowedPage, component);
209 assertTrue("check if any followedProductes received ", followedProductes != null);
210 assertTrue("check if any expectedProducts and followedProductes are the same size",
211 expectedProducts.length == followedProductes.size());
212 for (int i = 0; i < expectedProducts.length; i++) {
213 expectedProduct = expectedProducts[i];
215 for (int k = 0; k < followedProductes.size(); k++) {
216 JSONObject jobject = (JSONObject) followedProductes.get(k);
217 // if(jobject.get("uuid").toString().equals(expectedProduct.getUUID()))
218 if (jobject.get("uniqueId").toString().equals(expectedProduct.getUniqueId())) {
220 String productString = jobject.toJSONString();
221 Product actualProduct = ResponseParser.parseToObjectUsingMapper(productString, Product.class);
222 ProductValidationUtils.compareExpectedAndActualProducts(expectedProduct, actualProduct, null);
224 k = followedProductes.size();
231 private static JSONArray getListArrayFromRestResponse(RestResponse restResponse, String lst) {
232 String json = restResponse.getResponse();
233 JSONObject jsonResp = (JSONObject) JSONValue.parse(json);
234 JSONArray resources = (JSONArray) jsonResp.get(lst);