From 478d505e40517fcd7f011f28753197494f0224eb Mon Sep 17 00:00:00 2001 From: peterdeme Date: Thu, 17 Mar 2022 18:18:21 +0100 Subject: [PATCH] fix: additional fields mutable --- .github/CODEOWNERS | 2 +- .../getstream/chat/java/models/Channel.java | 19 +++++++++ .../io/getstream/chat/java/ChannelTest.java | 41 +++++++++++++++++++ 3 files changed, 61 insertions(+), 1 deletion(-) diff --git a/.github/CODEOWNERS b/.github/CODEOWNERS index ca8d20d3..1e8fdae0 100644 --- a/.github/CODEOWNERS +++ b/.github/CODEOWNERS @@ -1 +1 @@ -* @ferhatelmas @miagilepner @driver-devel +* @ferhatelmas @driver-devel @peterdeme diff --git a/src/main/java/io/getstream/chat/java/models/Channel.java b/src/main/java/io/getstream/chat/java/models/Channel.java index ddb8b6ed..5686fc7b 100644 --- a/src/main/java/io/getstream/chat/java/models/Channel.java +++ b/src/main/java/io/getstream/chat/java/models/Channel.java @@ -239,6 +239,25 @@ public void setAdditionalField(String name, Object value) { public static ChannelRequestObject buildFrom(@Nullable Channel channel) { return RequestObjectBuilder.build(ChannelRequestObject.class, channel); } + + private ChannelRequestObject( + @Nullable UserRequestObject createdBy, + @Nullable String team, + @Nullable Boolean autoTranslationEnabled, + @Nullable Language autoTranslationLanguage, + @Nullable Boolean frozen, + @Nullable List members, + @Nullable ConfigOverridesRequestObject configOverrides, + Map additionalFields) { + this.createdBy = createdBy; + this.team = team; + this.autoTranslationEnabled = autoTranslationEnabled; + this.autoTranslationLanguage = autoTranslationLanguage; + this.frozen = frozen; + this.members = members; + this.configOverrides = configOverrides; + this.additionalFields = new HashMap(additionalFields); + } } @Builder diff --git a/src/test/java/io/getstream/chat/java/ChannelTest.java b/src/test/java/io/getstream/chat/java/ChannelTest.java index 1df712d2..a78f1dc1 100644 --- a/src/test/java/io/getstream/chat/java/ChannelTest.java +++ b/src/test/java/io/getstream/chat/java/ChannelTest.java @@ -32,6 +32,47 @@ void whenRetrievingChannel_thenNoException() { .request()); } + @DisplayName("Can set custom field on channel") + @Test + void whenSettingCustomField_thenNoException() { + var channelReq = + ChannelRequestObject.builder() + .createdBy(testUserRequestObject) + .members(buildChannelMembersList()) + .build(); + channelReq.setAdditionalField("fieldkey", "fieldvalue"); + var channel = + Assertions.assertDoesNotThrow( + () -> + Channel.getOrCreate(testChannel.getType(), RandomStringUtils.randomAlphabetic(12)) + .data(channelReq) + .request()); + Assertions.assertEquals( + "fieldvalue", channel.getChannel().getAdditionalFields().get("fieldkey")); + } + + @DisplayName("Can set custom field on channel after fields have already been set in builder") + @Test + void whenSettingCustomFieldInBuilderAndAfer_thenNoException() { + var channelReq = + ChannelRequestObject.builder() + .createdBy(testUserRequestObject) + .members(buildChannelMembersList()) + .additionalField("fieldkey1", "fieldvalue1") + .build(); + channelReq.setAdditionalField("fieldkey2", "fieldvalue2"); + var channel = + Assertions.assertDoesNotThrow( + () -> + Channel.getOrCreate(testChannel.getType(), RandomStringUtils.randomAlphabetic(12)) + .data(channelReq) + .request()); + Assertions.assertEquals( + "fieldvalue1", channel.getChannel().getAdditionalFields().get("fieldkey1")); + Assertions.assertEquals( + "fieldvalue2", channel.getChannel().getAdditionalFields().get("fieldkey2")); + } + @DisplayName("Can add a moderator to a channel (update)") @Test void whenAddingModerator_thenHasModerator() {