Fix MongoSocketOpenException-issue
[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.configuration.Protocol;
27 import com.aventstack.extentreports.reporter.configuration.Theme;
28 import java.io.File;
29 import java.nio.file.Files;
30 import java.nio.file.Paths;
31 import java.text.SimpleDateFormat;
32 import java.util.Calendar;
33 import java.util.GregorianCalendar;
34 import org.onap.sdc.backend.ci.tests.config.Config;
35 import org.onap.sdc.backend.ci.tests.utils.Utils;
36 import org.onap.sdc.backend.ci.tests.utils.rest.AutomationUtils;
37 import org.onap.sdc.frontend.ci.tests.utilities.FileHandling;
38 import org.onap.sdc.frontend.ci.tests.utilities.RestCDUtils;
39 import org.testng.ITestContext;
40
41 public class ExtentManager {
42
43     private static final String VERSIONS_INFO_FILE_NAME = "versions.info";
44     private static ExtentReports extent;
45     private static ExtentHtmlReporter htmlReporter;
46
47     private static synchronized ExtentReports setReporter(String filePath, String htmlFile, Boolean isAppend) throws Exception {
48         if (extent == null) {
49             extent = new ExtentReports();
50             initAndSetExtentHtmlReporter(filePath, htmlFile, isAppend);
51         }
52         return extent;
53     }
54
55     private static synchronized void initAndSetExtentHtmlReporter(String filePath, String htmlFile, Boolean isAppend) throws Exception {
56         htmlReporter = new ExtentHtmlReporter(filePath + htmlFile);
57         setConfiguration(htmlReporter);
58         htmlReporter.setAppendExisting(isAppend);
59         extent.attachReporter(htmlReporter);
60     }
61
62     static synchronized ExtentReports getReporter() {
63         return extent;
64     }
65
66     static void initReporter(String filepath, String htmlFile, ITestContext context) throws Exception {
67
68         String onboardVersion = AutomationUtils.getOnboardVersion();
69         String osVersion = AutomationUtils.getOSVersion();
70         Config config = Utils.getConfig();
71         String envData = config.getUrl();
72         String suiteName = getSuiteName(context);
73         String reportStartDate = null;
74         if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) {
75             if (config.isUseBrowserMobProxy()) {
76                 setTrafficCaptue(config);
77             }
78             setReporter(filepath, htmlFile, true);
79             String suiteNameFromVersionInfoFile = FileHandling.getKeyByValueFromPropertyFormatFile(filepath + VERSIONS_INFO_FILE_NAME, "suiteName");
80             reporterDataDefinition(onboardVersion, osVersion, envData, suiteNameFromVersionInfoFile);
81         } else {
82             if (!Files.exists(Paths.get(filepath))) {
83                 FileHandling.createDirectory(filepath);
84             }
85             setReporter(filepath, htmlFile, false);
86             Calendar calendar = new GregorianCalendar();
87             SimpleDateFormat formatter = new SimpleDateFormat("MMM dd, yyyy hh:mm:ss a");
88             reportStartDate = formatter.format(calendar.getTime());
89             reporterDataDefinition(onboardVersion, osVersion, envData, suiteName);
90             AutomationUtils
91                 .createVersionsInfoFile(filepath + VERSIONS_INFO_FILE_NAME, onboardVersion, osVersion, envData, suiteName, reportStartDate);
92         }
93
94     }
95
96     private static void reporterDataDefinition(String onboardVersion, String osVersion, String envData, String suiteNameFromVersionInfoFile)
97         throws Exception {
98         extent.setSystemInfo("Onboard Version", onboardVersion);
99         extent.setSystemInfo("OS Version", osVersion);
100         extent.setSystemInfo("Host Name Address", RestCDUtils.getExecutionHostAddress());
101         extent.setSystemInfo("ExecutedOn", envData);
102         extent.setSystemInfo("SuiteName", suiteNameFromVersionInfoFile);
103     }
104
105     static String getSuiteName(ITestContext context) {
106         String suitePath = context.getSuite().getXmlSuite().getFileName();
107         if (suitePath != null) {
108             File file = new File(suitePath);
109             String suiteName = file.getName();
110             return suiteName;
111         }
112         return null;
113     }
114
115     private static synchronized ExtentHtmlReporter setConfiguration(ExtentHtmlReporter htmlReporter) throws Exception {
116
117         htmlReporter.config().setTheme(Theme.STANDARD);
118         htmlReporter.config().setEncoding("UTF-8");
119         htmlReporter.config().setProtocol(Protocol.HTTPS);
120         htmlReporter.config().setDocumentTitle("SDC Automation Report");
121         htmlReporter.config().setChartVisibilityOnOpen(true);
122         htmlReporter.config().setReportName("SDC Automation Report");
123         htmlReporter.config().setChartVisibilityOnOpen(false);
124         htmlReporter.setStartTime(null);
125         return htmlReporter;
126     }
127
128     private static void setTrafficCaptue(Config config) {
129         boolean mobProxyStatus = config.isUseBrowserMobProxy();
130         if (mobProxyStatus) {
131             config.setCaptureTraffic(true);
132         }
133     }
134
135     public enum suiteNameXml {
136
137         TESTNG_FAILED_XML_NAME("testng-failed.xml");
138
139         private String value;
140
141         suiteNameXml(String value) {
142             this.value = value;
143         }
144
145         public String getValue() {
146             return value;
147         }
148
149     }
150 }