diff --git a/src/main/java/net/dv8tion/jda/internal/managers/GuildManagerImpl.java b/src/main/java/net/dv8tion/jda/internal/managers/GuildManagerImpl.java index b3c7751046..6604ba8c1d 100644 --- a/src/main/java/net/dv8tion/jda/internal/managers/GuildManagerImpl.java +++ b/src/main/java/net/dv8tion/jda/internal/managers/GuildManagerImpl.java @@ -362,6 +362,8 @@ protected RequestBody finalizeData() body.put("rules_channel_id", rulesChannel); if (shouldUpdate(COMMUNITY_UPDATES_CHANNEL)) body.put("public_updates_channel_id", communityUpdatesChannel); + if (shouldUpdate(SAFETY_ALERTS_CHANNEL)) + body.put("safety_alerts_channel_id", safetyAlertsChannel); if (shouldUpdate(VERIFICATION_LEVEL)) body.put("verification_level", verificationLevel); if (shouldUpdate(NOTIFICATION_LEVEL)) diff --git a/src/test/java/net/dv8tion/jda/test/IntegrationTest.java b/src/test/java/net/dv8tion/jda/test/IntegrationTest.java index ebb0952457..0a961ced66 100644 --- a/src/test/java/net/dv8tion/jda/test/IntegrationTest.java +++ b/src/test/java/net/dv8tion/jda/test/IntegrationTest.java @@ -87,6 +87,11 @@ protected RestActionAssertions assertThatRequestFrom(@Nonnull RestAction acti .withNormalizedBody(this::normalizeRequestBody); } + protected void assertThatNoRequestsWereSent() + { + verify(requester, never()).request(any()); + } + protected void whenSuccess(RestActionImpl action, DataArray array, Consumer assertion) { Response response = mock(); diff --git a/src/test/java/net/dv8tion/jda/test/managers/GuildManagerTest.java b/src/test/java/net/dv8tion/jda/test/managers/GuildManagerTest.java new file mode 100644 index 0000000000..778f6bab39 --- /dev/null +++ b/src/test/java/net/dv8tion/jda/test/managers/GuildManagerTest.java @@ -0,0 +1,147 @@ +/* + * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package net.dv8tion.jda.test.managers; + +import net.dv8tion.jda.api.Permission; +import net.dv8tion.jda.api.entities.Guild; +import net.dv8tion.jda.api.entities.Member; +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; +import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; +import net.dv8tion.jda.api.managers.GuildManager; +import net.dv8tion.jda.api.utils.data.DataObject; +import net.dv8tion.jda.internal.managers.GuildManagerImpl; +import net.dv8tion.jda.test.Constants; +import net.dv8tion.jda.test.IntegrationTest; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; +import org.mockito.Mock; + +import java.lang.reflect.Method; +import java.util.Arrays; +import java.util.HashSet; +import java.util.Set; +import java.util.concurrent.atomic.AtomicInteger; + +import static net.dv8tion.jda.api.requests.Method.PATCH; +import static org.assertj.core.api.Assertions.assertThat; +import static org.assertj.core.api.Assertions.assertThatNoException; +import static org.mockito.ArgumentMatchers.any; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.when; + +public class GuildManagerTest extends IntegrationTest +{ + @Mock + private Guild guild; + @Mock + private Member selfMember; + @Mock + private TextChannel textChannel; + @Mock + private VoiceChannel voiceChannel; + + @BeforeEach + void setupMocks() + { + when(guild.getJDA()).thenReturn(jda); + when(guild.getId()).thenReturn(Long.toUnsignedString(Constants.GUILD_ID)); + when(guild.getSelfMember()).thenReturn(selfMember); + when(selfMember.hasPermission(any(Permission[].class))).thenReturn(true); + when(textChannel.getGuild()).thenReturn(guild); + when(textChannel.getId()).thenReturn(Long.toUnsignedString(Constants.CHANNEL_ID)); + when(voiceChannel.getGuild()).thenReturn(guild); + when(voiceChannel.getId()).thenReturn(Long.toUnsignedString(Constants.CHANNEL_ID)); + } + + @Test + void callNoSetters() + { + GuildManagerImpl manager = new GuildManagerImpl(guild); + manager.queue(); + assertThatNoRequestsWereSent(); + } + + @Test + void callEverySetter() + { + Set features = new HashSet<>(Arrays.asList("BANNER", "VERIFIED", "INVITE_SPLASH")); + when(guild.getFeatures()).thenReturn(features); + + Set ignoredSetters = new HashSet<>(Arrays.asList( + "setFeatures", "setInvitesDisabled" + )); + + AtomicInteger calledSetters = new AtomicInteger(0); + GuildManagerImpl manager = new GuildManagerImpl(guild); + for (Method method : GuildManager.class.getDeclaredMethods()) + { + if (ignoredSetters.contains(method.getName())) + continue; + + if (method.getName().startsWith("set") && method.getParameterCount() == 1) + { + assertThatNoException().describedAs("call " + method.getName()).isThrownBy(() -> { + Object mocked = getParameterForSetter(method); + method.invoke(manager, mocked); + calledSetters.incrementAndGet(); + }); + } + } + + DataObject expectedBody = DataObject.empty() + .put("afk_channel_id", "125227483518861312") + .put("afk_timeout", 0) + .put("banner", null) + .put("default_message_notifications", 0) + .put("description", "test") + .put("explicit_content_filter", 0) + .put("icon", null) + .put("mfa_level", 0) + .put("name", "test") + .put("premium_progress_bar_enabled", true) + .put("public_updates_channel_id", "125227483518861312") + .put("safety_alerts_channel_id", "125227483518861312") + .put("rules_channel_id", "125227483518861312") + .put("splash", null) + .put("system_channel_id", "125227483518861312") + .put("verification_level", 0); + + assertThatRequestFrom(manager) + .hasMethod(PATCH) + .hasBodyEqualTo(expectedBody) + .whenQueueCalled(); + + // Every setter should result in a new key in the body, make sure you updated finalizeData + assertThat(expectedBody.keys()).hasSize(calledSetters.get()); + } + + private Object getParameterForSetter(Method setter) + { + Class paramType = setter.getParameters()[0].getType(); + if (paramType == String.class) + return "test"; + if (paramType == Boolean.TYPE) + return true; + if (paramType == Integer.TYPE) + return 42; + if (TextChannel.class.isAssignableFrom(paramType)) + return textChannel; + if (VoiceChannel.class.isAssignableFrom(paramType)) + return voiceChannel; + return mock(paramType); + } +}