Skip to content

Commit

Permalink
Dev UI Test Base for Build Time data and core tests
Browse files Browse the repository at this point in the history
Signed-off-by: Phillip Kruger <[email protected]>
  • Loading branch information
phillip-kruger committed Jun 19, 2023
1 parent 7b2179c commit 1018d93
Show file tree
Hide file tree
Showing 7 changed files with 130 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,15 @@ public abstract class DevUIHibernateOrmTest extends DevUIJsonRPCTest {
private int expectedNumberOfPersistenceUnits = 1;

public DevUIHibernateOrmTest(String expectedPersistenceUnitName, String expectedTableName, String expectedClassName) {
super("io.quarkus.quarkus-hibernate-orm");
this.expectedPersistenceUnitName = expectedPersistenceUnitName;
this.expectedTableName = expectedTableName;
this.expectedClassName = expectedClassName;
}

public DevUIHibernateOrmTest(int expectedNumberOfEntityTypes,
int expectedNumberOfPersistenceUnits) {
super("io.quarkus.quarkus-hibernate-orm");
this.expectedNumberOfEntityTypes = expectedNumberOfEntityTypes;
this.expectedNumberOfPersistenceUnits = expectedNumberOfPersistenceUnits;
}
Expand Down Expand Up @@ -100,10 +102,4 @@ public void testGetNumberOfNamedQueries() throws Exception {
Assertions.assertTrue(getNumberOfNamedQueriesResponse.isInt());
Assertions.assertEquals(0, getNumberOfNamedQueriesResponse.asInt());
}

@Override
protected String getNamespace() {
return "io.quarkus.quarkus-hibernate-orm";
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ public class BuildMetricsTest extends DevUIJsonRPCTest {
@RegisterExtension
static final QuarkusDevModeTest config = new QuarkusDevModeTest().withEmptyApplication();

public BuildMetricsTest() {
super("devui-build-metrics");
}

@Test
public void testGetBuildStepsMetrics() throws Exception {
JsonNode buildStepsMetricsResponse = super.executeJsonRPCMethod("getBuildStepsMetrics");
Expand All @@ -26,9 +30,4 @@ public void testGetBuildStepsMetrics() throws Exception {
Assertions.assertTrue(dependencyGraphsIncluded);

}

@Override
protected String getNamespace() {
return "devui-build-metrics";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,10 @@ public class ConfigurationTest extends DevUIJsonRPCTest {
static final QuarkusDevModeTest config = new QuarkusDevModeTest()
.withEmptyApplication();

public ConfigurationTest() {
super("devui-configuration");
}

@Test
public void testLogstreamHistory() throws Exception {

Expand All @@ -31,9 +35,4 @@ public void testLogstreamHistory() throws Exception {
String applicationName = allPropertiesResponse.get("quarkus.application.name").asText();
Assertions.assertEquals("changedByTest", applicationName);
}

@Override
protected String getNamespace() {
return "devui-configuration";
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
package io.quarkus.devui;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.RegisterExtension;

import com.fasterxml.jackson.databind.JsonNode;

import io.quarkus.devui.tests.DevUIBuildTimeDataTest;
import io.quarkus.test.QuarkusDevModeTest;

public class ExtensionsTest extends DevUIBuildTimeDataTest {

@RegisterExtension
static final QuarkusDevModeTest config = new QuarkusDevModeTest().withEmptyApplication();

public ExtensionsTest() {
super("devui");
}

@Test
public void testGetExtensions() throws Exception {
JsonNode extensionsResponse = super.getBuildTimeData("extensions");
Assertions.assertNotNull(extensionsResponse);

JsonNode activeExtensions = extensionsResponse.get("active");
Assertions.assertNotNull(activeExtensions);
Assertions.assertTrue(activeExtensions.isArray());

JsonNode inactiveExtensions = extensionsResponse.get("inactive");
Assertions.assertNotNull(inactiveExtensions);
Assertions.assertTrue(inactiveExtensions.isArray());

log.info(extensionsResponse.toPrettyString());

}

}
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,10 @@ public class LogstreamTest extends DevUIJsonRPCTest {
static final QuarkusDevModeTest config = new QuarkusDevModeTest()
.withEmptyApplication();

public LogstreamTest() {
super("devui-logstream");
}

@Test
public void testHistory() throws Exception {
JsonNode historyResponse = super.executeJsonRPCMethod("history");
Expand Down Expand Up @@ -48,11 +52,6 @@ public void testUpdateLoggers() throws Exception {
Assertions.assertEquals("DEBUG", updateLogLevelResponse.get("effectiveLevel").asText());
}

@Override
protected String getNamespace() {
return "devui-logstream";
}

private boolean hasStartedLine(Iterator<JsonNode> elements) {
while (elements.hasNext()) {
JsonNode next = elements.next();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package io.quarkus.devui.tests;

import java.io.IOException;
import java.net.MalformedURLException;
import java.net.URI;
import java.nio.charset.StandardCharsets;
import java.util.Scanner;

import org.eclipse.microprofile.config.ConfigProvider;
import org.jboss.logging.Logger;

import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;

public abstract class DevUIBuildTimeDataTest {

protected static final Logger log = Logger.getLogger(DevUIBuildTimeDataTest.class);

protected URI uri;

private final ObjectMapper mapper = new ObjectMapper();
private final JsonFactory factory = mapper.getFactory();

public DevUIBuildTimeDataTest(String namespace) {
String testUrl = ConfigProvider.getConfig().getValue("test.url", String.class);
String nonApplicationRoot = ConfigProvider.getConfig()
.getOptionalValue("quarkus.http.non-application-root-path", String.class).orElse("q");
if (!nonApplicationRoot.startsWith("/")) {
nonApplicationRoot = "/" + nonApplicationRoot;
}
this.uri = URI.create(testUrl + nonApplicationRoot + "/dev-ui/" + namespace + "-data.js");
}

public JsonNode getBuildTimeData(String key) throws Exception {

String data = readDataFromUrl();
String[] kvs = data.split(CONST);

for (String kv : kvs) {
if (kv.startsWith(key + SPACE + EQUALS + SPACE + OPEN_CURLY_BRACKET)) {
String json = kv.substring(kv.indexOf(EQUALS) + 1).trim();
log.debug("json = " + json);
return toJsonNode(json);
}
}

return null;
}

protected JsonNode toJsonNode(String json) {
try {
JsonParser parser = factory.createParser(json);
return mapper.readTree(parser);
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}

private String readDataFromUrl() throws MalformedURLException, IOException {
try (Scanner scanner = new Scanner(uri.toURL().openStream(),
StandardCharsets.UTF_8.toString())) {
scanner.useDelimiter("\\A");
return scanner.hasNext() ? scanner.next() : null;
}
}

private static final String CONST = "export const ";
private static final String SPACE = " ";
private static final String EQUALS = "=";
private static final String OPEN_CURLY_BRACKET = "{";
}
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,10 @@ public abstract class DevUIJsonRPCTest {
private final JsonFactory factory = mapper.getFactory();
private final Random random = new Random();

protected abstract String getNamespace();
private final String namespace;

public DevUIJsonRPCTest() {
public DevUIJsonRPCTest(String namespace) {
this.namespace = namespace;
String testUrl = ConfigProvider.getConfig().getValue("test.url", String.class);
String nonApplicationRoot = ConfigProvider.getConfig()
.getOptionalValue("quarkus.http.non-application-root-path", String.class).orElse("q");
Expand Down Expand Up @@ -100,7 +101,7 @@ private String createJsonRPCRequest(int id, String methodName, Map<String, Strin

request.put("jsonrpc", "2.0");
request.put("id", id);
request.put("method", getNamespace() + "." + methodName);
request.put("method", this.namespace + "." + methodName);
ObjectNode jsonParams = mapper.createObjectNode();
if (params != null && !params.isEmpty()) {
for (Map.Entry<String, String> p : params.entrySet()) {
Expand All @@ -122,7 +123,7 @@ public void open(Session session) {
}

@OnMessage
void message(String msg) {
public void message(String msg) {
MESSAGES.add(msg);
}
}
Expand Down

0 comments on commit 1018d93

Please sign in to comment.