Skip to content

Commit

Permalink
Merge pull request #35670 from phillip-kruger/dev-ui-jackson-ct-fix
Browse files Browse the repository at this point in the history
Fix DevUI fix serialization error on Throwable
  • Loading branch information
phillip-kruger authored Sep 2, 2023
2 parents 00ebf12 + 613e164 commit b82b03a
Show file tree
Hide file tree
Showing 6 changed files with 70 additions and 27 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package io.quarkus.devui.deployment.jsonrpc;

import static io.quarkus.vertx.runtime.jackson.JsonUtil.BASE64_DECODER;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.exc.InvalidFormatException;

public class ByteArrayInputStreamDeserializer extends JsonDeserializer<ByteArrayInputStream> {

@Override
public ByteArrayInputStream deserialize(JsonParser p, DeserializationContext ctxt)
throws IOException, JsonProcessingException {
String text = p.getText();
try {
byte[] decode = BASE64_DECODER.decode(text);
return new ByteArrayInputStream(decode);
} catch (IllegalArgumentException e) {
throw new InvalidFormatException(p, "Expected a base64 encoded byte array", text, ByteArrayInputStream.class);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package io.quarkus.devui.deployment.jsonrpc;

import static io.quarkus.vertx.runtime.jackson.JsonUtil.BASE64_ENCODER;

import java.io.ByteArrayInputStream;
import java.io.IOException;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;

import io.quarkus.deployment.util.IoUtil;

public class ByteArrayInputStreamSerializer extends JsonSerializer<ByteArrayInputStream> {

@Override
public void serialize(ByteArrayInputStream value, JsonGenerator jgen, SerializerProvider provider) throws IOException {
byte[] readBytes = IoUtil.readBytes(value);
jgen.writeString(BASE64_ENCODER.encodeToString(readBytes));
}
}
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package io.quarkus.devui.deployment.jsonrpc;

import java.io.ByteArrayInputStream;
import java.io.Closeable;
import java.io.IOException;
import java.time.Instant;
Expand Down Expand Up @@ -137,6 +138,8 @@ public JsonMapper create(JsonTypeAdapter<?, Map<String, Object>> jsonObjectAdapt
module.addDeserializer(Instant.class, new InstantDeserializer());
module.addSerializer(byte[].class, new ByteArraySerializer());
module.addDeserializer(byte[].class, new ByteArrayDeserializer());
module.addSerializer(ByteArrayInputStream.class, new ByteArrayInputStreamSerializer());
module.addDeserializer(ByteArrayInputStream.class, new ByteArrayInputStreamDeserializer());
mapper.registerModule(module);

SimpleModule runtimeModule = new SimpleModule("vertx-module-runtime");
Expand Down
Original file line number Diff line number Diff line change
@@ -1,13 +1,10 @@
package io.quarkus.devui.deployment.menu;

import java.io.IOException;
import java.io.StringWriter;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;

import com.fasterxml.jackson.databind.ObjectMapper;

import io.quarkus.arc.deployment.BeanContainerBuildItem;
import io.quarkus.deployment.IsDevelopment;
import io.quarkus.deployment.annotations.BuildStep;
Expand Down Expand Up @@ -233,7 +230,6 @@ private void registerGetStatusMethod(LaunchModeBuildItem launchModeBuildItem) {

private void registerGetResultsMethod(LaunchModeBuildItem launchModeBuildItem) {
DevConsoleManager.register(NAMESPACE + DASH + "getResults", ignored -> {
ObjectMapper objectMapper = new ObjectMapper(); // Remove in favior of build in.
try {
Optional<TestSupport> ts = TestSupport.instance();
if (testsDisabled(launchModeBuildItem, ts)) {
Expand All @@ -246,10 +242,7 @@ private void registerGetResultsMethod(LaunchModeBuildItem launchModeBuildItem) {
return null;
}

try (StringWriter sw = new StringWriter()) {
objectMapper.writeValue(sw, testRunResults);
return sw.toString();
}
return testRunResults;

} catch (Exception e) {
throw new RuntimeException(e);
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package io.quarkus.vertx.http.testrunner;

import java.util.List;
import java.util.Iterator;
import java.util.Map;
import java.util.function.Function;
import java.util.function.Supplier;
Expand Down Expand Up @@ -56,22 +56,21 @@ public void checkTestsAreRun() throws InterruptedException, Exception {
Assertions.assertEquals(1L, ts.getTotalTestsPassed());
Assertions.assertEquals(0L, ts.getTotalTestsSkipped());

JsonNode jsonRPCResultString = super.executeJsonRPCMethod("getResults");
Assertions.assertNotNull(jsonRPCResultString);
JsonNode jsonRPCResult = super.executeJsonRPCMethod("getResults");
Assertions.assertNotNull(jsonRPCResult);
Assertions.assertTrue(jsonRPCResult.has("results"));

@SuppressWarnings("unchecked")
Map<String, Map<String, Map>> jsonRPCResult = mapper.readValue(jsonRPCResultString.textValue(), Map.class);
JsonNode results = jsonRPCResult.get("results");
Assertions.assertNotNull(results);

Assertions.assertTrue(jsonRPCResult.containsKey("results"));
Iterator<Map.Entry<String, JsonNode>> fields = results.fields();

Map<String, Map> results = jsonRPCResult.get("results");
Assertions.assertNotNull(results);
for (Map.Entry<String, Map> result : results.entrySet()) {
@SuppressWarnings("unchecked")
Map<String, Object> testResult = result.getValue();
String className = (String) testResult.get("className");
List passing = (List) testResult.get("passing");
List failing = (List) testResult.get("failing");
while (fields.hasNext()) {
JsonNode testResult = fields.next().getValue();
String className = testResult.get("className").asText();

JsonNode passing = testResult.get("passing");
JsonNode failing = testResult.get("failing");

if (className.equals(SimpleET.class.getName())) {
Assertions.assertEquals(1, failing.size(), className);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,10 @@
public class ContinuousTestingJsonRPCService implements Consumer<ContinuousTestingSharedStateManager.State> {

private final BroadcastProcessor<String> stateBroadcaster = BroadcastProcessor.create();
private final BroadcastProcessor<String> resultBroadcaster = BroadcastProcessor.create();
private final BroadcastProcessor<Object> resultBroadcaster = BroadcastProcessor.create();

private String lastKnownState = "";
private String lastKnownResults = "";
private Object lastKnownResults = "";

@Override
public void accept(ContinuousTestingSharedStateManager.State state) {
Expand All @@ -46,7 +46,7 @@ public Multi<String> streamTestState() {
return stateBroadcaster;
}

public Multi<String> streamTestResults() {
public Multi<Object> streamTestResults() {
return resultBroadcaster;
}

Expand All @@ -56,7 +56,7 @@ public String lastKnownState() {
}

@NonBlocking
public String lastKnownResults() {
public Object lastKnownResults() {
return this.lastKnownResults;
}

Expand Down Expand Up @@ -89,7 +89,7 @@ public boolean toggleInstrumentation() {
return invokeAction("toggleInstrumentation");
}

public String getResults() {
public Object getResults() {
return invokeAction("getResults");
}

Expand Down

0 comments on commit b82b03a

Please sign in to comment.