Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Uses new HTTP client in Java 11 for testing #37

Merged
merged 1 commit into from
Jan 8, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
125 changes: 40 additions & 85 deletions src/test/java/org/jenkinsci/plugins/pluginusage/JenkinsClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,9 @@
import java.net.URISyntaxException;
import java.net.URL;
import java.net.URLConnection;
import java.nio.charset.StandardCharsets;
import java.net.http.HttpClient;
import java.net.http.HttpRequest;
import java.net.http.HttpResponse;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
Expand Down Expand Up @@ -102,23 +104,15 @@ public List<String> getJobs() {
public List<String> getInstalledPlugins() {

try {
HttpURLConnection con = (HttpURLConnection) installedPluginsURL().openConnection();
con.setRequestMethod("GET");
final HttpRequest request = HttpRequest.newBuilder(installedPluginsURL().toURI())
.build();

int status = con.getResponseCode();
assertEquals(200, status);
final HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

StringBuilder content = new StringBuilder();
try(BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))){
String inputLine;
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
}
con.disconnect();
assertEquals(200, response.statusCode());

final JsonObject asJsonObject = JsonParser.parseString(content.toString()).getAsJsonObject();
final JsonObject asJsonObject = JsonParser.parseString(response.body()).getAsJsonObject();
final JsonArray jobs = asJsonObject.get("plugins").getAsJsonArray();
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(jobs.iterator(), Spliterator.ORDERED), false)
.map(element -> element.getAsJsonObject().get("shortName").getAsString())
Expand All @@ -131,23 +125,15 @@ public List<String> getInstalledPlugins() {
public List<String> getAvailablePlugins() {

try {
HttpURLConnection con = (HttpURLConnection) availablePluginsURL().openConnection();
con.setRequestMethod("GET");
final HttpRequest request = HttpRequest.newBuilder(availablePluginsURL().toURI())
.build();

int status = con.getResponseCode();
assertEquals(200, status);
final HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

StringBuilder content = new StringBuilder();
try(BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))){
String inputLine;
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
}
con.disconnect();
assertEquals(200, response.statusCode());

final JsonObject asJsonObject = JsonParser.parseString(content.toString()).getAsJsonObject();
final JsonObject asJsonObject = JsonParser.parseString(response.body()).getAsJsonObject();
final JsonArray jobs = asJsonObject.get("availables").getAsJsonArray();
return StreamSupport.stream(Spliterators.spliteratorUnknownSize(jobs.iterator(), Spliterator.ORDERED), false)
.map(element -> element.getAsJsonObject().get("name").getAsString())
Expand All @@ -159,26 +145,16 @@ public List<String> getAvailablePlugins() {

public PluginUsage getPluginUsage() {
try {
HttpURLConnection con = (HttpURLConnection) pluginUsageApiURL().openConnection();
con.setRequestMethod("GET");
final HttpRequest request = HttpRequest.newBuilder(pluginUsageApiURL().toURI())
.build();

int status = con.getResponseCode();
final HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());


StringBuilder content = new StringBuilder();
try(BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))){
String inputLine;
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
}
con.disconnect();

assertEquals(200, status);
assertEquals(200, response.statusCode());

Gson gson = new Gson();
return gson.fromJson(content.toString(), PluginUsage.class);
return gson.fromJson(response.body(), PluginUsage.class);
} catch (Exception e) {
throw new RuntimeException(e);
}
Expand All @@ -195,10 +171,6 @@ public Void createJob(String name, String jobResource) {

public Void postFile(URL url, String jobResource) {
try {
URLConnection con = url.openConnection();
HttpURLConnection http = (HttpURLConnection)con;
http.setRequestMethod("POST");
http.setDoOutput(true);

ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
try(final InputStream resourceAsStream = getClass().getClassLoader().getResourceAsStream(jobResource)){
Expand All @@ -210,51 +182,34 @@ public Void postFile(URL url, String jobResource) {
}
}

http.setFixedLengthStreamingMode(byteArrayOutputStream.size());
http.setRequestProperty("Content-Type", "application/xml; charset=UTF-8");
http.connect();
try(OutputStream os = http.getOutputStream()) {
os.write(byteArrayOutputStream.toByteArray());
}
int status = http.getResponseCode();
final HttpRequest request = HttpRequest.newBuilder(url.toURI())
.POST(HttpRequest.BodyPublishers.ofByteArray(byteArrayOutputStream.toByteArray()))
.header("Content-Type", "application/xml; charset=UTF-8")
.build();

final HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

StringBuilder content = new StringBuilder();
try(BufferedReader in = new BufferedReader(
new InputStreamReader(con.getInputStream()))){
String inputLine;
while ((inputLine = in.readLine()) != null) {
content.append(inputLine);
}
}
http.disconnect();
assertEquals(200, status);
} catch (IOException e) {
assertEquals(200, response.statusCode());

} catch (IOException | InterruptedException | URISyntaxException e) {
throw new RuntimeException(e);
}
return null;
}

public Void installPlugins(String plugin, String version) {
try {
URLConnection con = installNecessaryPluginsURL().openConnection();
HttpURLConnection http = (HttpURLConnection)con;
http.setRequestMethod("POST");
http.setDoOutput(true);

byte[] out = ("<jenkins><install plugin=\""+ plugin + "@" + version + "\" /></jenkins>")
.getBytes(StandardCharsets.UTF_8);
int length = out.length;

http.setFixedLengthStreamingMode(length);
http.setRequestProperty("Content-Type", "application/xml; charset=UTF-8");
http.connect();
try(OutputStream os = http.getOutputStream()) {
os.write(out);
}
int status = http.getResponseCode();
assertEquals(302, status);
} catch (IOException | URISyntaxException e) {
final HttpRequest request = HttpRequest.newBuilder(installNecessaryPluginsURL().toURI())
.POST(HttpRequest.BodyPublishers.ofString("<jenkins><install plugin=\""+ plugin + "@" + version + "\" /></jenkins>"))
.header("Content-Type", "application/xml; charset=UTF-8")
.build();

final HttpClient client = HttpClient.newHttpClient();
HttpResponse<String> response = client.send(request, HttpResponse.BodyHandlers.ofString());

assertEquals(302, response.statusCode());
} catch (IOException | URISyntaxException | InterruptedException e) {
throw new RuntimeException(e);
}
return null;
Expand Down