Catalog alignment
[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 import org.slf4j.Logger;
32 import org.slf4j.LoggerFactory;
33
34 import java.io.File;
35 import java.io.IOException;
36 import java.util.Optional;
37 import java.util.UUID;
38
39 public final class ExtentTestActions {
40
41     private static final SomeInterface testManager = ExtentTestManager.getInstance();
42     private static final Logger LOGGER = LoggerFactory.getLogger(ExtentTestActions.class);
43
44     private ExtentTestActions() {
45
46     }
47
48     public static void log(Status logStatus, Markup mark) {
49         ExtentTest test = testManager.getTest();
50         test.log(logStatus, mark);
51     }
52
53     public static void log(Status logStatus, String message) {
54         ExtentTest test = testManager.getTest();
55         test.log(logStatus, message);
56     }
57
58     public static void log(Status logStatus, String message, String duration) {
59         log(logStatus, message + addDurationTag(duration));
60     }
61
62     public static void log(Status logStatus, Throwable throwabel) {
63         ExtentTest test = testManager.getTest();
64         test.log(logStatus, throwabel);
65     }
66
67     static void addTag(Status logStatus, String message) {
68         Markup m = null;
69         switch (logStatus) {
70             case PASS:
71                 m = MarkupHelper.createLabel(message, ExtentColor.GREEN);
72                 break;
73             case FAIL:
74                 m = MarkupHelper.createLabel(message, ExtentColor.RED);
75                 break;
76             case SKIP:
77                 m = MarkupHelper.createLabel(message, ExtentColor.BLUE);
78                 break;
79             case FATAL:
80                 m = MarkupHelper.createLabel(message, ExtentColor.BROWN);
81                 break;
82             default:
83                 break;
84         }
85
86         if (m != null) {
87             log(logStatus, m);
88         }
89     }
90
91     public static Optional<String> takeScreenshot(final Status logStatus, final String screenshotName,
92                                                   final String message) {
93         try {
94             return Optional.of(addScreenshot(logStatus, screenshotName, message));
95         } catch (final IOException e) {
96             LOGGER.warn("Could not take screenshot", e);
97         }
98
99         return Optional.empty();
100     }
101
102     public static String addScreenshot(final Status logStatus, String screenshotName,
103                                        final String message) throws IOException {
104         final String[] splitUuid = UUID.randomUUID().toString().split("-");
105         screenshotName = screenshotName + "-" + splitUuid[splitUuid.length - 1];
106         final File imageFile = GeneralUIUtils.takeScreenshot(screenshotName, SetupCDTest.getScreenshotFolder());
107         final String imageFilePath = new File(SetupCDTest.getReportFolder()).toURI().relativize(imageFile.toURI())
108             .getPath();
109         testManager.getTest()
110             .log(logStatus, message, MediaEntityBuilder.createScreenCaptureFromPath(imageFilePath).build());
111         return imageFilePath;
112     }
113
114     private static String addDurationTag(String duration) {
115         return "<td width=\"80px\">" + duration + "</td>";
116     }
117
118     private static String addLinkTag(String fileName, String pathToFile) {
119         return String.format("<a download=\"%s\" href=\"%s\">HAR file</a>", fileName, pathToFile);
120     }
121
122     static void addFileToReportAsLink(File harFile, String pathToFileFromReportDirectory, String message) {
123         log(Status.INFO, message, addLinkTag(harFile.getName(), pathToFileFromReportDirectory));
124     }
125
126
127 }