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.lang3.tuple.ImmutablePair;
39 import org.apache.commons.lang3.tuple.ImmutableTriple;
40 import org.apache.log4j.Logger;
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.AtomicOperationUtils;
61 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
62 import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
63 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
64 import org.openecomp.sdc.ci.tests.utils.rest.CatalogRestUtils;
65 import org.openecomp.sdc.ci.tests.utils.rest.CategoryRestUtils;
66 import org.openecomp.sdc.ci.tests.utils.rest.ProductRestUtils;
67 import org.openecomp.sdc.ci.tests.utils.rest.ResourceRestUtils;
68 import org.openecomp.sdc.ci.tests.utils.rest.ResponseParser;
69 import org.openecomp.sdc.ci.tests.utils.rest.ServiceRestUtils;
70 import org.slf4j.LoggerFactory;
71 import org.testng.ITestContext;
72 import org.testng.ITestResult;
73 import org.testng.annotations.*;
75 import com.aventstack.extentreports.ExtentTest;
76 import com.aventstack.extentreports.Status;
77 import com.thinkaurelius.titan.core.TitanFactory;
78 import com.thinkaurelius.titan.core.TitanGraph;
79 import com.thinkaurelius.titan.core.TitanVertex;
81 import ch.qos.logback.classic.Level;
82 import ch.qos.logback.classic.LoggerContext;
84 public abstract class ComponentBaseTest {
86 // private static Logger logger = LoggerFactory.getLogger(ComponentBaseTest.class.getName());
88 protected static Logger logger= Logger.getLogger(ComponentBaseTest.class);
91 // public ComponentBaseTest(TestName testName, String className) {
92 // super(testName, className);
95 protected static final String REPORT_FOLDER = "." + File.separator + "ExtentReport" + File.separator;
96 private static final String VERSIONS_INFO_FILE_NAME = "versions.info";
97 private static final String REPORT_FILE_NAME = "SDC_CI_Extent_Report.html";
98 protected static TitanGraph titanGraph;
99 public static Config config;
100 protected static ITestContext myContext;
104 /**************** METHODS ****************/
105 public static ExtentTest getExtendTest() {
106 SomeInterface testManager = new ExtentTestManager();
107 return testManager.getTest();
110 public static enum ComponentOperationEnum {
111 CREATE_COMPONENT, UPDATE_COMPONENT, GET_COMPONENT, DELETE_COMPONENT, CHANGE_STATE_CHECKIN, CHANGE_STATE_CHECKOUT, CHANGE_STATE_UNDO_CHECKOUT
114 public ComponentBaseTest(TestName name, String name2) {
115 LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
116 lc.getLogger("com.thinkaurelius").setLevel(Level.INFO);
117 lc.getLogger("com.datastax").setLevel(Level.INFO);
118 lc.getLogger("io.netty").setLevel(Level.INFO);
119 lc.getLogger("c.d").setLevel(Level.INFO);
122 public static String getReportFolder() {
123 return REPORT_FOLDER;
126 @BeforeSuite(alwaysRun = true)
127 public void setupBeforeSuite(ITestContext context) throws Exception {
128 config = Utils.getConfig();
130 ExtentManager.initReporter(getReportFolder(), REPORT_FILE_NAME, context);
131 AtomicOperationUtils.createDefaultConsumer(true);
139 @BeforeMethod(alwaysRun = true)
140 public void setupBeforeTest(java.lang.reflect.Method method, ITestContext context) throws Exception {
142 System.out.println(" method.getName() " + method.getName());
143 if (!method.getName().equals("onboardVNFShotFlow")) {
144 // System.out.println("ExtentReport instance started from BeforeMethod...");
145 logger.info("ExtentReport instance started from BeforeMethod...");
146 ExtentTestManager.startTest(method.getName());
147 ExtentTestManager.assignCategory(this.getClass());
150 // System.out.println("ExtentReport instance started from Test...");
151 logger.debug("ExtentReport instance started from Test...");
156 @AfterMethod(alwaysRun = true)
157 public void quitAfterTest(ITestResult result, ITestContext context) throws Exception {
159 String testName = result.getName();
160 Throwable throwable = result.getThrowable();
161 int status = result.getStatus();
164 case ITestResult.SUCCESS:
165 getExtendTest().log(Status.PASS, "Test Result : <span class='label success'>Success</span>");
168 case ITestResult.FAILURE:
169 getExtendTest().log(Status.ERROR, "ERROR - The following exepction occured");
170 getExtendTest().log(Status.ERROR, result.getThrowable());
171 getExtendTest().log(Status.FAIL, "<span class='label failure'>Failure</span>");
174 case ITestResult.SKIP:
175 getExtendTest().log(Status.SKIP, "SKIP - The following exepction occured");
182 ExtentTestManager.endTest();
187 /* @AfterClass(alwaysRun = true)
188 public synchronized static void cleanAfterClass() throws Exception{
190 // System.out.println("<<<<<<<<class name>>>>>"+method.getDeclaringClass());
191 // System.out.println("<<<<<<<<class name>>>>>"+method.getName());
194 System.out.println("delete components AfterClass");
195 if(!config.getSystemUnderDebug()){
196 deleteCreatedComponents(getCatalogAsMap());
197 // CassandraUtils.truncateAllKeyspaces();
199 System.out.println("Accordindig to configuration components will not be deleted, in case to unable option to delete, please change systemUnderDebug parameter value to false ...");
201 // extentReport.flush();
205 @AfterSuite(alwaysRun = true)
206 public static void shutdownTitan() throws Exception {
209 // shutdownTitanLogic();
212 protected static void openTitanLogic() throws Exception {
214 logger.trace(config.toString());
215 String titanConfigFilePath = config.getTitanPropertiesFile();
216 titanGraph = TitanFactory.open(titanConfigFilePath);
217 assertNotNull(titanGraph);
222 protected static void shutdownTitanLogic() {
223 if (titanGraph.isOpen()) {
226 CassandraUtils.close();
231 public void setLog(String fromDataProvider) {
233 String suiteName = ExtentManager.getSuiteName(myContext);
234 ExtentTestManager.startTest(Thread.currentThread().getStackTrace()[2].getMethodName() + " " + fromDataProvider);
235 ExtentTestManager.assignCategory(this.getClass());
239 protected static void performClean() throws Exception, FileNotFoundException {
240 // cleanComponents();
241 if(!config.getSystemUnderDebug()){
242 deleteCreatedComponents(getCatalogAsMap());
243 // CassandraUtils.truncateAllKeyspaces();
244 FileHandling.overWriteExistindDir("outputCsar");
246 System.out.println("Accordindig to configuration components will not be deleted, in case to unable option to delete, please change systemUnderDebug parameter value to false ...");
250 public void verifyErrorCode(RestResponse response, String action, int expectedCode) {
251 assertNotNull("check response object is not null after " + action, response);
252 assertNotNull("check error code exists in response after " + action, response.getErrorCode());
253 assertEquals("Check response code after + action" + action, expectedCode, response.getErrorCode().intValue());
256 private static void cleanComponents() throws Exception {
258 // Components to delete
259 List<String> vfResourcesToDelete = new ArrayList<String>();
260 List<String> nonVfResourcesToDelete = new ArrayList<String>();
261 List<String> servicesToDelete = new ArrayList<String>();
262 List<String> productsToDelete = new ArrayList<String>();
264 // Categories to delete
265 List<ImmutableTriple<String, String, String>> productGroupingsToDelete = new ArrayList<>();
266 List<ImmutablePair<String, String>> productSubsToDelete = new ArrayList<>();
267 List<ImmutablePair<String, String>> resourceSubsToDelete = new ArrayList<>();
268 List<String> productCategoriesToDelete = new ArrayList<>();
269 List<String> resourceCategoriesToDelete = new ArrayList<String>();
270 List<String> serviceCategoriesToDelete = new ArrayList<String>();
272 List<String> resourcesNotToDelete = config.getResourcesNotToDelete();
273 List<String> resourceCategoriesNotToDelete = config.getResourceCategoriesNotToDelete();
274 List<String> serviceCategoriesNotToDelete = config.getServiceCategoriesNotToDelete();
276 Iterable<TitanVertex> vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.Resource.getName()).vertices();
277 if (vertices != null) {
278 Iterator<TitanVertex> iter = vertices.iterator();
279 while (iter.hasNext()) {
280 Vertex vertex = iter.next();
281 Boolean isAbstract = vertex.value(GraphPropertiesDictionary.IS_ABSTRACT.getProperty());
282 // if (!isAbstract) {
283 String name = vertex.value(GraphPropertiesDictionary.NAME.getProperty());
284 String version = vertex.value(GraphPropertiesDictionary.VERSION.getProperty());
286 if ((resourcesNotToDelete != null && !resourcesNotToDelete.contains(name)) || (version != null && !version.equals("1.0"))) {
287 String id = vertex.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
288 String resourceType = vertex.value(GraphPropertiesDictionary.RESOURCE_TYPE.getProperty());
289 if (name.startsWith("ci")) {
290 if (resourceType.equals(ResourceTypeEnum.VF.name())) {
291 vfResourcesToDelete.add(id);
293 nonVfResourcesToDelete.add(id);
296 } else if ((resourcesNotToDelete != null && !resourcesNotToDelete.contains(name)) || (version != null && version.equals("1.0"))) {
297 if ((boolean) vertex.value(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty()) == false) {
298 vertex.property(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty(), true);
304 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.Service.getName()).vertices();
305 if (vertices != null) {
306 Iterator<TitanVertex> iter = vertices.iterator();
307 while (iter.hasNext()) {
308 Vertex vertex = iter.next();
309 String id = vertex.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
310 String name = vertex.value(GraphPropertiesDictionary.NAME.getProperty());
311 if (name.startsWith("ci")) {
312 servicesToDelete.add(id);
317 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.Product.getName()).vertices();
318 if (vertices != null) {
319 Iterator<TitanVertex> iter = vertices.iterator();
320 while (iter.hasNext()) {
321 Vertex vertex = iter.next();
322 String id = vertex.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
323 String name = vertex.value(GraphPropertiesDictionary.NAME.getProperty());
324 if (name.startsWith("Ci")) {
325 productsToDelete.add(id);
330 // Getting categories
332 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.ResourceNewCategory.getName()).vertices();
333 if (vertices != null) {
334 Iterator<TitanVertex> iter = vertices.iterator();
335 while (iter.hasNext()) {
336 Vertex category = iter.next();
337 String name = category.value(GraphPropertiesDictionary.NAME.getProperty());
338 if (!resourceCategoriesNotToDelete.contains(name)) {
339 String catId = category.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
340 resourceCategoriesToDelete.add(catId);
341 Iterator<Vertex> subs = category.vertices(Direction.OUT, GraphEdgeLabels.SUB_CATEGORY.getProperty());
342 while (subs.hasNext()) {
343 Vertex sub = subs.next();
344 String subCatId = sub.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
345 resourceSubsToDelete.add(new ImmutablePair<String, String>(catId, subCatId));
351 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.ServiceNewCategory.getName()).vertices();
352 if (vertices != null) {
353 Iterator<TitanVertex> iter = vertices.iterator();
354 while (iter.hasNext()) {
355 Vertex category = iter.next();
356 String name = category.value(GraphPropertiesDictionary.NAME.getProperty());
357 if (!serviceCategoriesNotToDelete.contains(name)) {
358 String id = category.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
359 serviceCategoriesToDelete.add(id);
364 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.ProductCategory.getName()).vertices();
365 if (vertices != null) {
366 Iterator<TitanVertex> iter = vertices.iterator();
367 while (iter.hasNext()) {
368 Vertex category = iter.next();
369 String catId = category.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
370 productCategoriesToDelete.add(catId);
371 Iterator<Vertex> subs = category.vertices(Direction.OUT, GraphEdgeLabels.SUB_CATEGORY.getProperty());
372 while (subs.hasNext()) {
373 Vertex sub = subs.next();
374 String subCatId = sub.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
375 productSubsToDelete.add(new ImmutablePair<String, String>(catId, subCatId));
376 Iterator<Vertex> groupings = sub.vertices(Direction.OUT, GraphEdgeLabels.GROUPING.getProperty());
377 while (groupings.hasNext()) {
378 Vertex grouping = groupings.next();
379 String groupId = grouping.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
380 productGroupingsToDelete.add(new ImmutableTriple<String, String, String>(catId, subCatId, groupId));
387 titanGraph.tx().commit();
389 String adminId = UserRoleEnum.ADMIN.getUserId();
390 /* String productStrategistId = UserRoleEnum.PRODUCT_STRATEGIST1.getUserId();*/
394 /* for (String id : productsToDelete) {
395 RestResponse deleteProduct = ProductRestUtils.deleteProduct(id, productStrategistId);
398 for (String id : servicesToDelete) {
399 RestResponse deleteServiceById = ServiceRestUtils.deleteServiceById(id, adminId);
402 for (String id : vfResourcesToDelete) {
403 RestResponse deleteResource = ResourceRestUtils.deleteResource(id, adminId);
407 for (String id : nonVfResourcesToDelete) {
408 RestResponse deleteResource = ResourceRestUtils.deleteResource(id, adminId);
412 // Categories delete - product
413 /*String componentType = BaseRestUtils.PRODUCT_COMPONENT_TYPE;
414 for (ImmutableTriple<String, String, String> triple : productGroupingsToDelete) {
415 CategoryRestUtils.deleteGrouping(triple.getRight(), triple.getMiddle(), triple.getLeft(), productStrategistId, componentType);
417 for (ImmutablePair<String, String> pair : productSubsToDelete) {
418 CategoryRestUtils.deleteSubCategory(pair.getRight(), pair.getLeft(), productStrategistId, componentType);
420 for (String id : productCategoriesToDelete) {
421 CategoryRestUtils.deleteCategory(id, productStrategistId, componentType);
424 // Categories delete - resource
425 String componentType = BaseRestUtils.RESOURCE_COMPONENT_TYPE;
426 for (ImmutablePair<String, String> pair : resourceSubsToDelete) {
427 CategoryRestUtils.deleteSubCategory(pair.getRight(), pair.getLeft(), adminId, componentType);
429 for (String id : resourceCategoriesToDelete) {
430 CategoryRestUtils.deleteCategory(id, adminId, componentType);
432 // Categories delete - resource
433 componentType = BaseRestUtils.SERVICE_COMPONENT_TYPE;
434 for (String id : serviceCategoriesToDelete) {
435 CategoryRestUtils.deleteCategory(id, adminId, componentType);
440 private static void deleteCreatedComponents(Map<String, List<Component>> convertCatalogResponseToJavaObject) throws IOException {
441 final String userId = UserRoleEnum.DESIGNER.getUserId();
443 List<Component> resourcesArrayList = convertCatalogResponseToJavaObject.get(ComponentTypeEnum.PRODUCT_PARAM_NAME);
444 if (resourcesArrayList.size() > 0) {
445 List<String> collect = buildCollectionUniqueId(resourcesArrayList);
446 for (String uId : collect) {
447 ProductRestUtils.deleteProduct(uId, userId);
452 resourcesArrayList = convertCatalogResponseToJavaObject.get(ComponentTypeEnum.SERVICE_PARAM_NAME);
453 if (resourcesArrayList.size() > 0) {
454 List<String> collect = buildCollectionUniqueId(resourcesArrayList);
455 for (String uId : collect) {
456 ServiceRestUtils.markServiceToDelete(uId, userId);
458 ServiceRestUtils.deleteMarkedServices(userId);
462 resourcesArrayList = convertCatalogResponseToJavaObject.get(ComponentTypeEnum.RESOURCE_PARAM_NAME);
464 // List<String> collect = resourcesArrayList.stream().filter(s ->
465 // s.getName().startsWith("ci")).map(e ->
466 // e.getUniqueId()).collect(Collectors.toList());
468 // List<Map<String, String>> collect =
469 // resourcesArrayList.stream().filter(s ->
470 // s.getName().startsWith("ci")).map(e ->
471 // e.getAllVersions()).collect(Collectors.toList());
473 * List<String> collect = resourcesArrayList.stream().filter(s -> s.getName().startsWith("ci")) .flatMap(e -> e.getAllVersions().values().stream()).collect(Collectors.toList());
476 if (!CollectionUtils.isEmpty(resourcesArrayList)) {
477 List<String> collect = buildCollectionUniqueId(resourcesArrayList);
478 for (String uId : collect) {
479 ResourceRestUtils.markResourceToDelete(uId, userId);
481 ResourceRestUtils.deleteMarkedResources(userId);
488 private void deleteCollection(List<Component> componentArrayList, Consumer<String> deleteHandler) {
490 if (componentArrayList.size() > 0) {
491 List<String> collect = buildCollectionUniqueId(componentArrayList);
492 for (String uId : collect) {
493 deleteHandler.accept(uId);
494 // ProductRestUtils.deleteProduct(uId, userId);
499 protected static List<String> buildCollectionUniqueId(List<Component> resourcesArrayList) {
501 // Stream<String> flatMap = resourcesArrayList.stream().filter(s ->
502 // s.getName().startsWith("ci")).map(e -> e.getAllVersions()).map( e ->
503 // e.values()).flatMap( e -> e.stream());
505 // List<String> collect = resourcesArrayList.stream()
507 // .filter(s -> s.getName().startsWith("ci") )
509 // .map(e -> e.getUniqueId())
511 // .map( e -> e.values())
512 // .filter(out -> out!=null )
513 // .flatMap( e -> e.stream())
514 // .collect(Collectors.toList());
516 // List<String> collect = resourcesArrayList.stream().filter(s ->
517 // s.getName().startsWith("ci"))
519 // e.getAllVersions().values().stream()).collect(Collectors.toList());
520 ComponentTypeEnum componentTypeEnum = resourcesArrayList.get(0).getComponentType();
522 List<String> genericCollection = new ArrayList<String>();
524 resourcesArrayList.stream().filter(s -> s.getName().toLowerCase().startsWith("ci") && !s.getName().toLowerCase().equals("cindervolume")).map(e -> e.getUniqueId()).collect(Collectors.toList()).forEach((i) -> {
525 buildCollectionBaseOnComponentType(componentTypeEnum, genericCollection, i);
531 // List<String> collect =
532 // genericCollection.stream().collect(Collectors.toList());
534 return genericCollection;
537 public static void buildCollectionBaseOnComponentType(ComponentTypeEnum componentTypeEnum,
538 List<String> genericCollection, String i) {
540 switch (componentTypeEnum) {
542 RestResponse resource = ResourceRestUtils.getResource(i);
543 Resource convertResourceResponseToJavaObject = ResponseParser.convertResourceResponseToJavaObject(resource.getResponse());
544 Map<String, String> allVersions = convertResourceResponseToJavaObject.getAllVersions();
545 Collection<String> values = allVersions.values();
546 genericCollection.addAll(values);
550 RestResponse service = ServiceRestUtils.getService(i);
551 Service convertServiceResponseToJavaObject = ResponseParser.convertServiceResponseToJavaObject(service.getResponse());
552 allVersions = convertServiceResponseToJavaObject.getAllVersions();
553 values = allVersions.values();
554 genericCollection.addAll(values);
560 RestResponse product = ProductRestUtils.getProduct(i);
561 Product convertProductResponseToJavaObject = ResponseParser.convertProductResponseToJavaObject(product.getResponse());
562 allVersions = convertProductResponseToJavaObject.getAllVersions();
563 values = allVersions.values();
564 genericCollection.addAll(values);
571 } catch (Exception e1) {
572 // TODO Auto-generated catch block
573 e1.printStackTrace();
577 protected static Map<String, List<Component>> getCatalogAsMap() throws Exception {
578 RestResponse catalog = CatalogRestUtils.getCatalog(UserRoleEnum.DESIGNER.getUserId());
579 Map<String, List<Component>> convertCatalogResponseToJavaObject = ResponseParser.convertCatalogResponseToJavaObject(catalog.getResponse());
580 return convertCatalogResponseToJavaObject;
582 protected Resource createVfFromCSAR(User sdncModifierDetails, String csarId) throws Exception {
583 // create new resource from Csar
584 ResourceReqDetails resourceDetails = ElementFactory.getDefaultResource();
586 resourceDetails.setCsarUUID(csarId);
587 resourceDetails.setResourceType(ResourceTypeEnum.VF.name());
588 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
589 BaseRestUtils.checkCreateResponse(createResource);
590 Resource createdResource = ResponseParser.convertResourceResponseToJavaObject(createResource.getResponse());
591 return createdResource;