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 com.aventstack.extentreports.ExtentTest;
24 import com.aventstack.extentreports.Status;
25 import com.thinkaurelius.titan.core.TitanFactory;
26 import com.thinkaurelius.titan.core.TitanGraph;
27 import com.thinkaurelius.titan.core.TitanVertex;
28 import org.apache.commons.collections.CollectionUtils;
29 import org.apache.commons.io.FileUtils;
30 import org.apache.commons.lang3.tuple.ImmutablePair;
31 import org.apache.commons.lang3.tuple.ImmutableTriple;
32 import org.apache.tinkerpop.gremlin.structure.Direction;
33 import org.apache.tinkerpop.gremlin.structure.Vertex;
34 import org.junit.rules.TestName;
35 import org.openecomp.sdc.be.dao.neo4j.GraphEdgeLabels;
36 import org.openecomp.sdc.be.dao.neo4j.GraphPropertiesDictionary;
37 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
38 import org.openecomp.sdc.be.datatypes.enums.NodeTypeEnum;
39 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
40 import org.openecomp.sdc.be.model.*;
41 import org.openecomp.sdc.ci.tests.config.Config;
42 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
43 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
44 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
45 import org.openecomp.sdc.ci.tests.utils.Utils;
46 import org.openecomp.sdc.ci.tests.utils.cassandra.CassandraUtils;
47 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
48 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
49 import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
50 import org.openecomp.sdc.ci.tests.utils.rest.*;
51 import org.slf4j.Logger;
52 import org.slf4j.LoggerFactory;
53 import org.testng.ITestContext;
54 import org.testng.ITestResult;
55 import org.testng.annotations.*;
58 import java.io.FileNotFoundException;
59 import java.io.IOException;
61 import java.util.function.Consumer;
62 import java.util.stream.Collectors;
64 import static org.testng.AssertJUnit.assertEquals;
65 import static org.testng.AssertJUnit.assertNotNull;
70 public abstract class ComponentBaseTest {
72 // private static Logger logger = LoggerFactory.getLogger(ComponentBaseTest.class.getName());
73 protected static Logger logger= LoggerFactory.getLogger(ComponentBaseTest.class);
76 // public ComponentBaseTest(TestName testName, String className) {
77 // super(testName, className);
80 protected static final String REPORT_FOLDER = "." + File.separator + "ExtentReport" + File.separator;
81 private static final String VERSIONS_INFO_FILE_NAME = "versions.info";
82 private static final String REPORT_FILE_NAME = "SDC_CI_Extent_Report.html";
83 protected static TitanGraph titanGraph;
84 public static Config config;
85 protected static ITestContext myContext;
89 /**************** METHODS ****************/
90 public static ExtentTest getExtendTest() {
91 return ExtentTestManager.getTest();
94 public static enum ComponentOperationEnum {
95 CREATE_COMPONENT, UPDATE_COMPONENT, GET_COMPONENT, DELETE_COMPONENT, CHANGE_STATE_CHECKIN, CHANGE_STATE_CHECKOUT, CHANGE_STATE_UNDO_CHECKOUT
98 public ComponentBaseTest(TestName name, String name2) {
99 // TODO Auto-generated constructor stub
100 // LoggerContext lc = (LoggerContext) LoggerFactory.getILoggerFactory();
101 // lc.getLogger("com.thinkaurelius").setLevel(Level.INFO);
102 // lc.getLogger("com.datastax").setLevel(Level.INFO);
103 // lc.getLogger("io.netty").setLevel(Level.INFO);
104 // lc.getLogger("c.d").setLevel(Level.INFO);
105 // lc.getLogger("o.a.h").setLevel(Level.INFO);
106 // lc.getLogger("o.o.s.c.t.d.h").setLevel(Level.INFO);
110 public static String getReportFolder() {
111 return REPORT_FOLDER;
114 @BeforeSuite(alwaysRun = true)
115 public void setupBeforeSuite(ITestContext context) throws Exception {
116 config = Utils.getConfig();
118 ExtentManager.initReporter(getReportFolder(), REPORT_FILE_NAME, context);
119 AtomicOperationUtils.createDefaultConsumer(true);
123 String basePath = FileHandling.getBasePath();
124 String csarDir = FileHandling.getCreateDirByName("outputCsar");
125 FileUtils.cleanDirectory(new File(csarDir));
128 @BeforeMethod(alwaysRun = true)
129 public void setBrowserBeforeTest(java.lang.reflect.Method method, ITestContext context) throws Exception {
132 // String suiteName = ExtentManager.getSuiteName(context);
133 // ExtentTestManager.startTest(method.getName());
134 // ExtentTestManager.assignCategory(this.getClass());
136 boolean emptyDataProvider = method.getAnnotation(Test.class).dataProvider().isEmpty();
137 String className = method.getDeclaringClass().getName();
138 System.out.println(" method.getName() " + method.getName());
139 if (!method.getName().equals("onboardVNFShotFlow")) {
140 System.out.println("ExtentReport instance started from BeforeMethod...");
141 ExtentTestManager.startTest(method.getName());
142 ExtentTestManager.assignCategory(this.getClass());
145 System.out.println("ExtentReport instance started from Test...");
151 @AfterMethod(alwaysRun = true)
152 public void quitAfterTest(ITestResult result, ITestContext context) throws Exception {
154 String testName = result.getName();
155 Throwable throwable = result.getThrowable();
156 int status = result.getStatus();
159 case ITestResult.SUCCESS:
160 getExtendTest().log(Status.PASS, "Test Result : <span class='label success'>Success</span>");
163 case ITestResult.FAILURE:
164 getExtendTest().log(Status.ERROR, "ERROR - The following exepction occured");
165 getExtendTest().log(Status.ERROR, result.getThrowable());
166 getExtendTest().log(Status.FAIL, "<span class='label failure'>Failure</span>");
169 case ITestResult.SKIP:
170 getExtendTest().log(Status.SKIP, "SKIP - The following exepction occured");
177 ExtentTestManager.endTest();
182 @AfterClass(alwaysRun = true)
183 public synchronized static void cleanAfterClass() throws Exception{
185 System.out.println("delete components AfterClass");
186 deleteCreatedComponents(getCatalogAsMap());
190 @AfterSuite(alwaysRun = true)
191 public static void shutdownTitan() throws Exception {
194 shutdownTitanLogic();
197 protected static void openTitanLogic() throws Exception {
199 logger.trace(config.toString());
200 String titanConfigFilePath = config.getTitanPropertiesFile();
201 titanGraph = TitanFactory.open(titanConfigFilePath);
202 assertNotNull(titanGraph);
207 protected static void shutdownTitanLogic() {
208 if (titanGraph.isOpen()) {
211 CassandraUtils.close();
216 public void setLog(String fromDataProvider) {
218 String suiteName = ExtentManager.getSuiteName(myContext);
219 ExtentTestManager.startTest(Thread.currentThread().getStackTrace()[2].getMethodName() + " " + fromDataProvider);
220 ExtentTestManager.assignCategory(this.getClass());
224 protected static void performClean() throws Exception, FileNotFoundException {
225 // cleanComponents();
226 deleteCreatedComponents(getCatalogAsMap());
227 CassandraUtils.truncateAllKeyspaces();
230 public void verifyErrorCode(RestResponse response, String action, int expectedCode) {
231 assertNotNull("check response object is not null after " + action, response);
232 assertNotNull("check error code exists in response after " + action, response.getErrorCode());
233 assertEquals("Check response code after + action" + action, expectedCode, response.getErrorCode().intValue());
236 private static void cleanComponents() throws Exception {
238 // Components to delete
239 List<String> vfResourcesToDelete = new ArrayList<String>();
240 List<String> nonVfResourcesToDelete = new ArrayList<String>();
241 List<String> servicesToDelete = new ArrayList<String>();
242 List<String> productsToDelete = new ArrayList<String>();
244 // Categories to delete
245 List<ImmutableTriple<String, String, String>> productGroupingsToDelete = new ArrayList<>();
246 List<ImmutablePair<String, String>> productSubsToDelete = new ArrayList<>();
247 List<ImmutablePair<String, String>> resourceSubsToDelete = new ArrayList<>();
248 List<String> productCategoriesToDelete = new ArrayList<>();
249 List<String> resourceCategoriesToDelete = new ArrayList<String>();
250 List<String> serviceCategoriesToDelete = new ArrayList<String>();
252 List<String> resourcesNotToDelete = config.getResourcesNotToDelete();
253 List<String> resourceCategoriesNotToDelete = config.getResourceCategoriesNotToDelete();
254 List<String> serviceCategoriesNotToDelete = config.getServiceCategoriesNotToDelete();
256 Iterable<TitanVertex> vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.Resource.getName()).vertices();
257 if (vertices != null) {
258 Iterator<TitanVertex> iter = vertices.iterator();
259 while (iter.hasNext()) {
260 Vertex vertex = iter.next();
261 Boolean isAbstract = vertex.value(GraphPropertiesDictionary.IS_ABSTRACT.getProperty());
262 // if (!isAbstract) {
263 String name = vertex.value(GraphPropertiesDictionary.NAME.getProperty());
264 String version = vertex.value(GraphPropertiesDictionary.VERSION.getProperty());
266 if ((resourcesNotToDelete != null && !resourcesNotToDelete.contains(name)) || (version != null && !version.equals("1.0"))) {
267 String id = vertex.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
268 String resourceType = vertex.value(GraphPropertiesDictionary.RESOURCE_TYPE.getProperty());
269 if (name.startsWith("ci")) {
270 if (resourceType.equals(ResourceTypeEnum.VF.name())) {
271 vfResourcesToDelete.add(id);
273 nonVfResourcesToDelete.add(id);
276 } else if ((resourcesNotToDelete != null && !resourcesNotToDelete.contains(name)) || (version != null && version.equals("1.0"))) {
277 if ((boolean) vertex.value(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty()) == false) {
278 vertex.property(GraphPropertiesDictionary.IS_HIGHEST_VERSION.getProperty(), true);
284 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.Service.getName()).vertices();
285 if (vertices != null) {
286 Iterator<TitanVertex> iter = vertices.iterator();
287 while (iter.hasNext()) {
288 Vertex vertex = iter.next();
289 String id = vertex.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
290 String name = vertex.value(GraphPropertiesDictionary.NAME.getProperty());
291 if (name.startsWith("ci")) {
292 servicesToDelete.add(id);
297 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.Product.getName()).vertices();
298 if (vertices != null) {
299 Iterator<TitanVertex> iter = vertices.iterator();
300 while (iter.hasNext()) {
301 Vertex vertex = iter.next();
302 String id = vertex.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
303 String name = vertex.value(GraphPropertiesDictionary.NAME.getProperty());
304 if (name.startsWith("Ci")) {
305 productsToDelete.add(id);
310 // Getting categories
312 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.ResourceNewCategory.getName()).vertices();
313 if (vertices != null) {
314 Iterator<TitanVertex> iter = vertices.iterator();
315 while (iter.hasNext()) {
316 Vertex category = iter.next();
317 String name = category.value(GraphPropertiesDictionary.NAME.getProperty());
318 if (!resourceCategoriesNotToDelete.contains(name)) {
319 String catId = category.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
320 resourceCategoriesToDelete.add(catId);
321 Iterator<Vertex> subs = category.vertices(Direction.OUT, GraphEdgeLabels.SUB_CATEGORY.getProperty());
322 while (subs.hasNext()) {
323 Vertex sub = subs.next();
324 String subCatId = sub.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
325 resourceSubsToDelete.add(new ImmutablePair<String, String>(catId, subCatId));
331 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.ServiceNewCategory.getName()).vertices();
332 if (vertices != null) {
333 Iterator<TitanVertex> iter = vertices.iterator();
334 while (iter.hasNext()) {
335 Vertex category = iter.next();
336 String name = category.value(GraphPropertiesDictionary.NAME.getProperty());
337 if (!serviceCategoriesNotToDelete.contains(name)) {
338 String id = category.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
339 serviceCategoriesToDelete.add(id);
344 vertices = titanGraph.query().has(GraphPropertiesDictionary.LABEL.getProperty(), NodeTypeEnum.ProductCategory.getName()).vertices();
345 if (vertices != null) {
346 Iterator<TitanVertex> iter = vertices.iterator();
347 while (iter.hasNext()) {
348 Vertex category = iter.next();
349 String catId = category.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
350 productCategoriesToDelete.add(catId);
351 Iterator<Vertex> subs = category.vertices(Direction.OUT, GraphEdgeLabels.SUB_CATEGORY.getProperty());
352 while (subs.hasNext()) {
353 Vertex sub = subs.next();
354 String subCatId = sub.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
355 productSubsToDelete.add(new ImmutablePair<String, String>(catId, subCatId));
356 Iterator<Vertex> groupings = sub.vertices(Direction.OUT, GraphEdgeLabels.GROUPING.getProperty());
357 while (groupings.hasNext()) {
358 Vertex grouping = groupings.next();
359 String groupId = grouping.value(GraphPropertiesDictionary.UNIQUE_ID.getProperty());
360 productGroupingsToDelete.add(new ImmutableTriple<String, String, String>(catId, subCatId, groupId));
367 titanGraph.tx().commit();
369 String adminId = UserRoleEnum.ADMIN.getUserId();
370 String productStrategistId = UserRoleEnum.PRODUCT_STRATEGIST1.getUserId();
373 for (String id : productsToDelete) {
374 RestResponse deleteProduct = ProductRestUtils.deleteProduct(id, productStrategistId);
377 for (String id : servicesToDelete) {
378 RestResponse deleteServiceById = ServiceRestUtils.deleteServiceById(id, adminId);
381 for (String id : vfResourcesToDelete) {
382 RestResponse deleteResource = ResourceRestUtils.deleteResource(id, adminId);
386 for (String id : nonVfResourcesToDelete) {
387 RestResponse deleteResource = ResourceRestUtils.deleteResource(id, adminId);
391 // Categories delete - product
392 String componentType = BaseRestUtils.PRODUCT_COMPONENT_TYPE;
393 for (ImmutableTriple<String, String, String> triple : productGroupingsToDelete) {
394 CategoryRestUtils.deleteGrouping(triple.getRight(), triple.getMiddle(), triple.getLeft(), productStrategistId, componentType);
396 for (ImmutablePair<String, String> pair : productSubsToDelete) {
397 CategoryRestUtils.deleteSubCategory(pair.getRight(), pair.getLeft(), productStrategistId, componentType);
399 for (String id : productCategoriesToDelete) {
400 CategoryRestUtils.deleteCategory(id, productStrategistId, componentType);
403 // Categories delete - resource
404 componentType = BaseRestUtils.RESOURCE_COMPONENT_TYPE;
405 for (ImmutablePair<String, String> pair : resourceSubsToDelete) {
406 CategoryRestUtils.deleteSubCategory(pair.getRight(), pair.getLeft(), adminId, componentType);
408 for (String id : resourceCategoriesToDelete) {
409 CategoryRestUtils.deleteCategory(id, adminId, componentType);
411 // Categories delete - resource
412 componentType = BaseRestUtils.SERVICE_COMPONENT_TYPE;
413 for (String id : serviceCategoriesToDelete) {
414 CategoryRestUtils.deleteCategory(id, adminId, componentType);
419 private static void deleteCreatedComponents(Map<String, List<Component>> convertCatalogResponseToJavaObject) throws IOException {
420 final String userId = UserRoleEnum.DESIGNER.getUserId();
422 List<Component> resourcesArrayList = convertCatalogResponseToJavaObject.get(ComponentTypeEnum.PRODUCT_PARAM_NAME);
423 if (resourcesArrayList.size() > 0) {
424 List<String> collect = buildCollectionUniqueId(resourcesArrayList);
425 for (String uId : collect) {
426 ProductRestUtils.deleteProduct(uId, userId);
431 resourcesArrayList = convertCatalogResponseToJavaObject.get(ComponentTypeEnum.SERVICE_PARAM_NAME);
432 if (resourcesArrayList.size() > 0) {
433 List<String> collect = buildCollectionUniqueId(resourcesArrayList);
434 for (String uId : collect) {
435 ServiceRestUtils.markServiceToDelete(uId, userId);
437 ServiceRestUtils.deleteMarkedServices(userId);
441 resourcesArrayList = convertCatalogResponseToJavaObject.get(ComponentTypeEnum.RESOURCE_PARAM_NAME);
443 // List<String> collect = resourcesArrayList.stream().filter(s ->
444 // s.getName().startsWith("ci")).map(e ->
445 // e.getUniqueId()).collect(Collectors.toList());
447 // List<Map<String, String>> collect =
448 // resourcesArrayList.stream().filter(s ->
449 // s.getName().startsWith("ci")).map(e ->
450 // e.getAllVersions()).collect(Collectors.toList());
452 * List<String> collect = resourcesArrayList.stream().filter(s -> s.getName().startsWith("ci")) .flatMap(e -> e.getAllVersions().values().stream()).collect(Collectors.toList());
455 if (!CollectionUtils.isEmpty(resourcesArrayList)) {
456 List<String> collect = buildCollectionUniqueId(resourcesArrayList);
457 for (String uId : collect) {
458 ResourceRestUtils.markResourceToDelete(uId, userId);
460 ResourceRestUtils.deleteMarkedResources(userId);
467 private void deleteCollection(List<Component> componentArrayList, Consumer<String> deleteHandler) {
469 if (componentArrayList.size() > 0) {
470 List<String> collect = buildCollectionUniqueId(componentArrayList);
471 for (String uId : collect) {
472 deleteHandler.accept(uId);
473 // ProductRestUtils.deleteProduct(uId, userId);
478 protected static List<String> buildCollectionUniqueId(List<Component> resourcesArrayList) {
480 // Stream<String> flatMap = resourcesArrayList.stream().filter(s ->
481 // s.getName().startsWith("ci")).map(e -> e.getAllVersions()).map( e ->
482 // e.values()).flatMap( e -> e.stream());
484 // List<String> collect = resourcesArrayList.stream()
486 // .filter(s -> s.getName().startsWith("ci") )
488 // .map(e -> e.getUniqueId())
490 // .map( e -> e.values())
491 // .filter(out -> out!=null )
492 // .flatMap( e -> e.stream())
493 // .collect(Collectors.toList());
495 // List<String> collect = resourcesArrayList.stream().filter(s ->
496 // s.getName().startsWith("ci"))
498 // e.getAllVersions().values().stream()).collect(Collectors.toList());
499 ComponentTypeEnum componentTypeEnum = resourcesArrayList.get(0).getComponentType();
501 List<String> genericCollection = new ArrayList<String>();
503 resourcesArrayList.stream().filter(s -> s.getName().toLowerCase().startsWith("ci") && !s.getName().toLowerCase().equals("cindervolume")).map(e -> e.getUniqueId()).collect(Collectors.toList()).forEach((i) -> {
504 buildCollectionBaseOnComponentType(componentTypeEnum, genericCollection, i);
510 // List<String> collect =
511 // genericCollection.stream().collect(Collectors.toList());
513 return genericCollection;
516 public static void buildCollectionBaseOnComponentType(ComponentTypeEnum componentTypeEnum,
517 List<String> genericCollection, String i) {
519 switch (componentTypeEnum) {
521 RestResponse resource = ResourceRestUtils.getResource(i);
522 Resource convertResourceResponseToJavaObject = ResponseParser.convertResourceResponseToJavaObject(resource.getResponse());
523 Map<String, String> allVersions = convertResourceResponseToJavaObject.getAllVersions();
524 Collection<String> values = allVersions.values();
525 genericCollection.addAll(values);
529 RestResponse service = ServiceRestUtils.getService(i);
530 Service convertServiceResponseToJavaObject = ResponseParser.convertServiceResponseToJavaObject(service.getResponse());
531 allVersions = convertServiceResponseToJavaObject.getAllVersions();
532 values = allVersions.values();
533 genericCollection.addAll(values);
539 RestResponse product = ProductRestUtils.getProduct(i);
540 Product convertProductResponseToJavaObject = ResponseParser.convertProductResponseToJavaObject(product.getResponse());
541 allVersions = convertProductResponseToJavaObject.getAllVersions();
542 values = allVersions.values();
543 genericCollection.addAll(values);
550 } catch (Exception e1) {
551 // TODO Auto-generated catch block
552 e1.printStackTrace();
556 protected static Map<String, List<Component>> getCatalogAsMap() throws Exception {
557 RestResponse catalog = CatalogRestUtils.getCatalog(UserRoleEnum.DESIGNER.getUserId());
558 Map<String, List<Component>> convertCatalogResponseToJavaObject = ResponseParser.convertCatalogResponseToJavaObject(catalog.getResponse());
559 return convertCatalogResponseToJavaObject;
561 protected Resource createVfFromCSAR(User sdncModifierDetails, String csarId) throws Exception {
562 // create new resource from Csar
563 ResourceReqDetails resourceDetails = ElementFactory.getDefaultResource();
565 resourceDetails.setCsarUUID(csarId);
566 resourceDetails.setResourceType(ResourceTypeEnum.VF.name());
567 RestResponse createResource = ResourceRestUtils.createResource(resourceDetails, sdncModifierDetails);
568 BaseRestUtils.checkCreateResponse(createResource);
569 Resource createdResource = ResponseParser.convertResourceResponseToJavaObject(createResource.getResponse());
570 return createdResource;