aboutsummaryrefslogtreecommitdiffstats
path: root/test-apis-ci/src/main/java/org/openecomp/sdc/ci/tests/api/AttSdcTest.java
blob: f9752febafc393f8b4514c73d94ceef325018424 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
/*-
 * ============LICENSE_START=======================================================
 * SDC
 * ================================================================================
 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
 * ================================================================================
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * ============LICENSE_END=========================================================
 */

package org.openecomp.sdc.ci.tests.api;

import static org.junit.Assert.assertTrue;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.util.List;

import org.junit.After;
import org.junit.AfterClass;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Rule;
import org.junit.rules.TestName;
import org.junit.rules.TestWatcher;
import org.openecomp.sdc.ci.tests.config.Config;
import org.openecomp.sdc.ci.tests.rules.MyTestWatcher;
import org.openecomp.sdc.ci.tests.run.StartTest;
import org.openecomp.sdc.ci.tests.utils.Utils;
import org.openecomp.sdc.ci.tests.utils.general.FileUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;

public abstract class AttSdcTest {

	public static StringBuilder doc = new StringBuilder();
	public static String file = null;
	public static Config config = null;
	// protected Gson gson = new Gson();
	protected Gson prettyGson = new GsonBuilder().setPrettyPrinting().create();

	protected TestName testName = null;
	protected Logger logger = null;

	protected static boolean displayException = false;

	public AttSdcTest(TestName testName, String className) {
		super();

		StartTest.enableLogger();

		this.testName = testName;
		this.logger = LoggerFactory.getLogger(className);

		String displayEx = System.getProperty("displayException");
		if (displayEx != null && Boolean.valueOf(displayEx).booleanValue()) {
			displayException = true;
		}

	}

	@Rule
	public TestWatcher tw = new MyTestWatcher(this);

	@BeforeClass
	public static void beforeClass() {
		doc = new StringBuilder();
		doc.append(
				"<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>");

		doc.append("<meta name=\"viewport\" content=\"width=device-width, initial-scale=1\">");
		doc.append(
				"<link rel=\"stylesheet\" href=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/css/bootstrap.min.css\">");

		doc.append("</head><body>");

		doc.append("<script src=\"https://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js\"></script>");
		doc.append("<script src=\"http://maxcdn.bootstrapcdn.com/bootstrap/3.3.4/js/bootstrap.min.js\"></script>");

		doc.append("<table>");

		doc.append("<tr>");
		doc.append("<th>").append("Test Name").append("</th>");
		doc.append("<th>").append("Status").append("</th>");
		doc.append("<th>").append("Message").append("</th>");

		if (displayException) {
			doc.append("<th>").append("Exception").append("</th>");
		}
		doc.append("</tr>");
	}

	@AfterClass
	public static void afterClass() {
		doc.append("<table>");
		// writeToFile("./" + ConfigAttOdlIt.REPORT_FILE , doc.toString());
		FileUtils.writeToFile(
				Config.instance().getOutputFolder() + File.separator + file + StartTest.timeOfTest + ".html",
				doc.toString());

	}

	@Before
	public void beforeTest() throws FileNotFoundException {
		file = FileUtils.getFileName(this.getClass().getName());
		config = Utils.getConfig();
		assertTrue(config != null);		
	}

	@After
	public void afterTest() throws FileNotFoundException {

	}

	public void addTestSummary(String testName, boolean isSuccess) {
		addTestSummary(testName, isSuccess, null);
	}

	public void addTestSummary(String testName, boolean isSuccess, Throwable exception) {

		String message = exception == null ? "" : exception.getMessage();

		String result = (isSuccess) ? "success" : "fail";
		doc.append("<tr>");
		doc.append("<td class=\"name\">").append(testName).append("</td>");
		doc.append("<td class=\"" + result + "\">").append(result).append("</td>");
		doc.append("<td class=\"message\">").append(message).append("</td>");

		if (displayException) {
			// doc.append("<td
			// class=\"message\">").append(convertExceptionToString(exception)).append("</td>");
			doc.append("<td class=\"message\">");

			doc.append("<button type=\"button\" class=\"btn btn-info\" data-toggle=\"collapse\" data-target=\"#demo"
					+ testName + "\">Simple collapsible</button>");
			doc.append("<div id=\"demo" + testName + "\" class=\"collapse out\">");

			doc.append(convertExceptionToString(exception));

			doc.append("</div>");
			doc.append("</td>");
		}

		doc.append("</tr>");

		if (isSuccess) {
			logger.debug("Test {} {}",testName,(isSuccess ? " SUCCEEDED " : " FAILED with error " + message));
		} else {
			logger.error("Test {} {}",testName,(isSuccess ? " SUCCEEDED " : " FAILED with error " + message));
		}
	}

	private String convertExceptionToString(Throwable exception) {

		if (exception == null) {
			return "";
		}

		StringWriter sw = new StringWriter();
		exception.printStackTrace(new PrintWriter(sw));
		String exceptionAsString = sw.toString();

		return exceptionAsString;
	}

	public Logger getLogger() {
		return logger;
	}

	protected boolean ignoreDueToBug(String bug) {

		List<String> bugs = config.getBugs();

		if (bugs != null && bugs.size() > 0) {
			for (String bugNumber : bugs) {
				if (bugNumber.startsWith(bug)) {
					return true;
				}
			}
		}

		return false;
	}

}