Skip to content

Commit

Permalink
Add system alive check before running the tests
Browse files Browse the repository at this point in the history
  • Loading branch information
royteeuwen committed Sep 20, 2024
1 parent ba3eb2c commit b4ca813
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 21 deletions.
6 changes: 6 additions & 0 deletions it.tests/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,12 @@
<version>5.10.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.awaitility</groupId>
<artifactId>awaitility</artifactId>
<version>4.2.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,30 +11,32 @@
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;

import java.io.IOException;
import java.nio.charset.StandardCharsets;
import java.time.Duration;
import java.time.Instant;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.concurrent.atomic.AtomicBoolean;

import static org.awaitility.Awaitility.await;
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertNotNull;

public class GroovyConsoleServiceIT {
class GroovyConsoleServiceIT {

private static final int SLING_PORT = Integer.getInteger("HTTP_PORT", 8080);

@BeforeEach
void beforeEach() throws IOException {
await().atMost(Duration.ofMinutes(2)).untilTrue(servicesAreAvailable());
}

@Test
void testScriptReturnsResult() throws Exception {
printHealth();
System.out.println("Starting test at " + Instant.now());
Thread.sleep(3000); // TODO, fix to use system health
System.out.println("Executing script at " + Instant.now());
printHealth();


String script = "print 'test'";
Map response = executeScript(script);
Expand All @@ -44,17 +46,28 @@ void testScriptReturnsResult() throws Exception {
assertEquals("test", response.get("output"));
}

private void printHealth() throws IOException {
private static AtomicBoolean servicesAreAvailable() throws IOException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpGet printHealth = new HttpGet("http://localhost:" + SLING_PORT + "/system/health.json");
HttpGet printHealth = new HttpGet("http://localhost:" + SLING_PORT + "/system/health.json?tags=systemalive,bundles");
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);
System.out.println(body);
Map jsonResponse = new Gson().fromJson(body, Map.class);
try {
boolean systemAlive = "OK".equals(jsonResponse.get("overallResult"));
if (!systemAlive) {
System.out.println("Not alive yet:");
System.out.println(body);
}
return new AtomicBoolean(systemAlive);
} catch (JsonSyntaxException e) {
return new AtomicBoolean(false);
}
}
}
}


private static Map executeScript(String script) throws IOException {
try (CloseableHttpClient httpclient = HttpClients.createDefault()) {
HttpPost executeScript = new HttpPost("http://localhost:" + SLING_PORT + "/bin/groovyconsole/post");
Expand All @@ -63,24 +76,14 @@ private static Map executeScript(String script) throws IOException {
executeScript.setEntity(new UrlEncodedFormEntity(basicNameValuePairs, StandardCharsets.UTF_8));
executeScript.addHeader("Authorization", "Basic " + Base64.encodeBase64String("admin:admin".getBytes(StandardCharsets.UTF_8)));

System.out.println("Request headers");
Arrays.stream(executeScript.getAllHeaders()).forEach(header ->
System.out.println(header.getName() + ": " + header.getValue())
);
try (CloseableHttpResponse response = httpclient.execute(executeScript)) {
System.out.println("Response headers");

Arrays.stream(response.getAllHeaders()).forEach(header ->
System.out.println(header.getName() + ": " + header.getValue())
);
System.out.println(response.getStatusLine());

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

0 comments on commit b4ca813

Please sign in to comment.