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.api;
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
26 import org.junit.rules.TestName;
27 import org.junit.rules.TestWatcher;
28 import org.openecomp.sdc.ci.tests.config.Config;
29 import org.openecomp.sdc.ci.tests.rules.MyTestWatcher;
30 import org.openecomp.sdc.ci.tests.run.StartTest;
31 import org.openecomp.sdc.ci.tests.utils.Utils;
32 import org.openecomp.sdc.ci.tests.utils.general.FileUtils;
33 import org.slf4j.Logger;
34 import org.slf4j.LoggerFactory;
37 import java.io.FileNotFoundException;
38 import java.io.PrintWriter;
39 import java.io.StringWriter;
40 import java.util.List;
42 import static org.junit.Assert.assertTrue;
44 public abstract class AttSdcTest {
46 public static StringBuilder doc = new StringBuilder();
47 public static String file = null;
48 public static Config config = null;
49 // protected Gson gson = new Gson();
50 protected Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
52 protected TestName testName = null;
53 protected Logger logger = null;
55 protected static boolean displayException = false;
57 public AttSdcTest(TestName testName, String className) {
60 StartTest.enableLogger();
62 this.testName = testName;
63 this.logger = LoggerFactory.getLogger(className);
65 String displayEx = System.getProperty("displayException");
66 if (displayEx != null && Boolean.valueOf(displayEx).booleanValue()) {
67 displayException = true;
73 public TestWatcher tw = new MyTestWatcher(this);
76 public static void beforeClass() {
77 doc = new StringBuilder();
79 "<Html><head><style>th{background-color: gray;color: white;height: 30px;}td {color: black;height: 30px;}.fail {background-color: #FF5555;width: 100px;text-align: center;}.success {background-color: #00FF00;width: 100px;text-align: center;}.name {width: 200px;background-color: #F0F0F0;}.message {width: 300px;background-color: #F0F0F0;}</style>");
81 doc.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
83 "<link rel=\"stylesheet\" href=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">");
85 doc.append("</head><body>");
87 doc.append("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js\"></script>");
88 doc.append("<script src=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js\"></script>");
90 doc.append("<table>");
93 doc.append("<th>").append("Test Name").append("</th>");
94 doc.append("<th>").append("Status").append("</th>");
95 doc.append("<th>").append("Message").append("</th>");
97 if (displayException) {
98 doc.append("<th>").append("Exception").append("</th>");
104 public static void afterClass() {
105 doc.append("<table>");
106 // writeToFile("./" + ConfigAttOdlIt.REPORT_FILE , doc.toString());
107 FileUtils.writeToFile(
108 Config.instance().getOutputFolder() + File.separator + file + StartTest.timeOfTest + ".html",
114 public void beforeTest() throws FileNotFoundException {
115 file = FileUtils.getFileName(this.getClass().getName());
116 config = Utils.getConfig();
117 assertTrue(config != null);
121 public void afterTest() throws FileNotFoundException {
125 public void addTestSummary(String testName, boolean isSuccess) {
126 addTestSummary(testName, isSuccess, null);
129 public void addTestSummary(String testName, boolean isSuccess, Throwable exception) {
131 String message = exception == null ? "" : exception.getMessage();
133 String result = (isSuccess) ? "success" : "fail";
135 doc.append("<td class=\"name\">").append(testName).append("</td>");
136 doc.append("<td class=\"" + result + "\">").append(result).append("</td>");
137 doc.append("<td class=\"message\">").append(message).append("</td>");
139 if (displayException) {
141 // class=\"message\">").append(convertExceptionToString(exception)).append("</td>");
142 doc.append("<td class=\"message\">");
144 doc.append("<button type=\"button\" class=\"btn btn-info\" data-toggle=\"collapse\" data-target=\"#demo"
145 + testName + "\">Simple collapsible</button>");
146 doc.append("<div id=\"demo" + testName + "\" class=\"collapse out\">");
148 doc.append(convertExceptionToString(exception));
150 doc.append("</div>");
157 logger.debug("Test {} {}",testName,(isSuccess ? " SUCCEEDED " : " FAILED with error " + message));
159 logger.error("Test {} {}",testName,(isSuccess ? " SUCCEEDED " : " FAILED with error " + message));
163 private String convertExceptionToString(Throwable exception) {
165 if (exception == null) {
169 StringWriter sw = new StringWriter();
170 exception.printStackTrace(new PrintWriter(sw));
171 String exceptionAsString = sw.toString();
173 return exceptionAsString;
176 public Logger getLogger() {
180 protected boolean ignoreDueToBug(String bug) {
182 List<String> bugs = config.getBugs();
184 if (bugs != null && bugs.size() > 0) {
185 for (String bugNumber : bugs) {
186 if (bugNumber.startsWith(bug)) {