-
-
Notifications
You must be signed in to change notification settings - Fork 13
Expand file tree
/
Copy pathKatalonTestOpsSearchHelper.java
More file actions
84 lines (71 loc) · 3.04 KB
/
KatalonTestOpsSearchHelper.java
File metadata and controls
84 lines (71 loc) · 3.04 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
package com.katalon.jenkins.plugin.helper;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.katalon.jenkins.plugin.entity.Plan;
import com.katalon.jenkins.plugin.entity.Project;
import com.katalon.jenkins.plugin.search.SearchCondition;
import com.katalon.jenkins.plugin.search.SearchPagination;
import com.katalon.jenkins.plugin.search.SearchParameter;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.utils.URIBuilder;
import org.apache.commons.io.IOUtils;
import java.io.IOException;
import java.io.InputStream;
import java.net.URISyntaxException;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
import java.util.Map;
public class KatalonTestOpsSearchHelper {
private static final String SEARCH_URL = "/api/v1/search";
private ObjectMapper objectMapper;
public KatalonTestOpsSearchHelper() {
objectMapper = new ObjectMapper();
objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
}
private List<Object> search(String token, String serverUrl, SearchParameter searchParameter) {
String url = serverUrl + SEARCH_URL;
try {
URIBuilder uriBuilder = new URIBuilder(url);
HttpPost httpPost = new HttpPost(uriBuilder.build());
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, "application/json");
String requestContent = objectMapper.writeValueAsString(searchParameter);
HttpResponse httpResponse = HttpHelper.sendRequest(
httpPost,
token,
null,
null,
IOUtils.toInputStream(requestContent),
null,
null);
InputStream responseContent = httpResponse.getEntity().getContent();
Map map = objectMapper.readValue(responseContent, Map.class);
List<Object> content = (List) map.get("content");
return content;
} catch (URISyntaxException | IOException e) {
return null;
}
}
public Project[] getProjects(String token, String serverUrl) {
SearchParameter searchParameter = new SearchParameter();
searchParameter.setType("Project");
searchParameter.setConditions(Collections.emptyList());
SearchPagination pagination = new SearchPagination(0L, 30L, null);
searchParameter.setPagination(pagination);
List<Object> content = search(token, serverUrl, searchParameter);
return objectMapper.convertValue(content, Project[].class);
}
public Plan[] getPlan(String token, String serverUrl, String projectId) {
SearchParameter searchParameter = new SearchParameter();
searchParameter.setType("RunConfiguration");
searchParameter.setConditions(Arrays.asList(
new SearchCondition("Project.id", "=", projectId)
));
SearchPagination pagination = new SearchPagination(0L, 30L, null);
searchParameter.setPagination(pagination);
List<Object> content = search(token, serverUrl, searchParameter);
return objectMapper.convertValue(content, Plan[].class);
}
}