Fix for radio buttons
[sdc.git] / asdc-tests / 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.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;
46
47 import com.google.gson.Gson;
48 import com.google.gson.GsonBuilder;
49
50 public abstract class AttSdcTest {
51
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();
57
58         protected TestName testName = null;
59
60         protected static boolean displayException = false;
61         static Logger logger = LoggerFactory.getLogger(CrudArt.class.getName());
62         
63         
64         public AttSdcTest(TestName testName, String className) {
65                 super();
66
67                 StartTest.enableLogger();
68
69                 this.testName = testName;
70
71                 String displayEx = System.getProperty("displayException");
72                 if (displayEx != null && Boolean.valueOf(displayEx).booleanValue()) {
73                         displayException = true;
74                 }
75
76         }
77
78         @Rule
79         public TestWatcher tw = new MyTestWatcher(this);
80
81         @BeforeClass
82         public static void beforeClass() {
83                 doc = new StringBuilder();
84                 doc.append(
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>");
86
87                 doc.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
88                 doc.append(
89                                 "<link rel=\"stylesheet\" href=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">");
90
91                 doc.append("</head><body>");
92
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>");
95
96                 doc.append("<table>");
97
98                 doc.append("<tr>");
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>");
102
103                 if (displayException) {
104                         doc.append("<th>").append("Exception").append("</th>");
105                 }
106                 doc.append("</tr>");
107         }
108
109         @AfterClass
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",
115                                 doc.toString());
116
117         }
118
119         @Before
120         public void beforeTest() throws FileNotFoundException {
121                 file = FileUtils.getFileName(this.getClass().getName());
122                 config = Utils.getConfig();
123                 assertTrue(config != null);
124
125                 logger.info("Start running test {}", testName.getMethodName());
126         }
127
128         @After
129         public void afterTest() throws FileNotFoundException {
130
131                 logger.info("Finish running test {}", testName.getMethodName());
132         }
133
134         public void addTestSummary(String testName, boolean isSuccess) {
135                 addTestSummary(testName, isSuccess, null);
136         }
137
138         public void addTestSummary(String testName, boolean isSuccess, Throwable exception) {
139
140                 String message = exception == null ? "" : exception.getMessage();
141
142                 String result = (isSuccess) ? "success" : "fail";
143                 doc.append("<tr>");
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>");
147
148                 if (displayException) {
149                         // doc.append("<td
150                         // class=\"message\">").append(convertExceptionToString(exception)).append("</td>");
151                         doc.append("<td class=\"message\">");
152
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\">");
156
157                         doc.append(convertExceptionToString(exception));
158
159                         doc.append("</div>");
160                         doc.append("</td>");
161                 }
162
163                 doc.append("</tr>");
164
165                 if (isSuccess) {
166                         logger.debug("Test {} {}", testName, (isSuccess ? " SUCCEEDED " : " FAILED with error " + message));
167                 } else {
168                         logger.error("Test {} {}", testName, (isSuccess ? " SUCCEEDED " : " FAILED with error " + message));
169                 }
170         }
171
172         private String convertExceptionToString(Throwable exception) {
173
174                 if (exception == null) {
175                         return "";
176                 }
177
178                 StringWriter sw = new StringWriter();
179                 exception.printStackTrace(new PrintWriter(sw));
180                 String exceptionAsString = sw.toString();
181
182                 return exceptionAsString;
183         }
184
185         public Logger getLogger() {
186                 return logger;
187         }
188
189         protected boolean ignoreDueToBug(String bug) {
190
191                 List<String> bugs = config.getBugs();
192
193                 if (bugs != null && bugs.size() > 0) {
194                         for (String bugNumber : bugs) {
195                                 if (bugNumber.startsWith(bug)) {
196                                         return true;
197                                 }
198                         }
199                 }
200
201                 return false;
202         }
203
204 }