re base code
[sdc.git] / ui-ci / src / main / java / org / openecomp / sdc / ci / tests / execute / setup / ReportAfterTestManager.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.execute.setup;
22
23 import com.aventstack.extentreports.Status;
24 import org.openecomp.sdc.ci.tests.execute.setup.ExtentManager.suiteNameXml;
25 import org.testng.ITestContext;
26 import org.testng.ITestResult;
27
28 import java.io.IOException;
29
30 public class ReportAfterTestManager extends ExtentTestActions  {
31         
32         private static String testName;
33         private static Throwable throwable;
34         private static int status;
35         
36         private static void logSuccessAfterTest(){
37                 final Status logStatus = Status.PASS;
38                 addTag(logStatus, "Success");
39                 try{
40                         String message = "Finished the test with the following screenshot : ";
41                         addScreenshotToReport(logStatus, testName, message);
42                 }catch(Exception e){
43                         log(logStatus, "SUCCESS - The following exepction occured : " + e.getMessage());
44                 }
45         }
46         
47         private static void logFailAfterTest(){
48                 addTag(Status.FAIL, "Failure");
49                 try{
50                         log(Status.ERROR, "ERROR - The following exepction occured : ");
51                         log(Status.ERROR, throwable);
52                         String message = "Failure is described in the following screenshot : ";
53                         addScreenshotToReport(Status.FAIL, testName, message);
54                 }catch(Exception e){
55                         log(Status.ERROR, "ERROR - The following exepction occured : " + e.getMessage());
56                 }
57         }
58         
59         private static void logSkipAfterTest(){
60                 final Status logStatus = Status.SKIP;
61                 addTag(logStatus, "Skipped");
62                 try{
63                         log(logStatus, "SKIP - The following exepction occured : ");
64                         log(logStatus, throwable);
65                         String message = "Skip is described in the following screenshot : ";
66                         addScreenshotToReport(logStatus, testName, message);
67                 }catch(Exception e){
68                         log(logStatus, "SKIP - The following exepction occured : " + e.getMessage());
69                 }
70         }
71         private static void logFatalAfterTest(){
72                 final Status logStatus = Status.FATAL;
73                 addTag(logStatus, "Fatal");
74                 try{
75                         log(logStatus, "FATAL - The following exepction occured : ");
76                         log(logStatus, throwable);
77                         String message = "Fatal is described in the following screenshot : ";
78                         addScreenshotToReport(logStatus, testName, message);
79                 }catch(Exception e){
80                         log(logStatus, "FATAL - The following exepction occured : " + e.getMessage());
81                 }
82         }
83         
84         private static String addScreenshotToReport(Status logStatus, String testName, String message) throws IOException{
85                 
86                 String addedValueFromDataProvider = WindowTestManager.getWindowMap().getAddedValueFromDataProvider();
87                 if (addedValueFromDataProvider != null){
88                         addedValueFromDataProvider = addedValueFromDataProvider.replace(":", "-");
89                         testName = testName + "...." + addedValueFromDataProvider;
90                 }
91                 
92                 return addScreenshot(logStatus, testName, message);
93         }
94         
95         public static void report(ITestResult result, ITestContext context){
96                 
97                 testName = result.getName();
98                 throwable = result.getThrowable();
99                 status = result.getStatus();
100                 
101                 String suiteName = ExtentManager.getSuiteName(context);
102
103                 switch(status){
104                 case ITestResult.SUCCESS:                               
105                         logSuccessAfterTest();
106                         break;
107                                 
108                 case ITestResult.FAILURE:
109                         
110                         if (suiteName.equals(suiteNameXml.TESTNG_FAILED_XML_NAME.getValue())) {
111                                 logFatalAfterTest();
112                         }else{
113                                 logFailAfterTest();
114                         }
115                         break;
116                         
117                 case ITestResult.SKIP:
118                         logSkipAfterTest();
119                         break;
120                                 
121                 default:
122                         break;
123                 }
124                                 
125         }
126                 
127 }
128