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