From 5c518aeb9f3b11ef3399dc95bd5c44c3ecb1be13 Mon Sep 17 00:00:00 2001 From: David Griffin Date: Tue, 12 Nov 2024 13:43:43 -0800 Subject: [PATCH 1/3] Remove references to EventSourceResponse. --- .../java/com/fauna/client/FaunaClient.java | 15 ++++---- .../com/fauna/codec/DefaultCodecProvider.java | 6 ++-- .../com/fauna/codec/codecs/DynamicCodec.java | 4 +-- ...sponseCodec.java => EventSourceCodec.java} | 16 ++++----- .../java/com/fauna/event/EventSource.java | 36 ++++++++++++++++--- .../java/com/fauna/event/FeedRequest.java | 8 ++--- .../java/com/fauna/beans/InventorySource.java | 4 +-- .../fauna/codec/codecs/DynamicCodecTest.java | 4 +-- src/test/java/com/fauna/e2e/E2EFeedsTest.java | 16 ++++----- .../java/com/fauna/e2e/E2EStreamingTest.java | 18 ++++------ 10 files changed, 71 insertions(+), 56 deletions(-) rename src/main/java/com/fauna/codec/codecs/{EventSourceResponseCodec.java => EventSourceCodec.java} (63%) diff --git a/src/main/java/com/fauna/client/FaunaClient.java b/src/main/java/com/fauna/client/FaunaClient.java index e29fb214..d1ef878d 100644 --- a/src/main/java/com/fauna/client/FaunaClient.java +++ b/src/main/java/com/fauna/client/FaunaClient.java @@ -6,7 +6,6 @@ import com.fauna.codec.DefaultCodecRegistry; import com.fauna.codec.ParameterizedOf; import com.fauna.event.EventSource; -import com.fauna.event.EventSourceResponse; import com.fauna.event.FaunaStream; import com.fauna.event.FeedIterator; import com.fauna.event.FeedOptions; @@ -612,11 +611,9 @@ public FaunaStream stream(final EventSource eventSource, */ public CompletableFuture> asyncStream(final Query fql, final Class elementClass) { - return this.asyncQuery(fql, EventSourceResponse.class) + return this.asyncQuery(fql, EventSource.class) .thenApply(queryResponse -> - this.stream(EventSource.fromResponse( - queryResponse.getData()), - StreamOptions.builder().build(), elementClass)); + this.stream(queryResponse.getData(), StreamOptions.builder().build(), elementClass)); } /** @@ -625,8 +622,8 @@ public CompletableFuture> asyncStream(final Query fql, * *

* Query = fql("Product.all().eventSource()"); - * QuerySuccess<EventSourceResponse> querySuccess = client.query(fql, EventSourceResponse.class); - * EventSource source = EventSource.fromResponse(querySuccess.getData()); + * QuerySuccess<EventSource> querySuccess = client.query(fql, EventSource.class); + * EventSource source = querySuccess.getData(); * FaunaStream<Product> faunaStream = client.stream(source, StreamOptions.DEFAULT, Product.class) * * @param fql The FQL query to be executed. It must return a stream, e.g. ends in `.toStream()`. @@ -678,9 +675,9 @@ public CompletableFuture> poll(final EventSource eventSource, public CompletableFuture> asyncFeed(final Query fql, final FeedOptions feedOptions, final Class elementClass) { - return this.asyncQuery(fql, EventSourceResponse.class).thenApply( + return this.asyncQuery(fql, EventSource.class).thenApply( success -> this.feed( - EventSource.fromResponse(success.getData()), + success.getData(), feedOptions, elementClass)); } diff --git a/src/main/java/com/fauna/codec/DefaultCodecProvider.java b/src/main/java/com/fauna/codec/DefaultCodecProvider.java index 8b9bf55f..0202ba43 100644 --- a/src/main/java/com/fauna/codec/DefaultCodecProvider.java +++ b/src/main/java/com/fauna/codec/DefaultCodecProvider.java @@ -4,7 +4,7 @@ import com.fauna.codec.codecs.ClassCodec; import com.fauna.codec.codecs.DynamicCodec; import com.fauna.codec.codecs.EnumCodec; -import com.fauna.codec.codecs.EventSourceResponseCodec; +import com.fauna.codec.codecs.EventSourceCodec; import com.fauna.codec.codecs.ListCodec; import com.fauna.codec.codecs.MapCodec; import com.fauna.codec.codecs.NullableDocumentCodec; @@ -15,7 +15,7 @@ import com.fauna.codec.codecs.QueryLiteralCodec; import com.fauna.codec.codecs.QueryObjCodec; import com.fauna.codec.codecs.QueryValCodec; -import com.fauna.event.EventSourceResponse; +import com.fauna.event.EventSource; import com.fauna.query.builder.Query; import com.fauna.query.builder.QueryArr; import com.fauna.query.builder.QueryLiteral; @@ -62,7 +62,7 @@ public DefaultCodecProvider(final CodecRegistry registry) { registry.put(CodecRegistryKey.from(QueryVal.class), new QueryValCodec(this)); registry.put(CodecRegistryKey.from(QueryLiteral.class), new QueryLiteralCodec()); - registry.put(CodecRegistryKey.from(EventSourceResponse.class), new EventSourceResponseCodec()); + registry.put(CodecRegistryKey.from(EventSource.class), new EventSourceCodec()); var bdc = new BaseDocumentCodec(this); registry.put(CodecRegistryKey.from(BaseDocument.class), bdc); diff --git a/src/main/java/com/fauna/codec/codecs/DynamicCodec.java b/src/main/java/com/fauna/codec/codecs/DynamicCodec.java index 4d6c65a0..90c6c3b2 100644 --- a/src/main/java/com/fauna/codec/codecs/DynamicCodec.java +++ b/src/main/java/com/fauna/codec/codecs/DynamicCodec.java @@ -5,7 +5,7 @@ import com.fauna.codec.FaunaType; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import com.fauna.event.EventSourceResponse; +import com.fauna.event.EventSource; import com.fauna.exception.CodecException; import com.fauna.types.Document; import com.fauna.types.DocumentRef; @@ -52,7 +52,7 @@ public Object decode(final UTF8FaunaParser parser) throws CodecException { case START_DOCUMENT: return provider.get(Document.class).decode(parser); case STREAM: - return provider.get(EventSourceResponse.class).decode(parser); + return provider.get(EventSource.class).decode(parser); case MODULE: return parser.getValueAsModule(); case INT: diff --git a/src/main/java/com/fauna/codec/codecs/EventSourceResponseCodec.java b/src/main/java/com/fauna/codec/codecs/EventSourceCodec.java similarity index 63% rename from src/main/java/com/fauna/codec/codecs/EventSourceResponseCodec.java rename to src/main/java/com/fauna/codec/codecs/EventSourceCodec.java index be87f896..a5fd7d55 100644 --- a/src/main/java/com/fauna/codec/codecs/EventSourceResponseCodec.java +++ b/src/main/java/com/fauna/codec/codecs/EventSourceCodec.java @@ -4,19 +4,19 @@ import com.fauna.codec.FaunaType; import com.fauna.codec.UTF8FaunaGenerator; import com.fauna.codec.UTF8FaunaParser; -import com.fauna.event.EventSourceResponse; +import com.fauna.event.EventSource; import com.fauna.exception.CodecException; /** - * Codec for encoding and decoding {@link EventSourceResponse} instances in the Fauna tagged format. + * Codec for encoding and decoding {@link EventSource} instances in the Fauna tagged format. */ -public final class EventSourceResponseCodec extends BaseCodec { +public final class EventSourceCodec extends BaseCodec { @Override - public EventSourceResponse decode(final UTF8FaunaParser parser) + public EventSource decode(final UTF8FaunaParser parser) throws CodecException { if (parser.getCurrentTokenType() == FaunaTokenType.STREAM) { - return new EventSourceResponse(parser.getTaggedValueAsString()); + return new EventSource(parser.getTaggedValueAsString()); } else { throw new CodecException(this.unsupportedTypeDecodingMessage( parser.getCurrentTokenType().getFaunaType(), @@ -25,14 +25,14 @@ public EventSourceResponse decode(final UTF8FaunaParser parser) } @Override - public void encode(final UTF8FaunaGenerator gen, final EventSourceResponse obj) + public void encode(final UTF8FaunaGenerator gen, final EventSource obj) throws CodecException { - throw new CodecException("Cannot encode StreamTokenResponse"); + throw new CodecException("Cannot encode EventSource"); } @Override public Class getCodecClass() { - return EventSourceResponse.class; + return EventSource.class; } @Override diff --git a/src/main/java/com/fauna/event/EventSource.java b/src/main/java/com/fauna/event/EventSource.java index 448fbceb..0ec7e1af 100644 --- a/src/main/java/com/fauna/event/EventSource.java +++ b/src/main/java/com/fauna/event/EventSource.java @@ -2,6 +2,8 @@ import com.fauna.query.builder.Query; +import java.util.Objects; + /** * Represents an event source. You can consume event sources as Event Feeds by @@ -41,14 +43,38 @@ public String getToken() { public static EventSource fromToken(final String token) { return new EventSource(token); } + /** + * Compares this {@code EventSourceResponse} with another object for equality. + * + * @param o The object to compare with. + * @return {@code true} if the specified object is equal to this {@code EventSourceResponse}; {@code false} otherwise. + */ + @Override + public boolean equals(final Object o) { + if (this == o) { + return true; + } + + if (o == null) { + return false; + } + + if (getClass() != o.getClass()) { + return false; + } + + EventSource c = (EventSource) o; + + return Objects.equals(token, c.token); + } /** - * Creates an {@code EventSource} from an {@code EventSourceResponse}. + * Returns the hash code for this {@code EventSourceResponse}. * - * @param response An {@code EventSourceResponse} containing the token for the event source. - * @return A new {@code EventSource} instance. + * @return An {@code int} representing the hash code of this object. */ - public static EventSource fromResponse(final EventSourceResponse response) { - return new EventSource(response.getToken()); + @Override + public int hashCode() { + return Objects.hash(token); } } diff --git a/src/main/java/com/fauna/event/FeedRequest.java b/src/main/java/com/fauna/event/FeedRequest.java index c138fc44..44af9054 100644 --- a/src/main/java/com/fauna/event/FeedRequest.java +++ b/src/main/java/com/fauna/event/FeedRequest.java @@ -65,13 +65,13 @@ public String serialize() throws IOException { } /** - * Creates a new {@code FeedRequest} from an {@link EventSourceResponse}. + * Creates a new {@code FeedRequest} from an {@link EventSource}. * - * @param resp The {@link EventSourceResponse} containing the event source token. + * @param resp The {@link EventSource} containing the event source token. * @param options The {@link FeedOptions} specifying additional feed request options. * @return A new {@code FeedRequest} instance based on the response and options. */ - public static FeedRequest fromResponse(final EventSourceResponse resp, final FeedOptions options) { - return new FeedRequest(EventSource.fromToken(resp.getToken()), options); + public static FeedRequest fromResponse(final EventSource resp, final FeedOptions options) { + return new FeedRequest(resp, options); } } diff --git a/src/test/java/com/fauna/beans/InventorySource.java b/src/test/java/com/fauna/beans/InventorySource.java index e6114d8f..00f17296 100644 --- a/src/test/java/com/fauna/beans/InventorySource.java +++ b/src/test/java/com/fauna/beans/InventorySource.java @@ -1,10 +1,10 @@ package com.fauna.beans; import com.fauna.e2e.beans.Product; -import com.fauna.event.EventSourceResponse; +import com.fauna.event.EventSource; import com.fauna.types.Page; public class InventorySource { public Page firstPage; - public EventSourceResponse eventSource; + public EventSource eventSource; } diff --git a/src/test/java/com/fauna/codec/codecs/DynamicCodecTest.java b/src/test/java/com/fauna/codec/codecs/DynamicCodecTest.java index 13b7d4c8..4ea7796c 100644 --- a/src/test/java/com/fauna/codec/codecs/DynamicCodecTest.java +++ b/src/test/java/com/fauna/codec/codecs/DynamicCodecTest.java @@ -4,7 +4,7 @@ import com.fauna.codec.Codec; import com.fauna.codec.DefaultCodecProvider; import com.fauna.codec.FaunaType; -import com.fauna.event.EventSourceResponse; +import com.fauna.event.EventSource; import com.fauna.exception.NullDocumentException; import com.fauna.types.Document; import com.fauna.types.DocumentRef; @@ -84,7 +84,7 @@ public static Stream testCases() { null, NULL_DOC_EXCEPTION), Arguments.of(TestType.Decode, DYNAMIC_CODEC, "{\"@stream\":\"token\"}", - new EventSourceResponse("token"), null), + new EventSource("token"), null), Arguments.of(TestType.Decode, DYNAMIC_CODEC, "{\"@bytes\": \"RmF1bmE=\"}", new byte[] {70, 97, 117, 110, 97}, null) diff --git a/src/test/java/com/fauna/e2e/E2EFeedsTest.java b/src/test/java/com/fauna/e2e/E2EFeedsTest.java index 7daaafab..f30c9704 100644 --- a/src/test/java/com/fauna/e2e/E2EFeedsTest.java +++ b/src/test/java/com/fauna/e2e/E2EFeedsTest.java @@ -5,7 +5,6 @@ import com.fauna.client.FaunaConfig; import com.fauna.e2e.beans.Product; import com.fauna.event.EventSource; -import com.fauna.event.EventSourceResponse; import com.fauna.event.FaunaEvent; import com.fauna.event.FeedIterator; import com.fauna.event.FeedOptions; @@ -69,10 +68,10 @@ public void feedOfAll() { @Test public void manualFeed() { // Use the feeds API with complete (i.e. manual) control of the calls made to Fauna. - QuerySuccess sourceQuery = + QuerySuccess sourceQuery = client.query(fql("Product.all().eventSource()"), - EventSourceResponse.class); - EventSource source = EventSource.fromResponse(sourceQuery.getData()); + EventSource.class); + EventSource source = sourceQuery.getData(); List> productUpdates = new ArrayList<>(); FeedOptions initialOptions = FeedOptions.builder().startTs(productCollectionTs).pageSize(2) @@ -140,12 +139,9 @@ public void feedError() { public void feedEventError() { // Fauna can also return a valid feed page, with HTTP 200, but an "error" event type. FeedOptions options = FeedOptions.builder().startTs(0L).build(); - QuerySuccess sourceQuery = - client.query(fql("Product.all().eventSource()"), - EventSourceResponse.class); - FeedIterator iter = - client.feed(EventSource.fromResponse(sourceQuery.getData()), - options, Product.class); + QuerySuccess sourceQuery = + client.query(fql("Product.all().eventSource()"), EventSource.class); + FeedIterator iter = client.feed(sourceQuery.getData(), options, Product.class); FeedPage pageOne = iter.next(); assertFalse(pageOne.hasNext()); assertEquals(1, pageOne.getEvents().size()); diff --git a/src/test/java/com/fauna/e2e/E2EStreamingTest.java b/src/test/java/com/fauna/e2e/E2EStreamingTest.java index 69e7f4b5..47f0018d 100644 --- a/src/test/java/com/fauna/e2e/E2EStreamingTest.java +++ b/src/test/java/com/fauna/e2e/E2EStreamingTest.java @@ -7,7 +7,6 @@ import com.fauna.client.QueryStatsSummary; import com.fauna.e2e.beans.Product; import com.fauna.event.EventSource; -import com.fauna.event.EventSourceResponse; import com.fauna.event.FaunaEvent; import com.fauna.event.FaunaStream; import com.fauna.event.StreamOptions; @@ -206,8 +205,7 @@ public void query_trackStateWithStream() throws InterruptedException { }); // Now start a stream based on same query, and it's transaction timestamp. - EventSource eventSource = - EventSource.fromResponse(inventorySource.eventSource); + EventSource eventSource = inventorySource.eventSource; StreamOptions streamOptions = StreamOptions.builder().startTimestamp(success.getLastSeenTxn()) .build(); @@ -253,10 +251,10 @@ public void query_trackStateWithStream() throws InterruptedException { public void handleStreamError() throws InterruptedException { // It would be nice to have another test that generates a stream with normal events, and then an error // event, but this at least tests some of the functionality. - QuerySuccess queryResp = + QuerySuccess queryResp = client.query(fql("Product.all().eventSource()"), - EventSourceResponse.class); - EventSource source = EventSource.fromResponse(queryResp.getData()); + EventSource.class); + EventSource source = queryResp.getData(); StreamOptions options = StreamOptions.builder().cursor("invalid_cursor").build(); FaunaStream stream = @@ -274,14 +272,12 @@ public void handleStreamError() throws InterruptedException { @Test public void handleStreamTimeout() { - QuerySuccess queryResp = - client.query(fql("Product.all().eventSource()"), - EventSourceResponse.class); - EventSource source = EventSource.fromResponse(queryResp.getData()); + QuerySuccess queryResp = client.query( + fql("Product.all().eventSource()"), EventSource.class); StreamOptions options = StreamOptions.builder().timeout(Duration.ofMillis(1)).build(); ClientException exc = assertThrows(ClientException.class, - () -> client.stream(source, options, Product.class)); + () -> client.stream(queryResp.getData(), options, Product.class)); assertEquals(ExecutionException.class, exc.getCause().getClass()); assertEquals(HttpTimeoutException.class, exc.getCause().getCause().getClass()); From 26336e1898af68a81fd645cb1dbdecb62c660684 Mon Sep 17 00:00:00 2001 From: David Griffin Date: Tue, 12 Nov 2024 14:08:55 -0800 Subject: [PATCH 2/3] Delete EventSourceResponse.java. --- .../com/fauna/event/EventSourceResponse.java | 72 ------------------- .../java/com/fauna/event/package-info.java | 1 - 2 files changed, 73 deletions(-) delete mode 100644 src/main/java/com/fauna/event/EventSourceResponse.java diff --git a/src/main/java/com/fauna/event/EventSourceResponse.java b/src/main/java/com/fauna/event/EventSourceResponse.java deleted file mode 100644 index 5dc4ce45..00000000 --- a/src/main/java/com/fauna/event/EventSourceResponse.java +++ /dev/null @@ -1,72 +0,0 @@ -package com.fauna.event; - -import java.util.Objects; - -/** - * Represents a response containing details about an event source. - *

- * The {@code EventSourceResponse} class provides access to the event source token - * and includes methods for equality checks and hash code generation. - */ -public class EventSourceResponse { - private String token; - - /** - * Constructs a new {@code EventSourceResponse} with the specified token. - * - * @param token A {@code String} representing the token of the event source. - */ - public EventSourceResponse(final String token) { - this.token = token; - } - - /** - * Constructs an empty {@code EventSourceResponse}. - */ - public EventSourceResponse() { - } - - /** - * Retrieves the token associated with this event source response. - * - * @return A {@code String} representing the token. - */ - public String getToken() { - return this.token; - } - - /** - * Compares this {@code EventSourceResponse} with another object for equality. - * - * @param o The object to compare with. - * @return {@code true} if the specified object is equal to this {@code EventSourceResponse}; {@code false} otherwise. - */ - @Override - public boolean equals(final Object o) { - if (this == o) { - return true; - } - - if (o == null) { - return false; - } - - if (getClass() != o.getClass()) { - return false; - } - - EventSourceResponse c = (EventSourceResponse) o; - - return Objects.equals(token, c.token); - } - - /** - * Returns the hash code for this {@code EventSourceResponse}. - * - * @return An {@code int} representing the hash code of this object. - */ - @Override - public int hashCode() { - return Objects.hash(token); - } -} diff --git a/src/main/java/com/fauna/event/package-info.java b/src/main/java/com/fauna/event/package-info.java index 7ec5e69e..425825dc 100644 --- a/src/main/java/com/fauna/event/package-info.java +++ b/src/main/java/com/fauna/event/package-info.java @@ -6,7 +6,6 @@ * *

    *
  • {@link com.fauna.event.EventSource} - Represents an event source.
  • - *
  • {@link com.fauna.event.EventSourceResponse} - Encapsulates the response containing an event source token.
  • *
  • {@link com.fauna.event.FaunaEvent} - Defines an event.
  • *
  • {@link com.fauna.event.FaunaStream} - Processes events from an Event Stream, decoding them into {@code FaunaEvent} instances.
  • *
  • {@link com.fauna.event.FeedIterator} - Enables iteration through pages of events in an Event Feed.
  • From 1555cc32e9431f35d0d327a73b2f4879ea6338f5 Mon Sep 17 00:00:00 2001 From: Lucas Pedroza <40873230+pnwpedro@users.noreply.github.com> Date: Wed, 13 Nov 2024 12:05:04 +0100 Subject: [PATCH 3/3] Apply suggestions from code review --- src/main/java/com/fauna/event/EventSource.java | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main/java/com/fauna/event/EventSource.java b/src/main/java/com/fauna/event/EventSource.java index 0ec7e1af..27e3b1c3 100644 --- a/src/main/java/com/fauna/event/EventSource.java +++ b/src/main/java/com/fauna/event/EventSource.java @@ -44,10 +44,10 @@ public static EventSource fromToken(final String token) { return new EventSource(token); } /** - * Compares this {@code EventSourceResponse} with another object for equality. + * Compares this {@code EventSource} with another object for equality. * * @param o The object to compare with. - * @return {@code true} if the specified object is equal to this {@code EventSourceResponse}; {@code false} otherwise. + * @return {@code true} if the specified object is equal to this {@code EventSource}; {@code false} otherwise. */ @Override public boolean equals(final Object o) { @@ -69,7 +69,7 @@ public boolean equals(final Object o) { } /** - * Returns the hash code for this {@code EventSourceResponse}. + * Returns the hash code for this {@code EventSource}. * * @return An {@code int} representing the hash code of this object. */