f6b61249ddf9eb5cf5dced14caa8a63ae247f33b
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / backend / 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.onap.sdc.backend.ci.tests.utils.validation;
22
23 import org.apache.logging.log4j.Logger;
24 import org.apache.logging.log4j.LogManager;
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;
37
38 import java.util.Arrays;
39 import java.util.List;
40 import java.util.Map;
41 import java.util.UUID;
42
43 import static org.testng.AssertJUnit.*;
44
45 public class ProductValidationUtils {
46
47         static Logger logger = LogManager.getLogger(ProductValidationUtils.class);
48
49         public static void compareExpectedAndActualProducts(Product expectedProduct, Product actualProduct) {
50                 compareExpectedAndActualProducts(expectedProduct, actualProduct, null);
51         }
52
53         public static void compareExpectedAndActualProducts(Product expectedProduct, Product actualProduct,
54                         ComponentOperationEnum operation) {
55
56                 assertEquals(expectedProduct.getName(), actualProduct.getName());
57                 assertEquals(expectedProduct.getFullName(), actualProduct.getFullName());
58                 assertEquals(expectedProduct.getDescription(), actualProduct.getDescription());
59
60                 List<String> expectedContacts = expectedProduct.getContacts();
61                 List<String> actualContacts = actualProduct.getContacts();
62                 assertTrue(
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));
67
68                 List<String> expectedTags = expectedProduct.getTags();
69                 List<String> actualTags = actualProduct.getTags();
70                 assertTrue(
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));
75
76                 assertEquals(expectedProduct.getLifecycleState(), actualProduct.getLifecycleState());
77                 assertEquals(expectedProduct.getVersion(), actualProduct.getVersion());
78                 assertEquals(expectedProduct.isHighestVersion(), actualProduct.isHighestVersion());
79                 assertEquals(expectedProduct.getNormalizedName(), actualProduct.getNormalizedName());
80
81                 compareCategories(expectedProduct, actualProduct);
82                 assertEquals(expectedProduct.getLastUpdaterUserId(), actualProduct.getLastUpdaterUserId());
83                 if (operation != null) {
84                         assertEquals(expectedProduct.getCreatorUserId(), actualProduct.getCreatorUserId());
85                 }
86
87                 Long lastUpdateDate = actualProduct.getLastUpdateDate();
88                 Long creationDate = actualProduct.getCreationDate();
89                 Map<String, String> allVersions = actualProduct.getAllVersions();
90
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);
98                         } else {
99                                 assertTrue("Last update date:" + lastUpdateDate + ", creation date: " + creationDate,
100                                                 lastUpdateDate > 0 && lastUpdateDate.equals(creationDate));
101                         }
102                 }
103
104                 // Check UUIDs
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));
114                 } else {
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));
122                         }
123                         assertEquals(expectedProduct.getUUID(), actualProduct.getUUID());
124                         assertEquals(expectedProduct.getInvariantUUID(), actualProduct.getInvariantUUID());
125                 }
126         }
127
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();
134
135                         assertTrue("Expected size:" + expSize + ", actual size:" + actSize, expSize == actSize);
136
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);
152                                         }
153                                 }
154                         }
155
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);
172                                         }
173                                 }
174                         }
175                 }
176         }
177
178         public static void verifyProductsNotExistInUserFollowedPage(User user, Product... nonExpectedProducts)
179                         throws Exception {
180                 String component = "products";
181                 Boolean isExist;
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
186                                                                                         // page
187                         for (int i = 0; i < nonExpectedProducts.length; i++) {
188                                 nonExpectedProduct = nonExpectedProducts[i];
189                                 isExist = false;
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())) {
193                                                 isExist = true;
194                                                 k = followedProductes.size();
195                                         }
196                                 }
197                                 assertFalse(isExist);
198                         }
199                 }
200
201         }
202
203         public static void checkUserFollowedPage(User user, Product... expectedProducts) throws Exception {
204                 String component = "products";
205                 Boolean isExist;
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];
214                         isExist = false;
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())) {
219
220                                         String productString = jobject.toJSONString();
221                                         Product actualProduct = ResponseParser.parseToObjectUsingMapper(productString, Product.class);
222                                         ProductValidationUtils.compareExpectedAndActualProducts(expectedProduct, actualProduct, null);
223                                         isExist = true;
224                                         k = followedProductes.size();
225                                 }
226                         }
227                         assertTrue(isExist);
228                 }
229         }
230
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);
235                 return resources;
236         }
237
238 }