c3a1d1368458de5891fef43810550fba0e671843
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / setup / ExtentTestActions.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.execute.setup;
22
23 import com.aventstack.extentreports.ExtentTest;
24 import com.aventstack.extentreports.MediaEntityBuilder;
25 import com.aventstack.extentreports.Status;
26 import com.aventstack.extentreports.markuputils.ExtentColor;
27 import com.aventstack.extentreports.markuputils.Markup;
28 import com.aventstack.extentreports.markuputils.MarkupHelper;
29 import org.openecomp.sdc.ci.tests.api.SomeInterface;
30 import org.openecomp.sdc.ci.tests.utilities.GeneralUIUtils;
31
32 import java.io.File;
33 import java.io.IOException;
34 import java.util.UUID;
35
36 public class ExtentTestActions {
37
38     private static final SomeInterface testManager = ExtentTestManager.getInstance();
39
40     private ExtentTestActions() {
41
42     }
43
44     public static void log(Status logStatus, Markup mark) {
45         ExtentTest test = testManager.getTest();
46         test.log(logStatus, mark);
47     }
48
49     public static void log(Status logStatus, String message) {
50         ExtentTest test = testManager.getTest();
51         test.log(logStatus, message);
52     }
53
54     public static void log(Status logStatus, String message, String duration) {
55         log(logStatus, message + addDurationTag(duration));
56     }
57
58     public static void log(Status logStatus, Throwable throwabel) {
59         ExtentTest test = testManager.getTest();
60         test.log(logStatus, throwabel);
61     }
62
63     static void addTag(Status logStatus, String message) {
64         Markup m = null;
65         switch (logStatus) {
66             case PASS:
67                 m = MarkupHelper.createLabel(message, ExtentColor.GREEN);
68                 break;
69             case FAIL:
70                 m = MarkupHelper.createLabel(message, ExtentColor.RED);
71                 break;
72             case SKIP:
73                 m = MarkupHelper.createLabel(message, ExtentColor.BLUE);
74                 break;
75             case FATAL:
76                 m = MarkupHelper.createLabel(message, ExtentColor.BROWN);
77                 break;
78             default:
79                 break;
80         }
81
82         if (m != null) {
83             log(logStatus, m);
84         }
85     }
86
87     public static String addScreenshot(final Status logStatus, String screenshotName,
88                                        final String message) throws IOException {
89         final String[] splitUuid = UUID.randomUUID().toString().split("-");
90         screenshotName = screenshotName + "-" + splitUuid[splitUuid.length - 1];
91         final File imageFile = GeneralUIUtils.takeScreenshot(screenshotName, SetupCDTest.getScreenshotFolder());
92         final String imageFilePath = new File(SetupCDTest.getReportFolder()).toURI().relativize(imageFile.toURI())
93             .getPath();
94         testManager.getTest()
95             .log(logStatus, message, MediaEntityBuilder.createScreenCaptureFromPath(imageFilePath).build());
96         return imageFilePath;
97     }
98
99     private static String addDurationTag(String duration) {
100         return "<td width=\"80px\">" + duration + "</td>";
101     }
102
103     private static String addLinkTag(String fileName, String pathToFile) {
104         return String.format("<a download=\"%s\" href=\"%s\">HAR file</a>", fileName, pathToFile);
105     }
106
107     static void addFileToReportAsLink(File harFile, String pathToFileFromReportDirectory, String message) {
108         log(Status.INFO, message, addLinkTag(harFile.getName(), pathToFileFromReportDirectory));
109     }
110
111
112 }