2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * Copyright (C) 2021 Nokia Intellectual Property. All rights reserved.
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
22 package org.onap.sdc.frontend.ci.tests.execute.setup;
24 import com.aventstack.extentreports.ExtentReports;
25 import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
26 import com.aventstack.extentreports.reporter.ExtentXReporter;
27 import com.aventstack.extentreports.reporter.configuration.Protocol;
28 import com.aventstack.extentreports.reporter.configuration.Theme;
30 import java.nio.file.Files;
31 import java.nio.file.Paths;
32 import java.text.SimpleDateFormat;
33 import java.util.Calendar;
34 import java.util.GregorianCalendar;
35 import org.onap.sdc.backend.ci.tests.config.Config;
36 import org.onap.sdc.backend.ci.tests.utils.Utils;
37 import org.onap.sdc.backend.ci.tests.utils.rest.AutomationUtils;
38 import org.onap.sdc.frontend.ci.tests.utilities.FileHandling;
39 import org.onap.sdc.frontend.ci.tests.utilities.RestCDUtils;
40 import org.testng.ITestContext;
42 public class ExtentManager {
44 private static final String VERSIONS_INFO_FILE_NAME = "versions.info";
45 private static ExtentReports extent;
46 private static ExtentHtmlReporter htmlReporter;
47 private static ExtentXReporter extentxReporter;
49 public enum suiteNameXml {
51 TESTNG_FAILED_XML_NAME("testng-failed.xml");
53 suiteNameXml(String value) {
59 public String getValue() {
65 private static synchronized ExtentReports setReporter(String filePath, String htmlFile, Boolean isAppend) throws Exception {
66 String dbIp = DriverFactory.getConfig().getReportDBhost();
67 int dbPort = DriverFactory.getConfig().getReportDBport();
70 extentxReporter = new ExtentXReporter(dbIp, dbPort);
71 extent = new ExtentReports();
72 initAndSetExtentHtmlReporter(filePath, htmlFile, isAppend);
73 if (extentxReporter.config().getReportObjectId() != null) {
74 setExtentXReporter(isAppend);
76 extentxReporter.stop();
82 private static synchronized void setExtentXReporter(Boolean isAppend) {
83 extentxReporter.setAppendExisting(isAppend);
84 extent.attachReporter(extentxReporter);
87 private static synchronized void initAndSetExtentHtmlReporter(String filePath, String htmlFile, Boolean isAppend) throws Exception {
88 htmlReporter = new ExtentHtmlReporter(filePath + htmlFile);
89 setConfiguration(htmlReporter);
90 htmlReporter.setAppendExisting(isAppend);
91 extent.attachReporter(htmlReporter);
94 static synchronized ExtentReports getReporter() {
98 static void initReporter(String filepath, String htmlFile, ITestContext context) throws Exception {
100 String onboardVersion = AutomationUtils.getOnboardVersion();
101 String osVersion = AutomationUtils.getOSVersion();
102 Config config = Utils.getConfig();
103 String envData = config.getUrl();
104 String suiteName = getSuiteName(context);
105 String reportStartDate = null;
106 if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) {
107 if (config.isUseBrowserMobProxy()) {
108 setTrafficCaptue(config);
110 setReporter(filepath, htmlFile, true);
111 String suiteNameFromVersionInfoFile = FileHandling.getKeyByValueFromPropertyFormatFile(filepath + VERSIONS_INFO_FILE_NAME, "suiteName");
112 reporterDataDefinition(onboardVersion, osVersion, envData, suiteNameFromVersionInfoFile);
114 if (!Files.exists(Paths.get(filepath))) {
115 FileHandling.createDirectory(filepath);
117 setReporter(filepath, htmlFile, false);
118 Calendar calendar = new GregorianCalendar();
119 SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");
120 reportStartDate = formatter.format(calendar.getTime());
121 reporterDataDefinition(onboardVersion, osVersion, envData, suiteName);
123 .createVersionsInfoFile(filepath + VERSIONS_INFO_FILE_NAME, onboardVersion, osVersion, envData, suiteName, reportStartDate);
128 private static void reporterDataDefinition(String onboardVersion, String osVersion, String envData, String suiteNameFromVersionInfoFile)
130 extent.setSystemInfo("Onboard Version", onboardVersion);
131 extent.setSystemInfo("OS Version", osVersion);
132 extent.setSystemInfo("Host Name Address", RestCDUtils.getExecutionHostAddress());
133 extent.setSystemInfo("ExecutedOn", envData);
134 extent.setSystemInfo("SuiteName", suiteNameFromVersionInfoFile);
137 static String getSuiteName(ITestContext context) {
138 String suitePath = context.getSuite().getXmlSuite().getFileName();
139 if (suitePath != null) {
140 File file = new File(suitePath);
141 String suiteName = file.getName();
147 private static synchronized ExtentHtmlReporter setConfiguration(ExtentHtmlReporter htmlReporter) throws Exception {
149 htmlReporter.config().setTheme(Theme.STANDARD);
150 htmlReporter.config().setEncoding("UTF-8");
151 htmlReporter.config().setProtocol(Protocol.HTTPS);
152 htmlReporter.config().setDocumentTitle("SDC Automation Report");
153 htmlReporter.config().setChartVisibilityOnOpen(true);
154 htmlReporter.config().setReportName("SDC Automation Report");
155 htmlReporter.config().setChartVisibilityOnOpen(false);
156 htmlReporter.setStartTime(null);
160 public static void closeReporter() {
164 private static void setTrafficCaptue(Config config) {
165 boolean mobProxyStatus = config.isUseBrowserMobProxy();
166 if (mobProxyStatus) {
167 config.setCaptureTraffic(true);