Remove ES + Kibana
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / setup / ExtentManager.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.ExtentReports;
24 import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
25 import com.aventstack.extentreports.reporter.ExtentXReporter;
26 import com.aventstack.extentreports.reporter.configuration.Protocol;
27 import com.aventstack.extentreports.reporter.configuration.Theme;
28 import org.openecomp.sdc.ci.tests.config.Config;
29 import org.openecomp.sdc.ci.tests.utilities.FileHandling;
30 import org.openecomp.sdc.ci.tests.utilities.RestCDUtils;
31 import org.openecomp.sdc.ci.tests.utils.Utils;
32 import org.openecomp.sdc.ci.tests.utils.rest.AutomationUtils;
33 import org.testng.ITestContext;
34
35 import java.io.File;
36 import java.text.SimpleDateFormat;
37 import java.util.Calendar;
38 import java.util.GregorianCalendar;
39
40 public class ExtentManager {
41
42     private static final String VERSIONS_INFO_FILE_NAME = "versions.info";
43     private static ExtentReports extent;
44     private static ExtentHtmlReporter htmlReporter;
45     private static ExtentXReporter extentxReporter;
46
47     public enum suiteNameXml {
48
49         TESTNG_FAILED_XML_NAME("testng-failed.xml");
50
51         suiteNameXml(String value) {
52             this.value = value;
53         }
54
55         private String value;
56
57         public String getValue() {
58             return value;
59         }
60
61     }
62
63     private static synchronized  ExtentReports setReporter(String filePath, String htmlFile, Boolean isAppend) throws Exception {
64         String dbIp = DriverFactory.getConfig().getReportDBhost();
65         int dbPort = DriverFactory.getConfig().getReportDBport();
66
67         if (extent == null) {
68             extentxReporter = new ExtentXReporter(dbIp, dbPort);
69             extent = new ExtentReports();
70             initAndSetExtentHtmlReporter(filePath, htmlFile, isAppend);
71             if (extentxReporter.config().getReportObjectId() != null) {
72                 setExtentXReporter(isAppend);
73             } else {
74                 extentxReporter.stop();
75             }
76         }
77         return extent;
78     }
79
80     private static synchronized  void setExtentXReporter(Boolean isAppend) {
81         extentxReporter.setAppendExisting(isAppend);
82         extent.attachReporter(extentxReporter);
83     }
84
85     private static synchronized  void initAndSetExtentHtmlReporter(String filePath, String htmlFile, Boolean isAppend) throws Exception {
86         htmlReporter = new ExtentHtmlReporter(filePath + htmlFile);
87         setConfiguration(htmlReporter);
88         htmlReporter.setAppendExisting(isAppend);
89         extent.attachReporter(htmlReporter);
90     }
91
92     static synchronized  ExtentReports getReporter() {
93         return extent;
94     }
95
96     static void initReporter(String filepath, String htmlFile, ITestContext context) throws Exception {
97
98         String onboardVersion = AutomationUtils.getOnboardVersion();
99         String osVersion = AutomationUtils.getOSVersion();
100         Config config = Utils.getConfig();
101         String envData = config.getUrl();
102         String suiteName = getSuiteName(context);
103         String reportStartDate = null;
104         if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) {
105             if (config.getUseBrowserMobProxy()) {
106                 setTrafficCaptue(config);
107             }
108             setReporter(filepath, htmlFile, true);
109             String suiteNameFromVersionInfoFile = FileHandling.getKeyByValueFromPropertyFormatFile(filepath + VERSIONS_INFO_FILE_NAME, "suiteName");
110             reporterDataDefinition(onboardVersion, osVersion, envData, suiteNameFromVersionInfoFile);
111         } else {
112             FileHandling.deleteDirectory(SetupCDTest.getReportFolder());
113             FileHandling.createDirectory(filepath);
114             setReporter(filepath, htmlFile, false);
115             Calendar calendar = new GregorianCalendar();
116             SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");
117             reportStartDate = formatter.format(calendar.getTime());
118             reporterDataDefinition(onboardVersion, osVersion, envData, suiteName);
119             AutomationUtils.createVersionsInfoFile(filepath + VERSIONS_INFO_FILE_NAME, onboardVersion, osVersion, envData, suiteName, reportStartDate);
120         }
121
122     }
123
124     private static void reporterDataDefinition(String onboardVersion, String osVersion, String envData, String suiteNameFromVersionInfoFile) throws Exception {
125         extent.setSystemInfo("Onboard Version", onboardVersion);
126         extent.setSystemInfo("OS Version", osVersion);
127         extent.setSystemInfo("Host Name Address", RestCDUtils.getExecutionHostAddress());
128         extent.setSystemInfo("ExecutedOn", envData);
129         extent.setSystemInfo("SuiteName", suiteNameFromVersionInfoFile);
130     }
131
132     static String getSuiteName(ITestContext context) {
133         String suitePath = context.getSuite().getXmlSuite().getFileName();
134         if (suitePath != null) {
135             File file = new File(suitePath);
136             String suiteName = file.getName();
137             return suiteName;
138         }
139         return null;
140     }
141
142     private static synchronized  ExtentHtmlReporter setConfiguration(ExtentHtmlReporter htmlReporter) throws Exception {
143
144         htmlReporter.config().setTheme(Theme.STANDARD);
145         htmlReporter.config().setEncoding("UTF-8");
146         htmlReporter.config().setProtocol(Protocol.HTTPS);
147         htmlReporter.config().setDocumentTitle("SDC Automation Report");
148         htmlReporter.config().setChartVisibilityOnOpen(true);
149         htmlReporter.config().setReportName("SDC Automation Report");
150         htmlReporter.config().setChartVisibilityOnOpen(false);
151         htmlReporter.setStartTime(null);
152         return htmlReporter;
153     }
154
155     public static void closeReporter() {
156         extent.flush();
157     }
158
159     private static void setTrafficCaptue(Config config) {
160         boolean mobProxyStatus = config.getUseBrowserMobProxy();
161         if (mobProxyStatus) {
162             config.setCaptureTraffic(true);
163         }
164     }
165 }
166