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