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