Skip to content

Commit

Permalink
[AB-xxx] adding unit test for server controller
Browse files Browse the repository at this point in the history
refactoring parameter parsing tests to use assertj
  • Loading branch information
Sheldan committed Apr 7, 2024
1 parent 71c1445 commit 02b8ed2
Show file tree
Hide file tree
Showing 15 changed files with 195 additions and 116 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
package dev.sheldan.abstracto.core.api;

import dev.sheldan.abstracto.core.exception.GuildNotFoundException;
import dev.sheldan.abstracto.core.models.api.GuildDisplay;
import dev.sheldan.abstracto.core.service.GuildService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import net.dv8tion.jda.api.entities.Guild;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
public class ServerControllerTest {
@InjectMocks
private ServerController unitToTest;

@Mock
private ServerManagementService serverManagementService;

@Mock
private GuildService guildService;

@Mock
private Guild guild;

private static final Long GUILD_ID = 1L;

@Test
public void testExecuteRequest() {
when(guildService.getGuildById(GUILD_ID)).thenReturn(guild);
String guildIdString = String.valueOf(GUILD_ID);
when(guild.getId()).thenReturn(guildIdString);
GuildDisplay response = unitToTest.getLeaderboard(GUILD_ID);
assertThat(response.getId()).isEqualTo(guildIdString);
}

@Test
public void testExecuteServerNotFound() {
when(serverManagementService.loadServer(GUILD_ID)).thenThrow(new GuildNotFoundException(GUILD_ID));
assertThatThrownBy(() -> unitToTest.getLeaderboard(GUILD_ID))
.isInstanceOf(GuildNotFoundException.class)
.hasMessageContaining("Guild not found");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,13 @@
import dev.sheldan.abstracto.core.service.ChannelService;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.channel.concrete.TextChannel;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -53,12 +53,12 @@ public class AChannelParameterHandlerImplTest extends AbstractParameterHandlerTe
@Test
public void testSuccessfulCondition() {
when(unparsedCommandParameterPiece.getType()).thenReturn(ParameterPieceType.STRING);
Assert.assertTrue(testUnit.handles(AChannel.class, unparsedCommandParameterPiece));
assertThat(testUnit.handles(AChannel.class, unparsedCommandParameterPiece)).isTrue();
}

@Test
public void testWrongCondition() {
Assert.assertFalse(testUnit.handles(String.class, unparsedCommandParameterPiece));
assertThat(testUnit.handles(String.class, unparsedCommandParameterPiece)).isFalse();
}

@Test
Expand All @@ -67,7 +67,7 @@ public void testProperChannelMention() {
when(textChannelParameterHandler.handle(piece, iterators, parameter, message, command)).thenReturn(channel);
when(channelService.getFakeChannelFromTextChannel(channel)).thenReturn(aChannel);
AChannel parsed = (AChannel) testUnit.handle(piece, iterators, parameter, message, command);
Assert.assertEquals(aChannel, parsed);
assertThat(parsed).isEqualTo(aChannel);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import dev.sheldan.abstracto.core.service.EmoteService;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.emoji.RichCustomEmoji;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -61,12 +61,12 @@ public class AEmoteParameterHandlerImplImplTest extends AbstractParameterHandler
@Test
public void testSuccessfulCondition() {
when(unparsedCommandParameterPiece.getType()).thenReturn(ParameterPieceType.STRING);
Assert.assertTrue(testUnit.handles(AEmote.class, unparsedCommandParameterPiece));
assertThat(testUnit.handles(AEmote.class, unparsedCommandParameterPiece)).isTrue();
}

@Test
public void testWrongCondition() {
Assert.assertFalse(testUnit.handles(String.class, unparsedCommandParameterPiece));
assertThat(testUnit.handles(String.class, unparsedCommandParameterPiece)).isFalse();
}

@Test
Expand All @@ -76,7 +76,7 @@ public void testProperEmoteMention() {
when(emoteParameterHandler.handle(piece, iterators, parameter2, message, command)).thenReturn(emote);
when(emoteService.getFakeEmoteFromEmote(emote)).thenReturn(aEmote);
AEmote parsed = (AEmote) testUnit.handle(piece, iterators, parameter, message, command);
Assert.assertEquals(aEmote, parsed);
assertThat(parsed).isEqualTo(aEmote);
}

@Test
Expand All @@ -86,7 +86,7 @@ public void testDefaultEmoteHandling() {
when(emoteParameterHandler.handle(piece, iterators, parameter2, message, command)).thenReturn(null);
when(emoteService.getFakeEmote(INPUT)).thenReturn(aEmote);
AEmote parsed = (AEmote) testUnit.handle(piece, iterators, parameter, message, command);
Assert.assertEquals(aEmote, parsed);
assertThat(parsed).isEqualTo(aEmote);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,13 +9,13 @@
import dev.sheldan.abstracto.core.service.RoleService;
import net.dv8tion.jda.api.entities.Message;
import net.dv8tion.jda.api.entities.Role;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand Down Expand Up @@ -60,12 +60,12 @@ public class ARoleParameterHandlerImplImplTest extends AbstractParameterHandlerT
@Test
public void testSuccessfulCondition() {
when(unparsedCommandParameterPiece.getType()).thenReturn(ParameterPieceType.STRING);
Assert.assertTrue(testUnit.handles(ARole.class, unparsedCommandParameterPiece));
assertThat(testUnit.handles(ARole.class, unparsedCommandParameterPiece)).isTrue();
}

@Test
public void testWrongCondition() {
Assert.assertFalse(testUnit.handles(String.class, unparsedCommandParameterPiece));
assertThat(testUnit.handles(String.class, unparsedCommandParameterPiece)).isFalse();
}

@Test
Expand All @@ -75,7 +75,7 @@ public void testProperRoleMention() {
when(roleParameterHandler.handle(piece, iterators, parameter2, message, command)).thenReturn(role);
when(roleService.getFakeRoleFromRole(role)).thenReturn(aRole);
ARole parsed = (ARole) testUnit.handle(piece, iterators, parameter, message, command);
Assert.assertEquals(aRole, parsed);
assertThat(parsed).isEqualTo(aRole);
}


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.ParameterPieceType;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -31,30 +31,30 @@ public class BooleanParameterHandlerImplTest extends AbstractParameterHandlerTes
@Test
public void testSuccessfulCondition() {
when(unparsedCommandParameterPiece.getType()).thenReturn(ParameterPieceType.STRING);
Assert.assertTrue(testUnit.handles(Boolean.class, unparsedCommandParameterPiece));
assertThat(testUnit.handles(Boolean.class, unparsedCommandParameterPiece)).isTrue();
}

@Test
public void testWrongCondition() {
Assert.assertFalse(testUnit.handles(String.class, unparsedCommandParameterPiece));
assertThat(testUnit.handles(String.class, unparsedCommandParameterPiece)).isFalse();
}

@Test
public void testTrueParsing() {
UnparsedCommandParameterPiece piece = getPieceWithValue("true");
Assert.assertTrue((Boolean)testUnit.handle(piece, null, parameter, null, command));
assertThat((Boolean)testUnit.handle(piece, null, parameter, null, command)).isTrue();
}

@Test
public void testAnyOtherText() {
UnparsedCommandParameterPiece piece = getPieceWithValue("test");
Assert.assertFalse((Boolean)testUnit.handle(piece, null, parameter, null, command));
assertThat((Boolean)testUnit.handle(piece, null, parameter, null, command)).isFalse();
}

@Test
public void testEmptyStringAsInput() {
UnparsedCommandParameterPiece piece = getPieceWithValue("");
Assert.assertFalse((Boolean)testUnit.handle(piece, null, parameter, null, command));
assertThat((Boolean)testUnit.handle(piece, null, parameter, null, command)).isFalse();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
import dev.sheldan.abstracto.core.command.config.Parameter;
import dev.sheldan.abstracto.core.command.execution.ParameterPieceType;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;

import static org.assertj.core.api.AssertionsForClassTypes.*;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -31,42 +31,49 @@ public class DoubleParameterHandlerImplTest extends AbstractParameterHandlerTest
@Test
public void testSuccessfulCondition() {
when(unparsedCommandParameterPiece.getType()).thenReturn(ParameterPieceType.STRING);
Assert.assertTrue(testUnit.handles(Double.class, unparsedCommandParameterPiece));
assertThat(testUnit.handles(Double.class, unparsedCommandParameterPiece)).isTrue();
}

@Test
public void testWrongCondition() {
Assert.assertFalse(testUnit.handles(String.class, unparsedCommandParameterPiece));
assertThat(testUnit.handles(String.class, unparsedCommandParameterPiece)).isFalse();
}

@Test
public void testSuccessfulParse() {
Assert.assertEquals(5D, testUnit.handle(getPieceWithValue("5"), null, parameter, null, command));
assertThat(testUnit.handle(getPieceWithValue("5"), null, parameter, null, command)).isEqualTo(5D);
}

@Test
public void testNegativeNumber() {
Assert.assertEquals(-5D, testUnit.handle(getPieceWithValue("-5"), null, parameter, null, command));
assertThat(testUnit.handle(getPieceWithValue("-50"), null, parameter, null, command)).isEqualTo(-50D);
}


@Test
public void testDecimal() {
Assert.assertEquals(3.14D, testUnit.handle(getPieceWithValue("3.14"), null, parameter, null, command));
assertThat(testUnit.handle(getPieceWithValue("3.14"), null, parameter, null, command)).isEqualTo(3.14D);
}

@Test(expected = NumberFormatException.class)
@Test
public void testTextAsInput() {
testUnit.handle(getPieceWithValue("someText"), null, parameter, null, command);
assertThatThrownBy(() -> {
testUnit.handle(getPieceWithValue("someText"), null, parameter, null, command);
}).isInstanceOf(NumberFormatException.class);
}

@Test(expected = NullPointerException.class)
@Test
public void testNullInput() {
testUnit.handle(getPieceWithValue(null), null, parameter, null, command);
assertThatThrownBy(() -> {
testUnit.handle(getPieceWithValue(null), null, parameter, null, command);
}).isInstanceOf(NullPointerException.class);
}

@Test(expected = NumberFormatException.class)
@Test
public void testEmptyStringAsInput() {
testUnit.handle(getPieceWithValue(""), null, parameter, null, command);
assertThatThrownBy(() -> {
testUnit.handle(getPieceWithValue(""), null, parameter, null, command);
}).isInstanceOf(NumberFormatException.class);
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@
import dev.sheldan.abstracto.core.command.execution.ParameterPieceType;
import dev.sheldan.abstracto.core.command.execution.UnparsedCommandParameterPiece;
import dev.sheldan.abstracto.core.exception.DurationFormatException;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
Expand All @@ -15,6 +14,8 @@
import java.time.Duration;
import java.time.temporal.ChronoUnit;

import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
import static org.assertj.core.api.AssertionsForClassTypes.assertThatThrownBy;
import static org.mockito.Mockito.when;

@RunWith(MockitoJUnitRunner.class)
Expand All @@ -35,28 +36,30 @@ public class DurationParameterHandlerImplTest extends AbstractParameterHandlerTe
@Test
public void testSuccessfulCondition() {
when(unparsedCommandParameterPiece.getType()).thenReturn(ParameterPieceType.STRING);
Assert.assertTrue(testUnit.handles(Duration.class, unparsedCommandParameterPiece));
assertThat(testUnit.handles(Duration.class, unparsedCommandParameterPiece)).isTrue();
}

@Test
public void testWrongCondition() {
Assert.assertFalse(testUnit.handles(String.class, unparsedCommandParameterPiece));
assertThat(testUnit.handles(String.class, unparsedCommandParameterPiece)).isFalse();
}

@Test
public void testSimpleParsing() {
Assert.assertEquals(Duration.ofMinutes(1), testUnit.handle(getPieceWithValue("1m"), null, parameter, null, command));
assertThat(testUnit.handle(getPieceWithValue("1m"), null, parameter, null, command)).isEqualTo(Duration.ofMinutes(1));
}

@Test
public void testMoreComplicatedParsing() {
Duration targetDuration = Duration.ofDays(4).plus(5, ChronoUnit.HOURS).plus(5, ChronoUnit.MINUTES);
Assert.assertEquals(targetDuration, testUnit.handle(getPieceWithValue("5h5m4d"), null, parameter, null, command));
assertThat(testUnit.handle(getPieceWithValue("5h5m4d"), null, parameter, null, command)).isEqualTo(targetDuration);
}

@Test(expected = DurationFormatException.class)
@Test
public void testEmptyStringAsInput() {
testUnit.handle(getPieceWithValue(""), null, parameter, null, command);
assertThatThrownBy(() -> {
testUnit.handle(getPieceWithValue(""), null, parameter, null, command);
}).isInstanceOf(DurationFormatException.class);
}

}

0 comments on commit 02b8ed2

Please sign in to comment.