|
| 1 | +/* |
| 2 | + * Copyright 2015 Austin Keener, Michael Ritter, Florian Spieß, and the JDA contributors |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | + |
| 17 | +package net.dv8tion.jda.test.managers; |
| 18 | + |
| 19 | +import net.dv8tion.jda.api.Permission; |
| 20 | +import net.dv8tion.jda.api.entities.Guild; |
| 21 | +import net.dv8tion.jda.api.entities.Member; |
| 22 | +import net.dv8tion.jda.api.entities.channel.concrete.TextChannel; |
| 23 | +import net.dv8tion.jda.api.entities.channel.concrete.VoiceChannel; |
| 24 | +import net.dv8tion.jda.api.managers.GuildManager; |
| 25 | +import net.dv8tion.jda.api.utils.data.DataObject; |
| 26 | +import net.dv8tion.jda.internal.managers.GuildManagerImpl; |
| 27 | +import net.dv8tion.jda.test.Constants; |
| 28 | +import net.dv8tion.jda.test.IntegrationTest; |
| 29 | +import org.junit.jupiter.api.BeforeEach; |
| 30 | +import org.junit.jupiter.api.Test; |
| 31 | +import org.mockito.Mock; |
| 32 | + |
| 33 | +import java.lang.reflect.Method; |
| 34 | +import java.util.Arrays; |
| 35 | +import java.util.HashSet; |
| 36 | +import java.util.Set; |
| 37 | +import java.util.concurrent.atomic.AtomicInteger; |
| 38 | + |
| 39 | +import static net.dv8tion.jda.api.requests.Method.PATCH; |
| 40 | +import static org.assertj.core.api.Assertions.assertThat; |
| 41 | +import static org.assertj.core.api.Assertions.assertThatNoException; |
| 42 | +import static org.mockito.ArgumentMatchers.any; |
| 43 | +import static org.mockito.Mockito.mock; |
| 44 | +import static org.mockito.Mockito.when; |
| 45 | + |
| 46 | +public class GuildManagerTest extends IntegrationTest |
| 47 | +{ |
| 48 | + @Mock |
| 49 | + private Guild guild; |
| 50 | + @Mock |
| 51 | + private Member selfMember; |
| 52 | + @Mock |
| 53 | + private TextChannel textChannel; |
| 54 | + @Mock |
| 55 | + private VoiceChannel voiceChannel; |
| 56 | + |
| 57 | + @BeforeEach |
| 58 | + void setupMocks() |
| 59 | + { |
| 60 | + when(guild.getJDA()).thenReturn(jda); |
| 61 | + when(guild.getId()).thenReturn(Long.toUnsignedString(Constants.GUILD_ID)); |
| 62 | + when(guild.getSelfMember()).thenReturn(selfMember); |
| 63 | + when(selfMember.hasPermission(any(Permission[].class))).thenReturn(true); |
| 64 | + when(textChannel.getGuild()).thenReturn(guild); |
| 65 | + when(textChannel.getId()).thenReturn(Long.toUnsignedString(Constants.CHANNEL_ID)); |
| 66 | + when(voiceChannel.getGuild()).thenReturn(guild); |
| 67 | + when(voiceChannel.getId()).thenReturn(Long.toUnsignedString(Constants.CHANNEL_ID)); |
| 68 | + } |
| 69 | + |
| 70 | + @Test |
| 71 | + void callNoSetters() |
| 72 | + { |
| 73 | + GuildManagerImpl manager = new GuildManagerImpl(guild); |
| 74 | + manager.queue(); |
| 75 | + assertThatNoRequestsWereSent(); |
| 76 | + } |
| 77 | + |
| 78 | + @Test |
| 79 | + void callEverySetter() |
| 80 | + { |
| 81 | + Set<String> features = new HashSet<>(Arrays.asList("BANNER", "VERIFIED", "INVITE_SPLASH")); |
| 82 | + when(guild.getFeatures()).thenReturn(features); |
| 83 | + |
| 84 | + Set<String> ignoredSetters = new HashSet<>(Arrays.asList( |
| 85 | + "setFeatures", "setInvitesDisabled" |
| 86 | + )); |
| 87 | + |
| 88 | + AtomicInteger calledSetters = new AtomicInteger(0); |
| 89 | + GuildManagerImpl manager = new GuildManagerImpl(guild); |
| 90 | + for (Method method : GuildManager.class.getDeclaredMethods()) |
| 91 | + { |
| 92 | + if (ignoredSetters.contains(method.getName())) |
| 93 | + continue; |
| 94 | + |
| 95 | + if (method.getName().startsWith("set") && method.getParameterCount() == 1) |
| 96 | + { |
| 97 | + assertThatNoException().describedAs("call " + method.getName()).isThrownBy(() -> { |
| 98 | + Object mocked = getParameterForSetter(method); |
| 99 | + method.invoke(manager, mocked); |
| 100 | + calledSetters.incrementAndGet(); |
| 101 | + }); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + DataObject expectedBody = DataObject.empty() |
| 106 | + .put("afk_channel_id", "125227483518861312") |
| 107 | + .put("afk_timeout", 0) |
| 108 | + .put("banner", null) |
| 109 | + .put("default_message_notifications", 0) |
| 110 | + .put("description", "test") |
| 111 | + .put("explicit_content_filter", 0) |
| 112 | + .put("icon", null) |
| 113 | + .put("mfa_level", 0) |
| 114 | + .put("name", "test") |
| 115 | + .put("premium_progress_bar_enabled", true) |
| 116 | + .put("public_updates_channel_id", "125227483518861312") |
| 117 | + .put("safety_alerts_channel_id", "125227483518861312") |
| 118 | + .put("rules_channel_id", "125227483518861312") |
| 119 | + .put("splash", null) |
| 120 | + .put("system_channel_id", "125227483518861312") |
| 121 | + .put("verification_level", 0); |
| 122 | + |
| 123 | + assertThatRequestFrom(manager) |
| 124 | + .hasMethod(PATCH) |
| 125 | + .hasBodyEqualTo(expectedBody) |
| 126 | + .whenQueueCalled(); |
| 127 | + |
| 128 | + // Every setter should result in a new key in the body, make sure you updated finalizeData |
| 129 | + assertThat(expectedBody.keys()).hasSize(calledSetters.get()); |
| 130 | + } |
| 131 | + |
| 132 | + private Object getParameterForSetter(Method setter) |
| 133 | + { |
| 134 | + Class<?> paramType = setter.getParameters()[0].getType(); |
| 135 | + if (paramType == String.class) |
| 136 | + return "test"; |
| 137 | + if (paramType == Boolean.TYPE) |
| 138 | + return true; |
| 139 | + if (paramType == Integer.TYPE) |
| 140 | + return 42; |
| 141 | + if (TextChannel.class.isAssignableFrom(paramType)) |
| 142 | + return textChannel; |
| 143 | + if (VoiceChannel.class.isAssignableFrom(paramType)) |
| 144 | + return voiceChannel; |
| 145 | + return mock(paramType); |
| 146 | + } |
| 147 | +} |
0 commit comments