2 * ============LICENSE_START=======================================================
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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=========================================================
21 package org.openecomp.sdc.ci.tests.execute.setup;
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;
31 import java.io.IOException;
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;
37 public class ReportAfterTestManager {
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:";
44 private ReportAfterTestManager() {
48 public static void report(final ITestResult result, final ITestContext context) {
49 testName = result.getName();
50 throwable = result.getThrowable();
52 final String suiteName = ExtentManager.getSuiteName(context);
54 switch (result.getStatus()) {
55 case ITestResult.SUCCESS:
56 logSuccessAfterTest();
59 case ITestResult.FAILURE:
60 logFailure(suiteName);
63 case ITestResult.SKIP:
73 private static void logSuccessAfterTest() {
74 addTag(Status.PASS, "Success");
75 takeScreenshot(Status.PASS);
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);
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);
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);
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);
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()));
115 private static void logFailure(final String suiteName) {
116 if (suiteNameXml.TESTNG_FAILED_XML_NAME.getValue().equals(suiteName)) {