new ui sanity docker
[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 org.openecomp.sdc.ci.tests.config.Config;
24 import org.openecomp.sdc.ci.tests.utils.Utils;
25 import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
26 import org.testng.ITestContext;
27 import com.aventstack.extentreports.ExtentReports;
28 import com.aventstack.extentreports.reporter.ExtentHtmlReporter;
29 import com.aventstack.extentreports.reporter.ExtentXReporter;
30 import com.aventstack.extentreports.reporter.configuration.Protocol;
31 import com.aventstack.extentreports.reporter.configuration.Theme;
32 import org.openecomp.sdc.ci.tests.config.Config;
33
34 import org.openecomp.sdc.ci.tests.utils.Utils;
35 import org.openecomp.sdc.ci.tests.utils.general.FileHandling;
36 import org.openecomp.sdc.ci.tests.utils.rest.AutomationUtils;
37 import org.testng.ITestContext;
38
39 import java.io.File;
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         private static ExtentXReporter extentxReporter;
47
48         public enum suiteNameXml {
49
50                 TESTNG_FAILED_XML_NAME("testng-failed.xml");
51
52                 suiteNameXml(String value) {
53                         this.value = value;
54                     }
55
56                 private String value;
57
58             public String getValue() {
59                 return value;
60             }
61                 
62         }
63     
64     public synchronized static ExtentReports setReporter(String filePath, String htmlFile, Boolean isAppend) throws Exception {
65         String dbIp = Utils.getConfig().getReportDBhost();
66         int dbPort = Utils.getConfig().getReportDBport();
67
68         if (extent == null) {
69                 extentxReporter = new ExtentXReporter(dbIp, dbPort);
70                 extent = new ExtentReports();
71                 initAndSetExtentHtmlReporter(filePath, htmlFile, isAppend);
72                         
73                 if(extentxReporter.config().getReportObjectId() != null){
74                                 setExtentXReporter(isAppend);
75                         }else{
76                                 extentxReporter.stop();
77                         }
78         }
79         return extent;
80     }
81     
82     public synchronized static void setExtentXReporter(Boolean isAppend){
83         extentxReporter.setAppendExisting(isAppend);
84                 extent.attachReporter(extentxReporter);
85     }
86     
87     public synchronized static 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     public synchronized static ExtentReports getReporter() {
95         return extent;
96     }
97
98         public 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                 
106                 if(suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())){
107                         if (config.getUseBrowserMobProxy())
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                         FileHandling.deleteDirectory(ComponentBaseTest.getReportFolder());
115                         FileHandling.createDirectory(filepath);
116                         setReporter(filepath, htmlFile, false);
117                         reporterDataDefinition(onboardVersion, osVersion, envData, suiteName);
118                         AutomationUtils.createVersionsInfoFile(filepath + VERSIONS_INFO_FILE_NAME, onboardVersion, osVersion, envData, suiteName);
119                 }
120                 
121         }
122
123         public static void reporterDataDefinition(String onboardVersion, String osVersion, String envData, String suiteNameFromVersionInfoFile) throws Exception {
124                 extent.setSystemInfo("Onboard Version", onboardVersion);
125                 extent.setSystemInfo("OS Version", osVersion);
126 //              extent.setSystemInfo("Host Name Address", RestCDUtils.getExecutionHostAddress());
127                 extent.setSystemInfo("ExecutedOn", envData);
128                 extent.setSystemInfo("SuiteName", suiteNameFromVersionInfoFile);
129         }
130
131         public static  String getSuiteName(ITestContext context) {
132                 String suitePath = context.getSuite().getXmlSuite().getFileName();
133                 if(suitePath != null){
134                         File file = new File(suitePath);
135                         String suiteName = file.getName();
136                         return suiteName;
137                 }
138                 return null;
139         }
140         
141         public synchronized static ExtentHtmlReporter setConfiguration(ExtentHtmlReporter htmlReporter) throws Exception {
142                 
143         htmlReporter.config().setTheme(Theme.STANDARD);
144         htmlReporter.config().setEncoding("UTF-8");
145         htmlReporter.config().setProtocol(Protocol.HTTPS);
146         htmlReporter.config().setDocumentTitle("SDC Automation Report");
147         htmlReporter.config().setChartVisibilityOnOpen(true);
148 //      htmlReporter.config().setReportName(AutomationUtils.getATTVersion());
149         htmlReporter.config().setReportName("SDC Automation Report");
150         htmlReporter.config().setChartVisibilityOnOpen(false);
151 //      htmlReporter.config().setJS(icon);
152         return htmlReporter;
153     }
154         
155         public static void closeReporter(){
156                 extent.flush();
157         }
158         
159         public static void setTrafficCaptue(Config config) {
160                 boolean mobProxyStatus = config.getUseBrowserMobProxy();
161                 if (mobProxyStatus){
162                         config.setCaptureTraffic(true);;
163                 }
164         }       
165 }
166