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