Skip to content

Commit

Permalink
Cleanup tests
Browse files Browse the repository at this point in the history
  • Loading branch information
royteeuwen committed Sep 20, 2024
1 parent 3f7446b commit 834e7bd
Showing 1 changed file with 9 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package be.orbinson.aem.groovy.console.it;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.JsonSyntaxException;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.client.entity.UrlEncodedFormEntity;
Expand All @@ -18,7 +19,6 @@
import java.nio.charset.StandardCharsets;
import java.time.Instant;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import static org.awaitility.Awaitility.await;
Expand All @@ -29,21 +29,20 @@ class GroovyConsoleServiceIT {
private static final int SLING_PORT = Integer.getInteger("HTTP_PORT", 8080);

@BeforeEach
void beforeEach() throws IOException {
void beforeEach() {
await().atMost(30, TimeUnit.SECONDS) // Set maximum wait time to 30 seconds
.pollInterval(5, TimeUnit.SECONDS)
.untilAsserted(() -> assertTrue(servicesAreAvailable()));
}

@Test
void testScriptReturnsResult() throws Exception {

String script = "print 'test'";
Map response = executeScript(script);
JsonObject response = executeScript(script);
System.out.println("Got response script at " + Instant.now());

assertNotNull(response, "Could not get response from API");
assertEquals("test", response.get("output"));
assertEquals("test", response.get("output").getAsString());
}

private static boolean servicesAreAvailable() throws IOException {
Expand All @@ -53,9 +52,9 @@ private static boolean servicesAreAvailable() throws IOException {
printHealth.addHeader("Authorization", "Basic " + Base64.encodeBase64String("admin:admin".getBytes(StandardCharsets.UTF_8)));
try (CloseableHttpResponse response = httpclient.execute(printHealth)) {
String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
Map jsonResponse = new Gson().fromJson(body, Map.class);
JsonObject jsonResponse = JsonParser.parseString(body).getAsJsonObject();
try {
boolean systemAlive = "OK".equals(jsonResponse.get("overallResult"));
boolean systemAlive = "OK".equals(jsonResponse.get("overallResult").getAsString());
if (!systemAlive) {
System.out.println("Not alive yet:");
System.out.println(body);
Expand All @@ -69,7 +68,7 @@ private static boolean servicesAreAvailable() throws IOException {
}


private static Map executeScript(String script) throws IOException {
private static JsonObject executeScript(String script) throws IOException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpPost executeScript = new HttpPost("http://localhost:" + SLING_PORT + "/bin/groovyconsole/post");
List<BasicNameValuePair> basicNameValuePairs = new java.util.ArrayList<>();
Expand All @@ -82,7 +81,7 @@ private static Map executeScript(String script) throws IOException {

String body = EntityUtils.toString(response.getEntity(), StandardCharsets.UTF_8);
try {
return new Gson().fromJson(body, Map.class);
return JsonParser.parseString(body).getAsJsonObject();
} catch (JsonSyntaxException e) {
System.out.println("Could not parse body from JSON: " + e.getMessage());
return null;
Expand Down

0 comments on commit 834e7bd

Please sign in to comment.