1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package dev.aherscu.qa.testrail.reporter;
18
19 import static java.net.HttpURLConnection.*;
20
21 import java.io.*;
22 import java.net.*;
23 import java.nio.charset.*;
24 import java.util.*;
25
26 import org.json.simple.*;
27
28 import lombok.*;
29
30 public class TestRailClient {
31
32 private final String m_url;
33 private String m_user;
34 private String m_password;
35
36 public TestRailClient(String base_url) {
37 if (!base_url.endsWith("/")) {
38 base_url += "/";
39 }
40
41 this.m_url = base_url + "index.php?/api/v2/";
42 }
43
44 private static String getAuthorization(String user, String password) {
45 return new String(
46 Base64.getEncoder().encode((user + ":" + password).getBytes()));
47 }
48
49
50
51
52
53
54
55
56 public String getPassword() {
57 return this.m_password;
58 }
59
60 public void setPassword(String password) {
61 this.m_password = password;
62 }
63
64
65
66
67
68
69
70
71 public String getUser() {
72 return this.m_user;
73 }
74
75 public void setUser(String user) {
76 this.m_user = user;
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 public Object sendGet(String uri, String data) {
103 return this.sendRequest("GET", uri, data);
104 }
105
106 public Object sendGet(String uri) {
107 return this.sendRequest("GET", uri, null);
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 public Object sendPost(String uri, Object data) {
134 return this.sendRequest("POST", uri, data);
135 }
136
137 @SneakyThrows
138 private Object sendRequest(String method, String uri, Object data) {
139 URL url = new URL(this.m_url + uri);
140
141
142 HttpURLConnection conn = (HttpURLConnection) url.openConnection();
143
144 String auth = getAuthorization(this.m_user, this.m_password);
145 conn.addRequestProperty("Authorization", "Basic " + auth);
146
147 if (method.equals("POST")) {
148 conn.setRequestMethod("POST");
149
150
151
152 if (data != null) {
153 if (uri.startsWith(
154 "add_attachment"))
155 {
156 String boundary =
157 "TestRailAPIAttachmentBoundary";
158
159 File uploadFile = new File((String) data);
160
161 conn.setDoOutput(true);
162 conn.addRequestProperty("Content-Type",
163 "multipart/form-data; boundary=" + boundary);
164
165 OutputStream ostreamBody = conn.getOutputStream();
166 BufferedWriter bodyWriter =
167 new BufferedWriter(new OutputStreamWriter(ostreamBody));
168
169 bodyWriter.write("\n\n--" + boundary + "\r\n");
170 bodyWriter.write(
171 "Content-Disposition: form-data; name=\"attachment\"; filename=\""
172 + uploadFile.getName() + "\"");
173 bodyWriter.write("\r\n\r\n");
174 bodyWriter.flush();
175
176
177 InputStream istreamFile = new FileInputStream(uploadFile);
178 int bytesRead;
179 byte[] dataBuffer = new byte[1024];
180 while ((bytesRead = istreamFile.read(dataBuffer)) != -1) {
181 ostreamBody.write(dataBuffer, 0, bytesRead);
182 }
183
184 ostreamBody.flush();
185
186
187 bodyWriter.write("\r\n--" + boundary + "--\r\n");
188 bodyWriter.flush();
189
190
191 istreamFile.close();
192 ostreamBody.close();
193 bodyWriter.close();
194 } else
195 {
196 conn.addRequestProperty("Content-Type", "application/json");
197 byte[] block =
198 JSONValue.toJSONString(data).getBytes(
199 StandardCharsets.UTF_8);
200
201 conn.setDoOutput(true);
202 OutputStream ostream = conn.getOutputStream();
203 ostream.write(block);
204 ostream.close();
205 }
206 }
207 } else
208 {
209 conn.addRequestProperty("Content-Type", "application/json");
210 }
211
212
213
214
215 int status = conn.getResponseCode();
216
217 InputStream istream;
218 if (status != HTTP_OK) {
219 istream = conn.getErrorStream();
220 if (istream == null) {
221 throw new RuntimeException(
222 "TestRail API return HTTP " + status +
223 " (No additional error message received)");
224 }
225 } else {
226 istream = conn.getInputStream();
227 }
228
229
230
231 if ((istream != null)
232 && (uri.startsWith("get_attachment/"))) {
233 FileOutputStream outputStream = new FileOutputStream((String) data);
234
235 int bytesRead = 0;
236 byte[] buffer = new byte[1024];
237 while ((bytesRead = istream.read(buffer)) > 0) {
238 outputStream.write(buffer, 0, bytesRead);
239 }
240
241 outputStream.close();
242 istream.close();
243 return data;
244 }
245
246
247
248 String text = "";
249 if (istream != null) {
250 BufferedReader reader = new BufferedReader(
251 new InputStreamReader(
252 istream,
253 StandardCharsets.UTF_8));
254
255 String line;
256 while ((line = reader.readLine()) != null) {
257 text += line;
258 text += System.getProperty("line.separator");
259 }
260
261 reader.close();
262 }
263
264 Object result;
265 if (!text.equals("")) {
266 result = JSONValue.parse(text);
267 } else {
268 result = new JSONObject();
269 }
270
271
272
273
274 if (status != HTTP_OK) {
275 String error = "No additional error message received";
276 if (result instanceof JSONObject) {
277 JSONObject obj = (JSONObject) result;
278 if (obj.containsKey("error")) {
279 error = '"' + (String) obj.get("error") + '"';
280 }
281 }
282
283 throw new RuntimeException(
284 "TestRail API returned HTTP " + status +
285 "(" + error + ")");
286 }
287
288 return result;
289 }
290 }