[SDC-29] rebase continue work to align source
[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 static org.junit.Assert.assertTrue;
24
25 import java.io.File;
26 import java.io.FileNotFoundException;
27 import java.io.PrintWriter;
28 import java.io.StringWriter;
29 import java.util.List;
30
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.rules.MyTestWatcher;
40 import org.openecomp.sdc.ci.tests.run.StartTest;
41 import org.openecomp.sdc.ci.tests.utils.Utils;
42 import org.openecomp.sdc.ci.tests.utils.general.FileUtils;
43 import org.slf4j.Logger;
44 import org.slf4j.LoggerFactory;
45
46 import com.google.gson.Gson;
47 import com.google.gson.GsonBuilder;
48
49 public abstract class AttSdcTest {
50
51         public static StringBuilder doc = new StringBuilder();
52         public static String file = null;
53         public static Config config = null;
54         // protected Gson gson = new Gson();
55         protected Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();
56
57         protected TestName testName = null;
58         protected Logger logger = null;
59
60         protected static boolean displayException = false;
61
62         public AttSdcTest(TestName testName, String className) {
63                 super();
64
65                 StartTest.enableLogger();
66
67                 this.testName = testName;
68                 this.logger = LoggerFactory.getLogger(className);
69
70                 String displayEx = System.getProperty("displayException");
71                 if (displayEx != null && Boolean.valueOf(displayEx).booleanValue()) {
72                         displayException = true;
73                 }
74
75         }
76
77         @Rule
78         public TestWatcher tw = new MyTestWatcher(this);
79
80         @BeforeClass
81         public static void beforeClass() {
82                 doc = new StringBuilder();
83                 doc.append(
84                                 "<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>");
85
86                 doc.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
87                 doc.append(
88                                 "<link rel=\"stylesheet\" href=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">");
89
90                 doc.append("</head><body>");
91
92                 doc.append("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js\"></script>");
93                 doc.append("<script src=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js\"></script>");
94
95                 doc.append("<table>");
96
97                 doc.append("<tr>");
98                 doc.append("<th>").append("Test Name").append("</th>");
99                 doc.append("<th>").append("Status").append("</th>");
100                 doc.append("<th>").append("Message").append("</th>");
101
102                 if (displayException) {
103                         doc.append("<th>").append("Exception").append("</th>");
104                 }
105                 doc.append("</tr>");
106         }
107
108         @AfterClass
109         public static void afterClass() {
110                 doc.append("<table>");
111                 // writeToFile("./" + ConfigAttOdlIt.REPORT_FILE , doc.toString());
112                 FileUtils.writeToFile(
113                                 Config.instance().getOutputFolder() + File.separator + file + StartTest.timeOfTest + ".html",
114                                 doc.toString());
115
116         }
117
118         @Before
119         public void beforeTest() throws FileNotFoundException {
120                 file = FileUtils.getFileName(this.getClass().getName());
121                 config = Utils.getConfig();
122                 assertTrue(config != null);             
123         }
124
125         @After
126         public void afterTest() throws FileNotFoundException {
127
128         }
129
130         public void addTestSummary(String testName, boolean isSuccess) {
131                 addTestSummary(testName, isSuccess, null);
132         }
133
134         public void addTestSummary(String testName, boolean isSuccess, Throwable exception) {
135
136                 String message = exception == null ? "" : exception.getMessage();
137
138                 String result = (isSuccess) ? "success" : "fail";
139                 doc.append("<tr>");
140                 doc.append("<td class=\"name\">").append(testName).append("</td>");
141                 doc.append("<td class=\"" + result + "\">").append(result).append("</td>");
142                 doc.append("<td class=\"message\">").append(message).append("</td>");
143
144                 if (displayException) {
145                         // doc.append("<td
146                         // class=\"message\">").append(convertExceptionToString(exception)).append("</td>");
147                         doc.append("<td class=\"message\">");
148
149                         doc.append("<button type=\"button\" class=\"btn btn-info\" data-toggle=\"collapse\" data-target=\"#demo"
150                                         + testName + "\">Simple collapsible</button>");
151                         doc.append("<div id=\"demo" + testName + "\" class=\"collapse out\">");
152
153                         doc.append(convertExceptionToString(exception));
154
155                         doc.append("</div>");
156                         doc.append("</td>");
157                 }
158
159                 doc.append("</tr>");
160
161                 if (isSuccess) {
162                         logger.debug("Test {} {}",testName,(isSuccess ? " SUCCEEDED " : " FAILED with error " + message));
163                 } else {
164                         logger.error("Test {} {}",testName,(isSuccess ? " SUCCEEDED " : " FAILED with error " + message));
165                 }
166         }
167
168         private String convertExceptionToString(Throwable exception) {
169
170                 if (exception == null) {
171                         return "";
172                 }
173
174                 StringWriter sw = new StringWriter();
175                 exception.printStackTrace(new PrintWriter(sw));
176                 String exceptionAsString = sw.toString();
177
178                 return exceptionAsString;
179         }
180
181         public Logger getLogger() {
182                 return logger;
183         }
184
185         protected boolean ignoreDueToBug(String bug) {
186
187                 List<String> bugs = config.getBugs();
188
189                 if (bugs != null && bugs.size() > 0) {
190                         for (String bugNumber : bugs) {
191                                 if (bugNumber.startsWith(bug)) {
192                                         return true;
193                                 }
194                         }
195                 }
196
197                 return false;
198         }
199
200 }