-
Notifications
You must be signed in to change notification settings - Fork 0
/
VansahBinding.java
184 lines (158 loc) · 6.27 KB
/
VansahBinding.java
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
import internal.GlobalVariable;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.HttpClientBuilder;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.util.EntityUtils;
import org.json.JSONObject;
import java.util.regex.Pattern;
/**
* Class to send results to Vansah.
*/
public class VansahBinding {
/**
* The base URL of the Vansah API.
*/
private String apiURL = GlobalVariable.Vansah_URL.toString();
/**
* The version of the Vansah API.
*/
private String apiVersion = "v1";
/**
* The endpoint URL for adding a test run.
* This URL is constructed by appending the specific API operation path to the base Vansah URL and API version,
* facilitating the creation of new test runs in the Vansah system.
*/
private String addTestRun = apiURL + "/api/" + apiVersion + "/run";
/**
* The name of the sprint.
*/
private String sprintName = GlobalVariable.SprintName.toString();
/**
* The name of the release.
*/
private String releaseName = GlobalVariable.ReleaseName.toString();
/**
* The name of the environment.
*/
private String environmentName = GlobalVariable.EnvironmentName.toString();
/**
* The HTTP client used to execute HTTP requests.
*/
private CloseableHttpClient httpClient = null;
/**
* The HTTP POST request used to send data to the Vansah API.
*/
private HttpPost httpPost = null;
/**
* Sends results to Vansah.
*
* @param testCaseKey The key of the test case.
* @param assetKey The key of the asset (issue key or folder identifier).
* @param result The result of the test run.
*/
public void sendResultstoVansah(String testCaseKey, String assetKey, String result) {
httpClient = HttpClientBuilder.create().build();
httpPost = new HttpPost(addTestRun);
httpPost.addHeader("Authorization", System.getenv("VANSAH_TOKEN"));
httpPost.addHeader("Content-Type", "application/json");
try {
String requestBody = requestBodyJSON(testCaseKey, assetKey, filterResult(result)).toString();
StringEntity entity = new StringEntity(requestBody);
httpPost.setEntity(entity);
// Execute the request and get the response
HttpResponse response = httpClient.execute(httpPost);
// Get the response status code
int statusCode = response.getStatusLine().getStatusCode();
System.out.println("Vansah Response status code: " + statusCode);
// Get the response body
HttpEntity responseEntity = response.getEntity();
String responseBody = EntityUtils.toString(responseEntity);
System.out.println("Vansah Response Message: " + new JSONObject(responseBody).getString("message"));
// Close the response
EntityUtils.consume(responseEntity);
} catch (Exception e) {
e.printStackTrace();
} finally {
try {
// Close the HttpClient
httpClient.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
/**
* Constructs the JSON request body.
*
* @param testCaseKey The key of the test case.
* @param asset The key of the asset (issue key or folder identifier).
* @param result The result of the test run.
* @return The JSON request body.
*/
private JSONObject requestBodyJSON(String testCaseKey, String asset, String result) {
JSONObject requestBody = new JSONObject();
JSONObject caseObject = new JSONObject();
JSONObject assetObject = new JSONObject();
JSONObject resultObject = new JSONObject();
JSONObject propertiesObject = new JSONObject();
JSONObject environmentObject = new JSONObject();
JSONObject releaseObject = new JSONObject();
JSONObject sprintObject = new JSONObject();
// Create caseObject
caseObject.put("key", testCaseKey);
requestBody.put("case", caseObject);
// Create assetObject
if (isIssueKey(asset)) {
assetObject.put("type", "issue");
assetObject.put("key", asset);
} else {
assetObject.put("type", "folder");
assetObject.put("identifier", asset);
}
requestBody.put("asset", assetObject);
// Create resultObject
resultObject.put("name", result);
requestBody.put("result", resultObject);
// Create Test Run Properties Object
if (sprintName.trim().length() != 0) {
sprintObject.put("name", sprintName);
propertiesObject.put("sprint", sprintObject);
}
if (releaseName.trim().length() != 0) {
releaseObject.put("name", releaseName);
propertiesObject.put("release", releaseObject);
}
if (environmentName.trim().length() != 0) {
environmentObject.put("name", environmentName);
propertiesObject.put("environment", environmentObject);
}
requestBody.put("properties", propertiesObject);
return requestBody;
}
/**
* Checks if the provided key is an issue key.
*
* @param key The key to check.
* @return {@code true} if the key is an issue key, {@code false} otherwise.
*/
private boolean isIssueKey(String key) {
// Check if the key matches the pattern of an Issue Key (e.g., "ABC-123")
return Pattern.matches("[A-Z]+-[0-9]+", key);
}
/**
* Filters the input result to determine its status.
*
* @param result The input result to be filtered. This should be a string representing the status of a test or evaluation.
* @return A string representing the filtered status of the input result. If the input result is "passed" (case-insensitive), the method returns "passed". Otherwise, it returns "failed".
*/
private String filterResult(String result) {
if(result.toLowerCase().equals("passed")) {
return "passed";
} else {
return "failed";
}
}
}