1d515da14af0972568f241d5bdeb9868da4308cb
[sdc.git] / integration-tests / src / test / java / org / onap / sdc / frontend / 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  * 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
11  *
12  *      http://www.apache.org/licenses/LICENSE-2.0
13  *
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=========================================================
20  */
21
22 package org.onap.sdc.frontend.ci.tests.execute.setup;
23
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;
29 import java.io.File;
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;
41
42 public class ExtentManager {
43
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;
48
49     public enum suiteNameXml {
50
51         TESTNG_FAILED_XML_NAME("testng-failed.xml");
52
53         suiteNameXml(String value) {
54             this.value = value;
55         }
56
57         private String value;
58
59         public String getValue() {
60             return value;
61         }
62
63     }
64
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();
68
69         if (extent == null) {
70             extentxReporter = new ExtentXReporter(dbIp, dbPort);
71             extent = new ExtentReports();
72             initAndSetExtentHtmlReporter(filePath, htmlFile, isAppend);
73             if (extentxReporter.config().getReportObjectId() != null) {
74                 setExtentXReporter(isAppend);
75             } else {
76                 extentxReporter.stop();
77             }
78         }
79         return extent;
80     }
81
82     private static synchronized void setExtentXReporter(Boolean isAppend) {
83         extentxReporter.setAppendExisting(isAppend);
84         extent.attachReporter(extentxReporter);
85     }
86
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);
92     }
93
94     static synchronized ExtentReports getReporter() {
95         return extent;
96     }
97
98     static void initReporter(String filepath, String htmlFile, ITestContext context) throws Exception {
99
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);
109             }
110             setReporter(filepath, htmlFile, true);
111             String suiteNameFromVersionInfoFile = FileHandling.getKeyByValueFromPropertyFormatFile(filepath + VERSIONS_INFO_FILE_NAME, "suiteName");
112             reporterDataDefinition(onboardVersion, osVersion, envData, suiteNameFromVersionInfoFile);
113         } else {
114             if (!Files.exists(Paths.get(filepath))) {
115                 FileHandling.createDirectory(filepath);
116             }
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);
122             AutomationUtils
123                 .createVersionsInfoFile(filepath + VERSIONS_INFO_FILE_NAME, onboardVersion, osVersion, envData, suiteName, reportStartDate);
124         }
125
126     }
127
128     private static void reporterDataDefinition(String onboardVersion, String osVersion, String envData, String suiteNameFromVersionInfoFile)
129         throws Exception {
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);
135     }
136
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();
142             return suiteName;
143         }
144         return null;
145     }
146
147     private static synchronized ExtentHtmlReporter setConfiguration(ExtentHtmlReporter htmlReporter) throws Exception {
148
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);
157         return htmlReporter;
158     }
159
160     public static void closeReporter() {
161         extent.flush();
162     }
163
164     private static void setTrafficCaptue(Config config) {
165         boolean mobProxyStatus = config.isUseBrowserMobProxy();
166         if (mobProxyStatus) {
167             config.setCaptureTraffic(true);
168         }
169     }
170 }