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.execute.setup;
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;
36 import java.text.SimpleDateFormat;
37 import java.util.Calendar;
38 import java.util.GregorianCalendar;
40 public class ExtentManager {
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;
47 public enum suiteNameXml {
49 TESTNG_FAILED_XML_NAME("testng-failed.xml");
51 suiteNameXml(String value) {
57 public String getValue() {
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();
68 extentxReporter = new ExtentXReporter(dbIp, dbPort);
69 extent = new ExtentReports();
70 initAndSetExtentHtmlReporter(filePath, htmlFile, isAppend);
71 if (extentxReporter.config().getReportObjectId() != null) {
72 setExtentXReporter(isAppend);
74 extentxReporter.stop();
80 private static synchronized void setExtentXReporter(Boolean isAppend) {
81 extentxReporter.setAppendExisting(isAppend);
82 extent.attachReporter(extentxReporter);
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);
92 static synchronized ExtentReports getReporter() {
96 static void initReporter(String filepath, String htmlFile, ITestContext context) throws Exception {
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);
108 setReporter(filepath, htmlFile, true);
109 String suiteNameFromVersionInfoFile = FileHandling.getKeyByValueFromPropertyFormatFile(filepath + VERSIONS_INFO_FILE_NAME, "suiteName");
110 reporterDataDefinition(onboardVersion, osVersion, envData, suiteNameFromVersionInfoFile);
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);
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);
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();
142 private static synchronized ExtentHtmlReporter setConfiguration(ExtentHtmlReporter htmlReporter) throws Exception {
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);
155 public static void closeReporter() {
159 private static void setTrafficCaptue(Config config) {
160 boolean mobProxyStatus = config.getUseBrowserMobProxy();
161 if (mobProxyStatus) {
162 config.setCaptureTraffic(true);