Catalog alignment
[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.apache.commons.lang3.StringUtils;
25 import org.openecomp.sdc.ci.tests.execute.setup.ExtentManager.suiteNameXml;
26 import org.slf4j.Logger;
27 import org.slf4j.LoggerFactory;
28 import org.testng.ITestContext;
29 import org.testng.ITestResult;
30
31 import java.io.IOException;
32
33 import static org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions.addScreenshot;
34 import static org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions.addTag;
35 import static org.openecomp.sdc.ci.tests.execute.setup.ExtentTestActions.log;
36
37 public class ReportAfterTestManager {
38
39     private static final Logger LOGGER = LoggerFactory.getLogger(ReportAfterTestManager.class);
40     private static String testName;
41     private static Throwable throwable;
42     private static String exceptionMsgFormat = "%s - The following exception occurred:";
43
44     private ReportAfterTestManager() {
45
46     }
47
48     public static void report(final ITestResult result, final ITestContext context) {
49         testName = result.getName();
50         throwable = result.getThrowable();
51
52         final String suiteName = ExtentManager.getSuiteName(context);
53
54         switch (result.getStatus()) {
55             case ITestResult.SUCCESS:
56                 logSuccessAfterTest();
57                 break;
58
59             case ITestResult.FAILURE:
60                 logFailure(suiteName);
61                 break;
62
63             case ITestResult.SKIP:
64                 logSkipAfterTest();
65                 break;
66
67             default:
68                 break;
69         }
70
71     }
72
73     private static void logSuccessAfterTest() {
74         addTag(Status.PASS, "Success");
75         takeScreenshot(Status.PASS);
76     }
77
78     private static void logFailAfterTest() {
79         addTag(Status.FAIL, "Failure");
80         log(Status.ERROR, String.format(exceptionMsgFormat, Status.ERROR));
81         log(Status.ERROR, throwable);
82         takeScreenshot(Status.FAIL);
83     }
84
85     private static void logSkipAfterTest() {
86         addTag(Status.SKIP, "Skipped");
87         log(Status.SKIP, String.format(exceptionMsgFormat, Status.SKIP));
88         log(Status.SKIP, throwable);
89         takeScreenshot(Status.SKIP);
90     }
91
92     private static void logFatalAfterTest() {
93         addTag(Status.FATAL, "Fatal");
94         log(Status.FATAL, String.format(exceptionMsgFormat, Status.FATAL));
95         log(Status.FATAL, throwable);
96         takeScreenshot(Status.FATAL);
97     }
98
99     private static void takeScreenshot(final Status status) {
100         String adjustedTestName = testName;
101         String infoFromDataProvider = WindowTestManager.getWindowMap().getAddedValueFromDataProvider();
102         if (StringUtils.isNotEmpty(infoFromDataProvider)) {
103             infoFromDataProvider = infoFromDataProvider.replace(":", "-");
104             adjustedTestName = String.format("%s | %s", testName, infoFromDataProvider);
105         }
106         try {
107             addScreenshot(status, adjustedTestName, "Finished the test with the following screenshot:");
108         } catch (final IOException e) {
109             final String warnMsg = "Could not take screenshot of the final screen";
110             LOGGER.warn(warnMsg, e);
111             log(Status.WARNING, String.format("%s: %s", warnMsg, e.getMessage()));
112         }
113     }
114
115     private static void logFailure(final String suiteName) {
116         if (suiteNameXml.TESTNG_FAILED_XML_NAME.getValue().equals(suiteName)) {
117             logFatalAfterTest();
118         } else {
119             logFailAfterTest();
120         }
121     }
122
123 }
124