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