re base code
[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         public static SomeInterface testManager = new ExtentTestManager();
39         
40         public static void log(Status logStatus, Markup mark){
41                 ExtentTest test = testManager.getTest();
42                 test.log(logStatus, mark);
43         }
44
45         public static void log(Status logStatus, String message){
46                 ExtentTest test = testManager.getTest();
47                 test.log(logStatus, message);
48         }
49         
50         public static void log(Status logStatus, String message, String duration){
51                 log(logStatus, message + addDurationTag(duration));
52         }
53         
54         public static void log(Status logStatus, Throwable throwabel){
55                 ExtentTest test = testManager.getTest();
56                 test.log(logStatus, throwabel);
57         }
58         
59         public static void addTag(Status logStatus, String message){
60                 Markup m = null;
61                 switch(logStatus){
62                 case PASS:
63                         m = MarkupHelper.createLabel(message, ExtentColor.GREEN);
64                         break;
65                 case FAIL:
66                         m = MarkupHelper.createLabel(message, ExtentColor.RED);
67                         break;
68                 case SKIP:
69                         m = MarkupHelper.createLabel(message, ExtentColor.BLUE);
70                         break;
71                 case FATAL:
72                         m = MarkupHelper.createLabel(message, ExtentColor.BROWN);
73                         break;
74                 default:
75                         break;
76                 }
77                 
78                 if (m != null){
79                         log(logStatus, m);
80                 }
81         }
82         
83         public static String addScreenshot(Status logStatus, String screenshotName, String message) throws IOException{
84                 String imageFilePath = null;
85                 String uuid = UUID.randomUUID().toString();
86                 String[] stringArray = uuid.split("-");
87                 screenshotName = screenshotName + "-" + stringArray[stringArray.length - 1];
88                 try {
89                         File imageFile = GeneralUIUtils.takeScreenshot(screenshotName, SetupCDTest.getScreenshotFolder());
90                         imageFilePath = new File(SetupCDTest.getReportFolder()).toURI().relativize(imageFile.toURI()).getPath();
91                 } catch (IOException e) {
92                         e.printStackTrace();
93                 }
94                 
95                 ExtentTest test = testManager.getTest();
96                 test.log(logStatus, message, MediaEntityBuilder.createScreenCaptureFromPath(imageFilePath).build());
97                 return imageFilePath;
98         }
99         
100         private static String addDurationTag(String duration){
101                 return "<td width=\"80px\">" + duration + "</td>";
102         }
103         
104         public static String addLinkTag(String fileName, String pathToFile){
105                 return String.format("<a download=\"%s\" href=\"%s\">HAR file</a>", fileName, pathToFile);
106         }
107
108         public static void addFileToReportAsLink(File harFile, String pathToFileFromReportDirectory, String message) {
109                 log(Status.INFO, message, addLinkTag(harFile.getName(), pathToFileFromReportDirectory));
110         }
111         
112         
113         
114
115
116 }