From c3f21a9a36fd8fdc5ac75f8fbe2ba2c205a7026f Mon Sep 17 00:00:00 2001 From: Aditya Agarwal Date: Thu, 21 Nov 2024 14:26:53 +0100 Subject: [PATCH] chore: reactons batch change verb to GET, fix tests --- .../io/getstream/client/ReactionsClient.java | 3 +- .../io/getstream/core/StreamReactions.java | 16 +++++----- .../getstream/core/models/ReactionBatch.java | 13 ++++++--- .../getstream/client/ReactionsClientTest.java | 29 +++++++++++-------- 4 files changed, 36 insertions(+), 25 deletions(-) diff --git a/src/main/java/io/getstream/client/ReactionsClient.java b/src/main/java/io/getstream/client/ReactionsClient.java index 9a09c966..abdd5343 100644 --- a/src/main/java/io/getstream/client/ReactionsClient.java +++ b/src/main/java/io/getstream/client/ReactionsClient.java @@ -12,6 +12,7 @@ import io.getstream.core.models.FeedID; import io.getstream.core.models.Paginated; import io.getstream.core.models.Reaction; +import io.getstream.core.models.ReactionBatch; import io.getstream.core.options.Filter; import io.getstream.core.options.Limit; import io.getstream.core.utils.Auth.TokenAction; @@ -33,7 +34,7 @@ public CompletableFuture get(String id) throws StreamException { return reactions.get(token, id); } - public CompletableFuture> getBatch(List ids) throws StreamException { + public CompletableFuture getBatch(List ids) throws StreamException { final Token token = buildReactionsToken(secret, TokenAction.READ); return reactions.getBatchReactions(token, ids); } diff --git a/src/main/java/io/getstream/core/StreamReactions.java b/src/main/java/io/getstream/core/StreamReactions.java index 89e9799f..f8a08bc9 100644 --- a/src/main/java/io/getstream/core/StreamReactions.java +++ b/src/main/java/io/getstream/core/StreamReactions.java @@ -18,6 +18,7 @@ import io.getstream.core.models.FeedID; import io.getstream.core.models.Paginated; import io.getstream.core.models.Reaction; +import io.getstream.core.models.ReactionBatch; import io.getstream.core.options.CustomQueryParameter; import io.getstream.core.options.Filter; import io.getstream.core.options.Limit; @@ -343,28 +344,27 @@ public CompletableFuture restore(Token token, String id) throws StreamExce } } - - - public CompletableFuture> getBatchReactions(Token token, List ids) throws StreamException { + public CompletableFuture getBatchReactions(Token token, List ids) throws StreamException { checkNotNull(ids, "Reaction IDs can't be null"); checkArgument(!ids.isEmpty(), "Reaction IDs can't be empty"); try { final URL url = buildGetReactionsBatchURL(baseURL); - Map> payload = ImmutableMap.of("ids", ids); - final byte[] payloadBytes = toJSON(payload); + RequestOption optionIds = + new CustomQueryParameter( + "ids", String.join(",", ids)); return httpClient - .execute(buildPost(url, key, token, payloadBytes)) + .execute(buildGet(url, key, token, optionIds)) .thenApply( response -> { try { - return deserializeContainer(response, Reaction.class); + return deserialize(response, ReactionBatch.class); } catch (StreamException | IOException e) { throw new CompletionException(e); } }); - } catch (JsonProcessingException | MalformedURLException | URISyntaxException e) { + } catch (MalformedURLException | URISyntaxException e) { throw new StreamException(e); } } diff --git a/src/main/java/io/getstream/core/models/ReactionBatch.java b/src/main/java/io/getstream/core/models/ReactionBatch.java index a3583578..119dc1cf 100644 --- a/src/main/java/io/getstream/core/models/ReactionBatch.java +++ b/src/main/java/io/getstream/core/models/ReactionBatch.java @@ -3,24 +3,29 @@ import com.fasterxml.jackson.annotation.JsonInclude; import com.fasterxml.jackson.annotation.JsonProperty; +import java.util.List; + @JsonInclude(JsonInclude.Include.NON_NULL) public class ReactionBatch { @JsonProperty("reactions") - private Reaction[] reactions; + private List reactions; + + @JsonProperty("duration") + private String duration; public ReactionBatch() { } - public ReactionBatch(Reaction[] reactions) { + public ReactionBatch(List reactions) { this.reactions = reactions; } - public Reaction[] getReactions() { + public List getReactions() { return reactions; } - public void setReactions(Reaction[] reactions) { + public void setReactions(List reactions) { this.reactions = reactions; } } \ No newline at end of file diff --git a/src/test/java/io/getstream/client/ReactionsClientTest.java b/src/test/java/io/getstream/client/ReactionsClientTest.java index cba0d29c..8cc878b9 100644 --- a/src/test/java/io/getstream/client/ReactionsClientTest.java +++ b/src/test/java/io/getstream/client/ReactionsClientTest.java @@ -1,11 +1,12 @@ package io.getstream.client; import io.getstream.core.LookupKind; -import io.getstream.core.models.Activity; -import io.getstream.core.models.FeedID; -import io.getstream.core.models.Paginated; -import io.getstream.core.models.Reaction; +import io.getstream.core.models.*; + import java.util.List; +import java.util.Map; +import java.util.function.Function; +import java.util.stream.Collectors; import io.getstream.core.options.Filter; import io.getstream.core.options.Limit; @@ -102,15 +103,19 @@ public void batchFetchReactions() throws Exception { Reaction r5=client.reactions().add("user2", "comment", activity.getID()).join(); Reaction r6=client.reactions().add("user3", "comment", activity.getID()).join(); - List result = client.reactions().getBatch(List.of(r1.getId(), r2.getId(), r3.getId(), r4.getId(), r5.getId(), r6.getId())).join(); - assertEquals(6, result.size()); + Map reactionsRequest = Map.of(r1.getId(), r1, r2.getId(), r2, r3.getId(), r3, r4.getId(), r4, r5.getId(), r5, r6.getId(), r6); + + ReactionBatch response = client.reactions().getBatch(List.of(r1.getId(), r2.getId(), r3.getId(), r4.getId(), r5.getId(), r6.getId())).join(); + List result = response.getReactions(); - assertEquals("like", result.get(0).getKind()); - assertEquals("comment", result.get(1).getKind()); - assertEquals("share", result.get(2).getKind()); - assertEquals("like", result.get(3).getKind()); - assertEquals("comment", result.get(4).getKind()); - assertEquals("comment", result.get(5).getKind()); + //convert result to map and compare each id and type mapping from reactionsRequest to result + Map resultMap = result.stream().collect(Collectors.toMap(Reaction::getId, Function.identity())); + assertEquals(6, resultMap.size()); + for (Reaction r : result) { + Reaction req = reactionsRequest.get(r.getId()); + assertEquals(req.getActivityID(), r.getActivityID()); + assertEquals(req.getKind(), r.getKind()); + } } @Test