/*- * ============LICENSE_START======================================================= * SDC * ================================================================================ * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved. * ================================================================================ * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. * ============LICENSE_END========================================================= */ package org.onap.sdc.ci.tests.execute.setup; import com.aventstack.extentreports.ExtentReports; import com.aventstack.extentreports.reporter.ExtentHtmlReporter; import com.aventstack.extentreports.reporter.configuration.Protocol; import com.aventstack.extentreports.reporter.configuration.Theme; import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import org.onap.sdc.ci.tests.datatypes.Configuration; import org.onap.sdc.ci.tests.utilities.FileHandling; import org.testng.ITestContext; public class ExtentManager { private static final String VERSIONS_INFO_FILE_NAME = "versions.info"; private static ExtentReports extent; private static final String icon = "$(document).ready(function() {" +"\n"+ "$('.brand-logo').html('').prepend(\"\").width(\"120px\").css(\"float\",\"left\").css(\"padding-left\",\"0\");$('.report-name').css(\"font-weight\",\"bold\");"+"\n"+ "})"; public enum suiteNameXml { TESTNG_FAILED_XML_NAME("testng-failed.xml"); suiteNameXml(String value) { this.value = value; } private String value; public String getValue() { return value; } } public synchronized static void setReporter(String filePath, String htmlFile) { if (extent == null) { extent = new ExtentReports(); initAndSetExtentHtmlReporter(filePath, htmlFile); } } public synchronized static void initAndSetExtentHtmlReporter(String filePath, String htmlFile) { ExtentHtmlReporter htmlReporter = new ExtentHtmlReporter(filePath + htmlFile); setConfiguration(htmlReporter); extent.attachReporter(htmlReporter); } public synchronized static ExtentReports getReporter() { return extent; } public static void initReporter(Configuration config, ITestContext context) throws Exception { String envData = config.getUrl(); String suiteName = getSuiteName(context); String filepath = config.getReportFolder(); String htmlFile = config.getReportFileName(); if(suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())){ if (config.isUseBrowserMobProxy()){ setTrafficCapture(config); } setReporter(filepath, htmlFile); suiteName = FileHandling.getKeyByValueFromPropertyFormatFile(filepath + VERSIONS_INFO_FILE_NAME, "suiteName"); }else{ FileHandling.deleteDirectory(SetupCDTest.getReportFolder()); FileHandling.createDirectory(filepath); setReporter(filepath, htmlFile); createVersionsInfoFile(filepath , VERSIONS_INFO_FILE_NAME, envData, suiteName); } reporterDataDefinition(envData, suiteName); } private static void createVersionsInfoFile(String path, String file, String envData, String suiteName) throws IOException { File myFoo = new File(path + file); FileOutputStream fooStream = new FileOutputStream(myFoo, false); // true to append String versions = ("env=\""+ envData + "\"\n" + "suiteName=\""+ suiteName+ "\"\n"); byte[] myBytes = versions.getBytes(); fooStream.write(myBytes); fooStream.close(); } private static void reporterDataDefinition(String envData, String suiteNameFromVersionInfoFile) { extent.setSystemInfo("Host Name Address", FileHandling.getExecutionHostAddress()); extent.setSystemInfo("ExecutedOn", envData); extent.setSystemInfo("SuiteName", suiteNameFromVersionInfoFile); } public static String getSuiteName(ITestContext context) { String suitePath = context.getSuite().getXmlSuite().getFileName(); if(suitePath != null){ File file = new File(suitePath); String suiteName = file.getName(); return suiteName; } return null; } public synchronized static ExtentHtmlReporter setConfiguration(ExtentHtmlReporter htmlReporter) { htmlReporter.config().setTheme(Theme.STANDARD); htmlReporter.config().setEncoding("UTF-8"); htmlReporter.config().setProtocol(Protocol.HTTPS); htmlReporter.config().setDocumentTitle("Automation Report"); htmlReporter.config().setReportName("Automation Report"); htmlReporter.config().setJS(icon); return htmlReporter; } public static void closeReporter(){ extent.flush(); } public static void setTrafficCapture(Configuration config) { config.setCaptureTraffic(true); } }