Migrate to Mockito #15
@ -2,16 +2,18 @@ package net.tomatentum.marinara.test.javacord;
|
|||||||
|
|
||||||
import static org.junit.jupiter.api.Assertions.assertFalse;
|
import static org.junit.jupiter.api.Assertions.assertFalse;
|
||||||
import static org.junit.jupiter.api.Assertions.assertTrue;
|
import static org.junit.jupiter.api.Assertions.assertTrue;
|
||||||
|
import static org.mockito.ArgumentMatchers.any;
|
||||||
|
import static org.mockito.Mockito.mock;
|
||||||
|
import static org.mockito.Mockito.when;
|
||||||
|
|
||||||
import org.javacord.api.entity.permission.PermissionType;
|
import org.javacord.api.entity.server.Server;
|
||||||
|
import org.javacord.api.interaction.ButtonInteraction;
|
||||||
import org.junit.jupiter.api.Test;
|
import org.junit.jupiter.api.Test;
|
||||||
import org.junit.jupiter.api.TestInstance;
|
import org.junit.jupiter.api.TestInstance;
|
||||||
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
import org.junit.jupiter.api.TestInstance.Lifecycle;
|
||||||
|
|
||||||
import net.tomatentum.marinara.Marinara;
|
import net.tomatentum.marinara.Marinara;
|
||||||
import net.tomatentum.marinara.test.javacord.mocks.ButtonInteractionMock;
|
import net.tomatentum.marinara.test.javacord.mocks.CommonMocks;
|
||||||
import net.tomatentum.marinara.test.javacord.mocks.DiscordApiMock;
|
|
||||||
import net.tomatentum.marinara.test.javacord.mocks.ServerMock;
|
|
||||||
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
import net.tomatentum.marinara.wrapper.LibraryWrapper;
|
||||||
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
import net.tomatentum.marinara.wrapper.javacord.JavacordWrapper;
|
||||||
import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck;
|
import net.tomatentum.marinara.wrapper.javacord.checks.PermissionCheck;
|
||||||
@ -21,27 +23,31 @@ public class InteractionCheckTest {
|
|||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testInteractionCheck() {
|
public void testInteractionCheck() {
|
||||||
LibraryWrapper wrapper = new JavacordWrapper(new DiscordApiMock());
|
LibraryWrapper wrapper = new JavacordWrapper(null);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getCheckRegistry().addCheck(new TestInteractionCheck());
|
marinara.getCheckRegistry().addCheck(new TestInteractionCheck());
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getRegistry().addInteractions(new TestButton());
|
||||||
wrapper.handleInteraction(new ButtonInteractionMock("test"));
|
wrapper.handleInteraction(CommonMocks.getButtonInteractionMock("test"));
|
||||||
assertTrue(TestInteractionCheck.preExecuted);
|
assertTrue(TestInteractionCheck.preExecuted);
|
||||||
assertTrue(TestInteractionCheck.postExecuted);
|
assertTrue(TestInteractionCheck.postExecuted);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void testPermissionCheck() {
|
public void testPermissionCheck() {
|
||||||
LibraryWrapper wrapper = new JavacordWrapper(new DiscordApiMock());
|
LibraryWrapper wrapper = new JavacordWrapper(null);
|
||||||
Marinara marinara = Marinara.load(wrapper);
|
Marinara marinara = Marinara.load(wrapper);
|
||||||
marinara.getCheckRegistry().addCheck(new PermissionCheck());
|
marinara.getCheckRegistry().addCheck(new PermissionCheck());
|
||||||
marinara.getRegistry().addInteractions(new TestButton());
|
marinara.getRegistry().addInteractions(new TestButton());
|
||||||
wrapper.handleInteraction(new ButtonInteractionMock("permissionCheck"));
|
|
||||||
assertTrue(TestButton.didPermRun);
|
Server serverMock = mock();
|
||||||
TestButton.didPermRun = false;
|
ButtonInteraction buttonInteractionMock = CommonMocks.getButtonInteractionMock("permissionCheck", serverMock);
|
||||||
ServerMock.TESTPERMISSION = PermissionType.ATTACH_FILE;
|
when(serverMock.hasPermissions(any(), any())).thenReturn(false);
|
||||||
wrapper.handleInteraction(new ButtonInteractionMock("permissionCheck"));
|
wrapper.handleInteraction(buttonInteractionMock);
|
||||||
assertFalse(TestButton.didPermRun);
|
assertFalse(TestButton.didPermRun);
|
||||||
|
|
||||||
|
when(serverMock.hasPermissions(any(), any())).thenReturn(true);
|
||||||
|
wrapper.handleInteraction(buttonInteractionMock);
|
||||||
|
assertTrue(TestButton.didPermRun);
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
@ -23,6 +23,19 @@ public class CommonMocks {
|
|||||||
when(buttonInteractionMock.getUser()).thenReturn(mock(User.class));
|
when(buttonInteractionMock.getUser()).thenReturn(mock(User.class));
|
||||||
|
|
||||||
|
|
||||||
|
return buttonInteractionMock;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static ButtonInteraction getButtonInteractionMock(String customId, Server serverMock) {
|
||||||
|
ButtonInteraction buttonInteractionMock = mock();
|
||||||
|
|
||||||
|
when(buttonInteractionMock.getCustomId()).thenReturn(customId);
|
||||||
|
when(buttonInteractionMock.getMessage()).thenReturn(mock(Message.class));
|
||||||
|
when(buttonInteractionMock.getServer()).thenReturn(Optional.of(serverMock));
|
||||||
|
when(buttonInteractionMock.getChannel()).thenReturn(Optional.of(mock(TextChannel.class)));
|
||||||
|
when(buttonInteractionMock.getUser()).thenReturn(mock(User.class));
|
||||||
|
|
||||||
|
|
||||||
return buttonInteractionMock;
|
return buttonInteractionMock;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user