-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathKatalonTestOpsHelper.java
More file actions
206 lines (179 loc) · 6.9 KB
/
KatalonTestOpsHelper.java
File metadata and controls
206 lines (179 loc) · 6.9 KB
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
201
202
203
204
205
206
package com.katalon.jenkins.plugin.helper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.katalon.jenkins.plugin.entity.BuildInfo;
import com.katalon.jenkins.plugin.entity.Job;
import com.katalon.jenkins.plugin.entity.JobStatus;
import com.katalon.jenkins.plugin.entity.TestProject;
import com.katalon.utils.Logger;
import org.apache.commons.io.IOUtils;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpResponseException;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.utils.URIBuilder;
import org.apache.http.message.BasicNameValuePair;
import java.io.InputStream;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.util.Arrays;
import java.util.Base64;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;
public class KatalonTestOpsHelper {
private static String TOKEN_URI = "/oauth/token";
private static String EXECUTE_JOB = "/api/v1/run-configurations/%s/execute";
private static String GET_LOG = "/api/v1/jobs/%s";
private static String LOG_INFOR = "/team/%s/project/%s/grid/plan/%s/job/%s";
private static String serverApiOAuth2GrantType = "password";
private static String serverApiOAuth2ClientId = "kit";
private static String serverApiOAuth2ClientSecret = "kit";
private ObjectMapper objectMapper;
private Logger logger;
public KatalonTestOpsHelper() {
init();
}
public KatalonTestOpsHelper(Logger logger) {
this.logger = logger;
init();
}
private void init() {
objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
public boolean run(String serverUrl, String apiKey, String plan, String projectId) {
try {
String token = requestToken(serverUrl, apiKey);
if (token != null) {
BuildInfo buildInfo = runJob(token, serverUrl, plan);
if (buildInfo == null) {
logger.info("Cannot create job");
return false;
}
long jobId = buildInfo.getJob_id();
TimeUnit.SECONDS.sleep(1);
//Wait for execute job is done.
JobStatus jobStatus = null;
while (true) {
Job job = getJob(token, serverUrl, jobId);
if (job == null) {
logger.info("Cannot get job from Katalon TestOps");
break;
}
if (jobStatus == null) {
logger.info("Job Detail: " + getJobUrl(serverUrl, job, plan));
}
JobStatus jobStatusCurrent = job.getStatus();
if (jobStatusCurrent == jobStatus) {
jobStatus = jobStatusCurrent;
continue;
}
jobStatus = jobStatusCurrent;
if (JobStatus.getRunningStatuses().contains(jobStatus)) {
logger.info("Job Status: " + jobStatus);
} else {
logger.info("Job Status: " + jobStatus);
logger.info("Execute done.");
if (jobStatus == JobStatus.SUCCESS) {
return true;
}
return false;
}
TimeUnit.SECONDS.sleep(30);
}
}
} catch (Exception e) {
logger.info(e.getMessage());
return false;
}
return false;
}
private String getJobUrl(String serverUrl, Job job, Object plainId) {
TestProject testProject = job.getTestProject();
return String.format(serverUrl + LOG_INFOR, testProject.getTeamId(), testProject.getProjectId(), plainId, job.getOrder());
}
private Job getJob(String token, String serverUrl, long jobId) {
String url = String.format(serverUrl + GET_LOG, jobId);
try {
URIBuilder uriBuilder = new URIBuilder(url);
HttpGet httpGet = new HttpGet(uriBuilder.build());
httpGet.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
HttpResponse httpResponse = HttpHelper.sendRequest(
httpGet,
token,
null,
null,
null,
null,
null);
InputStream responseContentStream = httpResponse.getEntity().getContent();
StringWriter writer = new StringWriter();
IOUtils.copy(responseContentStream, writer, StandardCharsets.UTF_8);
String responseContent = writer.toString();
logger.info("Response: " + responseContent);
return objectMapper.readValue(responseContent, Job.class);
} catch (Exception e) {
logger.info(e.getMessage());
return null;
}
}
private BuildInfo runJob(String token, String serverUrl, Object planId) {
String url = String.format(serverUrl + EXECUTE_JOB, planId);
try {
URIBuilder uriBuilder = new URIBuilder(url);
HttpPut httpPut = new HttpPut(uriBuilder.build());
httpPut.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
HttpResponse httpResponse = HttpHelper.sendRequest(
httpPut,
token,
null,
null,
null,
null,
null);
InputStream responseContentStream = httpResponse.getEntity().getContent();
StringWriter writer = new StringWriter();
IOUtils.copy(responseContentStream, writer, StandardCharsets.UTF_8);
String responseContent = writer.toString();
logger.info("Response: " + responseContent);
BuildInfo[] buildInfos = objectMapper.readValue(responseContent, BuildInfo[].class);
return buildInfos[0];
} catch (Exception e) {
logger.info(e.getMessage());
return null;
}
}
public String requestToken(String serverUrl, String apiKey) throws Exception {
String url = serverUrl + TOKEN_URI;
URIBuilder uriBuilder = new URIBuilder(url);
List<NameValuePair> pairs = Arrays.asList(
new BasicNameValuePair("username", ""),
new BasicNameValuePair("password", apiKey),
new BasicNameValuePair("grant_type", serverApiOAuth2GrantType)
);
HttpPost httpPost = new HttpPost(uriBuilder.build());
String clientCredentials = serverApiOAuth2ClientId + ":" + serverApiOAuth2ClientSecret;
httpPost.setHeader(HttpHeaders.AUTHORIZATION, "Basic " +
Base64.getEncoder().encodeToString(clientCredentials.getBytes(StandardCharsets.UTF_8)));
HttpResponse httpResponse = HttpHelper.sendRequest(
httpPost,
null,
serverApiOAuth2ClientId,
serverApiOAuth2ClientSecret,
null,
null,
pairs);
InputStream content = httpResponse.getEntity().getContent();
ObjectMapper objectMapper = new ObjectMapper();
Map map = objectMapper.readValue(content, Map.class);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode >= 300) {
throw new HttpResponseException(statusCode, (String) map.get("error_description"));
}
return (String) map.get("access_token");
}
}