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.api;
23 import static org.testng.AssertJUnit.assertEquals;
24 import static org.testng.AssertJUnit.assertNotNull;
27 import java.io.FileNotFoundException;
28 import java.io.IOException;
29 import java.util.ArrayList;
30 import java.util.Collection;
31 import java.util.Iterator;
32 import java.util.List;
34 import java.util.function.Consumer;
35 import java.util.stream.Collectors;
37 import org.apache.commons.collections.CollectionUtils;
38 import org.apache.commons.io.FileUtils;
39 import org.apache.commons.lang3.tuple.ImmutablePair;
40 import org.apache.commons.lang3.tuple.ImmutableTriple;
41 import org.apache.tinkerpop.gremlin.structure.Direction;
42 import org.apache.tinkerpop.gremlin.structure.Vertex;
43 import org.junit.rules.TestName;
44 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
45 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
46 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
47 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
48 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
49 import org.openecomp.sdc.be.model.Component;
50 import org.openecomp.sdc.be.model.Product;
51 import org.openecomp.sdc.be.model.Resource;
52 import org.openecomp.sdc.be.model.Service;
53 import org.openecomp.sdc.be.model.User;
54 import org.openecomp.sdc.ci.tests.config.Config;
55 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
56 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
57 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
58 import org.openecomp.sdc.ci.tests.utils.Utils;
59 import org.openecomp.sdc.ci.tests.utils.cassandra.CassandraUtils;
60 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
61 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
62 import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
63 import org.openecomp.sdc.ci.tests.utils.rest.CategoryRestUtils;
64 import org.openecomp.sdc.ci.tests.utils.rest.ProductRestUtils;
65 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
66 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
67 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
68 import org.slf4j.Logger;
69 import org.slf4j.LoggerFactory;
70 import org.testng.ITestResult;
71 import org.testng.annotations.AfterMethod;
72 import org.testng.annotations.AfterSuite;
73 import org.testng.annotations.BeforeMethod;
74 import org.testng.annotations.BeforeSuite;
76 import com.relevantcodes.extentreports.ExtentReports;
77 import com.relevantcodes.extentreports.ExtentTest;
78 import com.relevantcodes.extentreports.LogStatus;
79 import com.thinkaurelius.titan.core.TitanFactory;
80 import com.thinkaurelius.titan.core.TitanGraph;
81 import com.thinkaurelius.titan.core.TitanVertex;
83 import ch.qos.logback.classic.Level;
84 import ch.qos.logback.classic.LoggerContext;
86 public abstract class ComponentBaseTest {
88 private static Logger logger = LoggerFactory.getLogger(ComponentBaseTest.class.getName());
90 // public ComponentBaseTest(TestName testName, String className) {
91 // super(testName, className);
94 protected static ExtentReports extentReport;
95 protected static ExtentTest extendTest;
96 public static final String REPORT_FOLDER = "./ExtentReport/";
97 private static final String REPORT_FILE_NAME = "ASDC_CI_Extent_Report.html";
98 protected static TitanGraph titanGraph;
99 private static Config myconfig;
100 public static Config config;
102 public static enum ComponentOperationEnum {
103 CREATE_COMPONENT, UPDATE_COMPONENT, GET_COMPONENT, DELETE_COMPONENT, CHANGE_STATE_CHECKIN, CHANGE_STATE_CHECKOUT, CHANGE_STATE_UNDO_CHECKOUT
106 public ComponentBaseTest(TestName name, String name2) {
107 // TODO Auto-generated constructor stub
108 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
109 lc.getLogger("com.thinkaurelius").setLevel(Level.INFO);
110 lc.getLogger("com.datastax").setLevel(Level.INFO);
111 lc.getLogger("io.netty").setLevel(Level.INFO);
112 lc.getLogger("c.d").setLevel(Level.INFO);
115 @BeforeSuite(alwaysRun = true)
116 public static void openTitan() throws Exception {
118 File dir = new File(REPORT_FOLDER);
120 FileUtils.deleteDirectory(dir);
121 } catch (IOException e) {
123 extentReport = new ExtentReports(REPORT_FOLDER + REPORT_FILE_NAME);
124 // extentReport = new ExtentReports(REPORT_FOLDER +
125 // REPORT_FILE_NAME,true , NetworkMode.ONLINE);
131 protected static void openTitanLogic() throws Exception {
134 // DbUtils.deleteFromEsDbByPattern("_all");
135 CassandraUtils.truncateAllKeyspaces();
138 protected static void initGraph() throws FileNotFoundException {
139 myconfig = Utils.getConfig();
140 config = Utils.getConfig();
141 logger.trace(config.toString());
142 String titanConfigFilePath = myconfig.getTitanPropertiesFile();
143 titanGraph = TitanFactory.open(titanConfigFilePath);
144 assertNotNull(titanGraph);
147 @AfterSuite(alwaysRun = true)
148 public static void shutdownTitan() throws Exception {
149 shutdownTitanLogic();
150 extentReport.flush();
153 protected static void shutdownTitanLogic() {
154 if (titanGraph.isOpen()) {
157 CassandraUtils.close();
160 @BeforeMethod(alwaysRun = true)
161 public void beforeState(java.lang.reflect.Method method) throws Exception {
163 // deleteCreatedComponents(getCatalogAsMap());
166 extendTest = extentReport.startTest(method.getName());
167 extendTest.log(LogStatus.INFO, "Test started");
171 @AfterMethod(alwaysRun = true)
172 public void afterState(ITestResult result) throws Exception {
175 if (result.isSuccess()) {
176 extendTest.log(LogStatus.PASS, "Test Result : <span class='label success'>Success</span>");
178 extendTest.log(LogStatus.ERROR, "ERROR - The following exepction occured");
179 extendTest.log(LogStatus.ERROR, result.getThrowable());
180 extendTest.log(LogStatus.FAIL, "<span class='label failure'>Failure</span>");
183 extentReport.endTest(extendTest);
187 protected void performeClean() throws Exception, FileNotFoundException {
189 CassandraUtils.truncateAllKeyspaces();
192 public void verifyErrorCode(RestResponse response, String action, int expectedCode) {
193 assertNotNull("check response object is not null after " + action, response);
194 assertNotNull("check error code exists in response after " + action, response.getErrorCode());
195 assertEquals("Check response code after + action" + action, expectedCode, response.getErrorCode().intValue());
198 private static void cleanComponents() throws Exception {
200 // Components to delete
201 List<String> vfResourcesToDelete = new ArrayList<String>();
202 List<String> nonVfResourcesToDelete = new ArrayList<String>();
203 List<String> servicesToDelete = new ArrayList<String>();
204 List<String> productsToDelete = new ArrayList<String>();
206 // Categories to delete
207 List<ImmutableTriple<String, String, String>> productGroupingsToDelete = new ArrayList<>();
208 List<ImmutablePair<String, String>> productSubsToDelete = new ArrayList<>();
209 List<ImmutablePair<String, String>> resourceSubsToDelete = new ArrayList<>();
210 List<String> productCategoriesToDelete = new ArrayList<>();
211 List<String> resourceCategoriesToDelete = new ArrayList<String>();
212 List<String> serviceCategoriesToDelete = new ArrayList<String>();
214 List<String> resourcesNotToDelete = config.getResourcesNotToDelete();
215 List<String> resourceCategoriesNotToDelete = config.getResourceCategoriesNotToDelete();
216 List<String> serviceCategoriesNotToDelete = config.getServiceCategoriesNotToDelete();
218 Iterable<TitanVertex> vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.Resource.getName()).vertices();
219 if (vertices != null) {
220 Iterator<TitanVertex> iter = vertices.iterator();
221 while (iter.hasNext()) {
222 Vertex vertex = iter.next();
223 Boolean isAbstract = vertex.value(GraphPropertiesDictionary.IS_ABSTRACT.getProperty());
224 // if (!isAbstract) {
225 String name = vertex.value(GraphPropertiesDictionary.NAME.getProperty());
226 String version = vertex.value(GraphPropertiesDictionary.VERSION.getProperty());
228 if ((resourcesNotToDelete != null && !resourcesNotToDelete.contains(name)) || (version != null && !version.equals("1.0"))) {
229 String id = vertex.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
230 String resourceType = vertex.value(GraphPropertiesDictionary.RESOURCE_TYPE.getProperty());
231 if (name.startsWith("ci")) {
232 if (resourceType.equals(ResourceTypeEnum.VF.name())) {
233 vfResourcesToDelete.add(id);
235 nonVfResourcesToDelete.add(id);
238 } else if ((resourcesNotToDelete != null && !resourcesNotToDelete.contains(name)) || (version != null && version.equals("1.0"))) {
239 if ((boolean) vertex.value(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty()) == false) {
240 vertex.property(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty(), true);
246 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.Service.getName()).vertices();
247 if (vertices != null) {
248 Iterator<TitanVertex> iter = vertices.iterator();
249 while (iter.hasNext()) {
250 Vertex vertex = iter.next();
251 String id = vertex.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
252 String name = vertex.value(GraphPropertiesDictionary.NAME.getProperty());
253 if (name.startsWith("ci")) {
254 servicesToDelete.add(id);
259 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.Product.getName()).vertices();
260 if (vertices != null) {
261 Iterator<TitanVertex> iter = vertices.iterator();
262 while (iter.hasNext()) {
263 Vertex vertex = iter.next();
264 String id = vertex.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
265 String name = vertex.value(GraphPropertiesDictionary.NAME.getProperty());
266 if (name.startsWith("Ci")) {
267 productsToDelete.add(id);
272 // Getting categories
274 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.ResourceNewCategory.getName()).vertices();
275 if (vertices != null) {
276 Iterator<TitanVertex> iter = vertices.iterator();
277 while (iter.hasNext()) {
278 Vertex category = iter.next();
279 String name = category.value(GraphPropertiesDictionary.NAME.getProperty());
280 if (!resourceCategoriesNotToDelete.contains(name)) {
281 String catId = category.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
282 resourceCategoriesToDelete.add(catId);
283 Iterator<Vertex> subs = category.vertices(Direction.OUT, GraphEdgeLabels.SUB_CATEGORY.getProperty());
284 while (subs.hasNext()) {
285 Vertex sub = subs.next();
286 String subCatId = sub.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
287 resourceSubsToDelete.add(new ImmutablePair<String, String>(catId, subCatId));
293 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.ServiceNewCategory.getName()).vertices();
294 if (vertices != null) {
295 Iterator<TitanVertex> iter = vertices.iterator();
296 while (iter.hasNext()) {
297 Vertex category = iter.next();
298 String name = category.value(GraphPropertiesDictionary.NAME.getProperty());
299 if (!serviceCategoriesNotToDelete.contains(name)) {
300 String id = category.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
301 serviceCategoriesToDelete.add(id);
306 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.ProductCategory.getName()).vertices();
307 if (vertices != null) {
308 Iterator<TitanVertex> iter = vertices.iterator();
309 while (iter.hasNext()) {
310 Vertex category = iter.next();
311 String catId = category.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
312 productCategoriesToDelete.add(catId);
313 Iterator<Vertex> subs = category.vertices(Direction.OUT, GraphEdgeLabels.SUB_CATEGORY.getProperty());
314 while (subs.hasNext()) {
315 Vertex sub = subs.next();
316 String subCatId = sub.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
317 productSubsToDelete.add(new ImmutablePair<String, String>(catId, subCatId));
318 Iterator<Vertex> groupings = sub.vertices(Direction.OUT, GraphEdgeLabels.GROUPING.getProperty());
319 while (groupings.hasNext()) {
320 Vertex grouping = groupings.next();
321 String groupId = grouping.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
322 productGroupingsToDelete.add(new ImmutableTriple<String, String, String>(catId, subCatId, groupId));
329 titanGraph.tx().commit();
331 String adminId = UserRoleEnum.ADMIN.getUserId();
332 String productStrategistId = UserRoleEnum.PRODUCT_STRATEGIST1.getUserId();
335 for (String id : productsToDelete) {
336 RestResponse deleteProduct = ProductRestUtils.deleteProduct(id, productStrategistId);
339 for (String id : servicesToDelete) {
340 RestResponse deleteServiceById = ServiceRestUtils.deleteServiceById(id, adminId);
343 for (String id : vfResourcesToDelete) {
344 RestResponse deleteResource = ResourceRestUtils.deleteResource(id, adminId);
348 for (String id : nonVfResourcesToDelete) {
349 RestResponse deleteResource = ResourceRestUtils.deleteResource(id, adminId);
353 // Categories delete - product
354 String componentType = BaseRestUtils.PRODUCT_COMPONENT_TYPE;
355 for (ImmutableTriple<String, String, String> triple : productGroupingsToDelete) {
356 CategoryRestUtils.deleteGrouping(triple.getRight(), triple.getMiddle(), triple.getLeft(), productStrategistId, componentType);
358 for (ImmutablePair<String, String> pair : productSubsToDelete) {
359 CategoryRestUtils.deleteSubCategory(pair.getRight(), pair.getLeft(), productStrategistId, componentType);
361 for (String id : productCategoriesToDelete) {
362 CategoryRestUtils.deleteCategory(id, productStrategistId, componentType);
365 // Categories delete - resource
366 componentType = BaseRestUtils.RESOURCE_COMPONENT_TYPE;
367 for (ImmutablePair<String, String> pair : resourceSubsToDelete) {
368 CategoryRestUtils.deleteSubCategory(pair.getRight(), pair.getLeft(), adminId, componentType);
370 for (String id : resourceCategoriesToDelete) {
371 CategoryRestUtils.deleteCategory(id, adminId, componentType);
373 // Categories delete - resource
374 componentType = BaseRestUtils.SERVICE_COMPONENT_TYPE;
375 for (String id : serviceCategoriesToDelete) {
376 CategoryRestUtils.deleteCategory(id, adminId, componentType);
381 private void deleteCreatedComponents(Map<String, List<Component>> convertCatalogResponseToJavaObject) throws IOException {
382 final String userId = UserRoleEnum.DESIGNER.getUserId();
383 List<Component> resourcesArrayList = convertCatalogResponseToJavaObject.get(ComponentTypeEnum.RESOURCE_PARAM_NAME);
385 // List<String> collect = resourcesArrayList.stream().filter(s ->
386 // s.getName().startsWith("ci")).map(e ->
387 // e.getUniqueId()).collect(Collectors.toList());
389 // List<Map<String, String>> collect =
390 // resourcesArrayList.stream().filter(s ->
391 // s.getName().startsWith("ci")).map(e ->
392 // e.getAllVersions()).collect(Collectors.toList());
394 * List<String> collect = resourcesArrayList.stream().filter(s -> s.getName().startsWith("ci")) .flatMap(e -> e.getAllVersions().values().stream()).collect(Collectors.toList());
397 if (!CollectionUtils.isEmpty(resourcesArrayList)) {
398 List<String> collect = buildCollectionUniqueId(resourcesArrayList);
399 for (String uId : collect) {
400 ResourceRestUtils.deleteResource(uId, userId);
404 resourcesArrayList = convertCatalogResponseToJavaObject.get(ComponentTypeEnum.SERVICE_PARAM_NAME);
405 if (resourcesArrayList.size() > 0) {
406 List<String> collect = buildCollectionUniqueId(resourcesArrayList);
407 for (String uId : collect) {
408 ServiceRestUtils.deleteServiceById(uId, userId);
411 resourcesArrayList = convertCatalogResponseToJavaObject.get(ComponentTypeEnum.PRODUCT_PARAM_NAME);
412 if (resourcesArrayList.size() > 0) {
413 List<String> collect = buildCollectionUniqueId(resourcesArrayList);
414 for (String uId : collect) {
415 ProductRestUtils.deleteProduct(uId, userId);
421 private void deleteCollection(List<Component> componentArrayList, Consumer<String> deleteHandler) {
423 if (componentArrayList.size() > 0) {
424 List<String> collect = buildCollectionUniqueId(componentArrayList);
425 for (String uId : collect) {
426 deleteHandler.accept(uId);
427 // ProductRestUtils.deleteProduct(uId, userId);
432 private List<String> buildCollectionUniqueId(List<Component> resourcesArrayList) {
434 // Stream<String> flatMap = resourcesArrayList.stream().filter(s ->
435 // s.getName().startsWith("ci")).map(e -> e.getAllVersions()).map( e ->
436 // e.values()).flatMap( e -> e.stream());
438 // List<String> collect = resourcesArrayList.stream()
440 // .filter(s -> s.getName().startsWith("ci") )
442 // .map(e -> e.getUniqueId())
444 // .map( e -> e.values())
445 // .filter(out -> out!=null )
446 // .flatMap( e -> e.stream())
447 // .collect(Collectors.toList());
449 // List<String> collect = resourcesArrayList.stream().filter(s ->
450 // s.getName().startsWith("ci"))
452 // e.getAllVersions().values().stream()).collect(Collectors.toList());
453 ComponentTypeEnum componentTypeEnum = resourcesArrayList.get(0).getComponentType();
455 List<String> genericCollection = new ArrayList<String>();
456 resourcesArrayList.stream().filter(s -> s.getName().toLowerCase().startsWith("ci")).map(e -> e.getUniqueId()).collect(Collectors.toList()).forEach((i) -> {
458 switch (componentTypeEnum) {
460 RestResponse resource = ResourceRestUtils.getResource(i);
461 Resource convertResourceResponseToJavaObject = ResponseParser.convertResourceResponseToJavaObject(resource.getResponse());
462 Map<String, String> allVersions = convertResourceResponseToJavaObject.getAllVersions();
463 Collection<String> values = allVersions.values();
464 genericCollection.addAll(values);
468 RestResponse service = ServiceRestUtils.getService(i);
469 Service convertServiceResponseToJavaObject = ResponseParser.convertServiceResponseToJavaObject(service.getResponse());
470 allVersions = convertServiceResponseToJavaObject.getAllVersions();
471 values = allVersions.values();
472 genericCollection.addAll(values);
478 RestResponse product = ProductRestUtils.getProduct(i);
479 Product convertProductResponseToJavaObject = ResponseParser.convertProductResponseToJavaObject(product.getResponse());
480 allVersions = convertProductResponseToJavaObject.getAllVersions();
481 values = allVersions.values();
482 genericCollection.addAll(values);
489 } catch (Exception e1) {
490 // TODO Auto-generated catch block
491 e1.printStackTrace();
496 // List<String> collect =
497 // genericCollection.stream().collect(Collectors.toList());
499 return genericCollection;
502 private Map<String, List<Component>> getCatalogAsMap() throws Exception {
503 RestResponse catalog = CatalogRestUtils.getCatalog(UserRoleEnum.DESIGNER.getUserId());
504 Map<String, List<Component>> convertCatalogResponseToJavaObject = ResponseParser.convertCatalogResponseToJavaObject(catalog.getResponse());
505 return convertCatalogResponseToJavaObject;
507 protected Resource createVfFromCSAR(User sdncModifierDetails, String csarId) throws Exception {
508 // create new resource from Csar
509 ResourceReqDetails resourceDetails = ElementFactory.getDefaultResource();
511 resourceDetails.setCsarUUID(csarId);
512 resourceDetails.setResourceType(ResourceTypeEnum.VF.name());
513 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
514 BaseRestUtils.checkCreateResponse(createResource);
515 Resource createdResource = ResponseParser.convertResourceResponseToJavaObject(createResource.getResponse());
516 return createdResource;