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 static org.junit.Assert.assertTrue;
26 import java.io.FileNotFoundException;
27 import java.io.PrintWriter;
28 import java.io.StringWriter;
29 import java.util.List;
31 import org.junit.After;
32 import org.junit.AfterClass;
33 import org.junit.Before;
34 import org.junit.BeforeClass;
35 import org.junit.Rule;
36 import org.junit.rules.TestName;
37 import org.junit.rules.TestWatcher;
38 import org.openecomp.sdc.ci.tests.config.Config;
39 import org.openecomp.sdc.ci.tests.execute.artifacts.CrudArt;
40 import org.openecomp.sdc.ci.tests.rules.MyTestWatcher;
41 import org.openecomp.sdc.ci.tests.run.StartTest;
42 import org.openecomp.sdc.ci.tests.utils.Utils;
43 import org.openecomp.sdc.ci.tests.utils.general.FileUtils;
44 import org.slf4j.Logger;
45 import org.slf4j.LoggerFactory;
47 import com.google.gson.Gson;
48 import com.google.gson.GsonBuilder;
50 public abstract class AttSdcTest {
52 public static StringBuilder doc = new StringBuilder();
53 public static String file = null;
54 public static Config config = null;
55 // protected Gson gson = new Gson();
56 protected Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
58 protected TestName testName = null;
60 protected static boolean displayException = false;
61 static Logger logger = LoggerFactory.getLogger(CrudArt.class.getName());
64 public AttSdcTest(TestName testName, String className) {
67 StartTest.enableLogger();
69 this.testName = testName;
71 String displayEx = System.getProperty("displayException");
72 if (displayEx != null && Boolean.valueOf(displayEx).booleanValue()) {
73 displayException = true;
79 public TestWatcher tw = new MyTestWatcher(this);
82 public static void beforeClass() {
83 doc = new StringBuilder();
85 "<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>");
87 doc.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
89 "<link rel=\"stylesheet\" href=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">");
91 doc.append("</head><body>");
93 doc.append("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js\"></script>");
94 doc.append("<script src=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js\"></script>");
96 doc.append("<table>");
99 doc.append("<th>").append("Test Name").append("</th>");
100 doc.append("<th>").append("Status").append("</th>");
101 doc.append("<th>").append("Message").append("</th>");
103 if (displayException) {
104 doc.append("<th>").append("Exception").append("</th>");
110 public static void afterClass() {
111 doc.append("<table>");
112 // writeToFile("./" + ConfigAttOdlIt.REPORT_FILE , doc.toString());
113 FileUtils.writeToFile(
114 Config.instance().getOutputFolder() + File.separator + file + StartTest.timeOfTest + ".html",
120 public void beforeTest() throws FileNotFoundException {
121 file = FileUtils.getFileName(this.getClass().getName());
122 config = Utils.getConfig();
123 assertTrue(config != null);
125 logger.info("Start running test {}", testName.getMethodName());
129 public void afterTest() throws FileNotFoundException {
131 logger.info("Finish running test {}", testName.getMethodName());
134 public void addTestSummary(String testName, boolean isSuccess) {
135 addTestSummary(testName, isSuccess, null);
138 public void addTestSummary(String testName, boolean isSuccess, Throwable exception) {
140 String message = exception == null ? "" : exception.getMessage();
142 String result = (isSuccess) ? "success" : "fail";
144 doc.append("<td class=\"name\">").append(testName).append("</td>");
145 doc.append("<td class=\"" + result + "\">").append(result).append("</td>");
146 doc.append("<td class=\"message\">").append(message).append("</td>");
148 if (displayException) {
150 // class=\"message\">").append(convertExceptionToString(exception)).append("</td>");
151 doc.append("<td class=\"message\">");
153 doc.append("<button type=\"button\" class=\"btn btn-info\" data-toggle=\"collapse\" data-target=\"#demo"
154 + testName + "\">Simple collapsible</button>");
155 doc.append("<div id=\"demo" + testName + "\" class=\"collapse out\">");
157 doc.append(convertExceptionToString(exception));
159 doc.append("</div>");
166 logger.debug("Test {} {}", testName, (isSuccess ? " SUCCEEDED " : " FAILED with error " + message));
168 logger.error("Test {} {}", testName, (isSuccess ? " SUCCEEDED " : " FAILED with error " + message));
172 private String convertExceptionToString(Throwable exception) {
174 if (exception == null) {
178 StringWriter sw = new StringWriter();
179 exception.printStackTrace(new PrintWriter(sw));
180 String exceptionAsString = sw.toString();
182 return exceptionAsString;
185 public Logger getLogger() {
189 protected boolean ignoreDueToBug(String bug) {
191 List<String> bugs = config.getBugs();
193 if (bugs != null && bugs.size() > 0) {
194 for (String bugNumber : bugs) {
195 if (bugNumber.startsWith(bug)) {