re base code
[sdc.git] / test-apis-ci / src / main / java / org / openecomp / sdc / ci / tests / api / AttSdcTest.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.api;
22
23 import com.google.gson.Gson;
24 import com.google.gson.GsonBuilder;
25 import org.junit.*;
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;
35
36 import java.io.File;
37 import java.io.FileNotFoundException;
38 import java.io.PrintWriter;
39 import java.io.StringWriter;
40 import java.util.List;
41
42 import static org.junit.Assert.assertTrue;
43
44 public abstract class AttSdcTest {
45
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();
51
52         protected TestName testName = null;
53         protected Logger logger = null;
54
55         protected static boolean displayException = false;
56
57         public AttSdcTest(TestName testName, String className) {
58                 super();
59
60                 StartTest.enableLogger();
61
62                 this.testName = testName;
63                 this.logger = LoggerFactory.getLogger(className);
64
65                 String displayEx = System.getProperty("displayException");
66                 if (displayEx != null && Boolean.valueOf(displayEx).booleanValue()) {
67                         displayException = true;
68                 }
69
70         }
71
72         @Rule
73         public TestWatcher tw = new MyTestWatcher(this);
74
75         @BeforeClass
76         public static void beforeClass() {
77                 doc = new StringBuilder();
78                 doc.append(
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>");
80
81                 doc.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
82                 doc.append(
83                                 "<link rel=\"stylesheet\" href=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">");
84
85                 doc.append("</head><body>");
86
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>");
89
90                 doc.append("<table>");
91
92                 doc.append("<tr>");
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>");
96
97                 if (displayException) {
98                         doc.append("<th>").append("Exception").append("</th>");
99                 }
100                 doc.append("</tr>");
101         }
102
103         @AfterClass
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",
109                                 doc.toString());
110
111         }
112
113         @Before
114         public void beforeTest() throws FileNotFoundException {
115                 file = FileUtils.getFileName(this.getClass().getName());
116                 config = Utils.getConfig();
117                 assertTrue(config != null);             
118         }
119
120         @After
121         public void afterTest() throws FileNotFoundException {
122
123         }
124
125         public void addTestSummary(String testName, boolean isSuccess) {
126                 addTestSummary(testName, isSuccess, null);
127         }
128
129         public void addTestSummary(String testName, boolean isSuccess, Throwable exception) {
130
131                 String message = exception == null ? "" : exception.getMessage();
132
133                 String result = (isSuccess) ? "success" : "fail";
134                 doc.append("<tr>");
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>");
138
139                 if (displayException) {
140                         // doc.append("<td
141                         // class=\"message\">").append(convertExceptionToString(exception)).append("</td>");
142                         doc.append("<td class=\"message\">");
143
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\">");
147
148                         doc.append(convertExceptionToString(exception));
149
150                         doc.append("</div>");
151                         doc.append("</td>");
152                 }
153
154                 doc.append("</tr>");
155
156                 if (isSuccess) {
157                         logger.debug("Test {} {}",testName,(isSuccess ? " SUCCEEDED " : " FAILED with error " + message));
158                 } else {
159                         logger.error("Test {} {}",testName,(isSuccess ? " SUCCEEDED " : " FAILED with error " + message));
160                 }
161         }
162
163         private String convertExceptionToString(Throwable exception) {
164
165                 if (exception == null) {
166                         return "";
167                 }
168
169                 StringWriter sw = new StringWriter();
170                 exception.printStackTrace(new PrintWriter(sw));
171                 String exceptionAsString = sw.toString();
172
173                 return exceptionAsString;
174         }
175
176         public Logger getLogger() {
177                 return logger;
178         }
179
180         protected boolean ignoreDueToBug(String bug) {
181
182                 List<String> bugs = config.getBugs();
183
184                 if (bugs != null && bugs.size() > 0) {
185                         for (String bugNumber : bugs) {
186                                 if (bugNumber.startsWith(bug)) {
187                                         return true;
188                                 }
189                         }
190                 }
191
192                 return false;
193         }
194
195 }