From e9b7e4a0eb227c203419d3f05a7040b8dc0d2e3a Mon Sep 17 00:00:00 2001 From: Simon Hofbauer <61789910+hofi1@users.noreply.github.com> Date: Thu, 26 Oct 2023 22:27:09 -0500 Subject: [PATCH] fix JSON flaky tests (#15261) Co-authored-by: simonh5 --- .../java/util/emitter/core/EmitterTest.java | 38 ++++++++++++------- .../core/ParametrizedUriEmitterTest.java | 4 +- 2 files changed, 27 insertions(+), 15 deletions(-) diff --git a/processing/src/test/java/org/apache/druid/java/util/emitter/core/EmitterTest.java b/processing/src/test/java/org/apache/druid/java/util/emitter/core/EmitterTest.java index 39b55596ca0..62244d3bd28 100644 --- a/processing/src/test/java/org/apache/druid/java/util/emitter/core/EmitterTest.java +++ b/processing/src/test/java/org/apache/druid/java/util/emitter/core/EmitterTest.java @@ -240,12 +240,14 @@ public class EmitterTest request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE) ); Assert.assertEquals( - StringUtils.format( + JSON_MAPPER.readTree(StringUtils.format( "[%s,%s]\n", JSON_MAPPER.writeValueAsString(events.get(0)), JSON_MAPPER.writeValueAsString(events.get(1)) - ), - StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString() + )), + JSON_MAPPER.readTree( + StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString() + ) ); return GoHandlers.immediateFuture(okResponse()); @@ -282,12 +284,14 @@ public class EmitterTest request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE) ); Assert.assertEquals( - StringUtils.format( + JSON_MAPPER.readTree(StringUtils.format( "[%s,%s]\n", JSON_MAPPER.writeValueAsString(events.get(0)), JSON_MAPPER.writeValueAsString(events.get(1)) - ), - StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString() + )), + JSON_MAPPER.readTree( + StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString() + ) ); return GoHandlers.immediateFuture(okResponse()); @@ -467,12 +471,14 @@ public class EmitterTest request.getHeaders().get(HttpHeaders.Names.AUTHORIZATION) ); Assert.assertEquals( - StringUtils.format( + JSON_MAPPER.readTree(StringUtils.format( "%s\n%s\n", JSON_MAPPER.writeValueAsString(events.get(0)), JSON_MAPPER.writeValueAsString(events.get(1)) - ), - StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString() + )), + JSON_MAPPER.readTree( + StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString() + ) ); return GoHandlers.immediateFuture(okResponse()); @@ -521,12 +527,14 @@ public class EmitterTest request.getHeaders().get(HttpHeaders.Names.CONTENT_TYPE) ); Assert.assertEquals( - StringUtils.format( + JSON_MAPPER.readTree(StringUtils.format( "[%s,%s]\n", JSON_MAPPER.writeValueAsString(events.get(counter.getAndIncrement())), JSON_MAPPER.writeValueAsString(events.get(counter.getAndIncrement())) - ), - StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString() + )), + JSON_MAPPER.readTree( + StandardCharsets.UTF_8.decode(request.getByteBufferData().slice()).toString() + ) ); return GoHandlers.immediateFuture(okResponse()); @@ -584,12 +592,14 @@ public class EmitterTest CompressionUtils.gunzip(new ByteArrayInputStream(dataArray), baos); Assert.assertEquals( - StringUtils.format( + JSON_MAPPER.readTree(StringUtils.format( "[%s,%s]\n", JSON_MAPPER.writeValueAsString(events.get(0)), JSON_MAPPER.writeValueAsString(events.get(1)) - ), + )), + JSON_MAPPER.readTree( baos.toString(StandardCharsets.UTF_8.name()) + ) ); return GoHandlers.immediateFuture(okResponse()); diff --git a/processing/src/test/java/org/apache/druid/java/util/emitter/core/ParametrizedUriEmitterTest.java b/processing/src/test/java/org/apache/druid/java/util/emitter/core/ParametrizedUriEmitterTest.java index f7d7a8337a2..c5dc26432fe 100644 --- a/processing/src/test/java/org/apache/druid/java/util/emitter/core/ParametrizedUriEmitterTest.java +++ b/processing/src/test/java/org/apache/druid/java/util/emitter/core/ParametrizedUriEmitterTest.java @@ -155,7 +155,9 @@ public class ParametrizedUriEmitterTest "http://example.com/test1", StringUtils.format("[%s]\n", JSON_MAPPER.writeValueAsString(events.get(0))), "http://example.com/test2", StringUtils.format("[%s]\n", JSON_MAPPER.writeValueAsString(events.get(1))) ); - Assert.assertEquals(expected, results); + for (Map.Entry entry : expected.entrySet()) { + Assert.assertEquals(JSON_MAPPER.readTree(expected.get(entry.getKey())), JSON_MAPPER.readTree(results.get(entry.getKey()))); + } } @Test