[AB-165] removing mocking utils and general test improvements

This commit is contained in:
Sheldan
2021-02-28 21:46:45 +01:00
parent 5f6746d742
commit 821971523e
55 changed files with 834 additions and 842 deletions

View File

@@ -56,7 +56,7 @@ public class UserNotesConverter {
List<NoteEntryModel> entryModels = new ArrayList<>();
futureHashMap.keySet().forEach(serverSpecificId -> {
Member member = futureHashMap.get(serverSpecificId).join();
UserNote note = userNoteManagementService.loadNote(serverSpecificId.getId(), serverSpecificId.getServerId());
UserNote note = userNoteManagementService.loadNote(serverSpecificId.getServerId(), serverSpecificId.getId());
FullUserInServer fullUser = FullUserInServer
.builder()
.member(member)

View File

@@ -47,7 +47,7 @@ public class UserNoteManagementServiceBean implements UserNoteManagementService
}
@Override
public UserNote loadNote(Long userNoteId, Long serverId) {
public UserNote loadNote(Long serverId, Long userNoteId) {
return userNoteRepository.findByUserNoteId_IdAndUserNoteId_ServerId(userNoteId, serverId);
}

View File

@@ -2,9 +2,6 @@ package dev.sheldan.abstracto.moderation.commands;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.test.MockUtils;
import dev.sheldan.abstracto.core.test.command.CommandConfigValidator;
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
import dev.sheldan.abstracto.moderation.models.database.Warning;
@@ -13,6 +10,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
@@ -29,15 +27,14 @@ public class DeleteWarningTest {
private WarnManagementService warnManagementService;
private static final Long WARN_ID = 5L;
private static final Long SERVER_ID = 1L;
@Test
public void testDeleteExistingWarning() {
AServer server = MockUtils.getServer();
AUserInAServer warnedUser = MockUtils.getUserObject(5L, server);
AUserInAServer warningUser = MockUtils.getUserObject(6L, server);
Warning existingWarning = Warning.builder().warnedUser(warnedUser).warningUser(warningUser).build();
Warning existingWarning = Mockito.mock(Warning.class);
CommandContext parameters = CommandTestUtilities.getWithParameters(Arrays.asList(WARN_ID));
when(warnManagementService.findByIdOptional(WARN_ID, parameters.getGuild().getIdLong())).thenReturn(Optional.of(existingWarning));
when(parameters.getGuild().getIdLong()).thenReturn(SERVER_ID);
when(warnManagementService.findByIdOptional(WARN_ID, SERVER_ID)).thenReturn(Optional.of(existingWarning));
CommandResult result = testUnit.execute(parameters);
verify(warnManagementService, times(1)).deleteWarning(existingWarning);
CommandTestUtilities.checkSuccessfulCompletion(result);

View File

@@ -2,10 +2,8 @@ package dev.sheldan.abstracto.moderation.commands;
import dev.sheldan.abstracto.core.command.execution.CommandContext;
import dev.sheldan.abstracto.core.command.execution.CommandResult;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.core.test.MockUtils;
import dev.sheldan.abstracto.core.test.command.CommandConfigValidator;
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
import dev.sheldan.abstracto.moderation.service.management.UserNoteManagementService;
@@ -36,8 +34,7 @@ public class UserNoteCommandTest {
@Test
public void testExecuteUserNoteCommand() {
Member member = Mockito.mock(Member.class);
AServer server = MockUtils.getServer();
AUserInAServer userInAServer = MockUtils.getUserObject(4L, server);
AUserInAServer userInAServer = Mockito.mock(AUserInAServer.class);
String note = "note";
CommandContext parameters = CommandTestUtilities.getWithParameters(Arrays.asList(member, note));
when(userInServerManagementService.loadOrCreateUser(member)).thenReturn(userInAServer);

View File

@@ -7,7 +7,6 @@ import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.core.test.MockUtils;
import dev.sheldan.abstracto.core.test.command.CommandConfigValidator;
import dev.sheldan.abstracto.core.test.command.CommandTestUtilities;
import dev.sheldan.abstracto.moderation.converter.UserNotesConverter;
@@ -56,15 +55,14 @@ public class UserNotesTest {
public void testExecuteUserNotesCommandForMember() {
Member member = Mockito.mock(Member.class);
CommandContext parameters = CommandTestUtilities.getWithParameters(Arrays.asList(member));
AServer server = Mockito.mock(AServer.class);
AUserInAServer userNoteUser = MockUtils.getUserObject(4L, server);
AUserInAServer userNoteUser = Mockito.mock(AUserInAServer.class);
when(userInServerManagementService.loadOrCreateUser(member)).thenReturn(userNoteUser);
UserNote firstNote = UserNote.builder().build();
UserNote secondNote = UserNote.builder().build();
UserNote firstNote = Mockito.mock(UserNote.class);
UserNote secondNote = Mockito.mock(UserNote.class);
List<UserNote> userNotes = Arrays.asList(firstNote, secondNote);
when(userNoteManagementService.loadNotesForUser(userNoteUser)).thenReturn(userNotes);
NoteEntryModel firstConvertedNote = NoteEntryModel.builder().build();
NoteEntryModel secondConvertedNote = NoteEntryModel.builder().build();
NoteEntryModel firstConvertedNote = Mockito.mock(NoteEntryModel.class);
NoteEntryModel secondConvertedNote = Mockito.mock(NoteEntryModel.class);
CompletableFuture<List<NoteEntryModel>> convertedNotes = CompletableFuture.completedFuture(Arrays.asList(firstConvertedNote, secondConvertedNote));
when(userNotesConverter.fromNotes(userNotes)).thenReturn(convertedNotes);
CompletableFuture<CommandResult> result = testUnit.executeAsync(parameters);
@@ -85,14 +83,14 @@ public class UserNotesTest {
@Test
public void testExecuteUserNotesCommandForServer() {
CommandContext parameters = CommandTestUtilities.getNoParameters();
UserNote firstNote = UserNote.builder().build();
UserNote secondNote = UserNote.builder().build();
UserNote firstNote = Mockito.mock(UserNote.class);
UserNote secondNote = Mockito.mock(UserNote.class);
List<UserNote> userNotes = Arrays.asList(firstNote, secondNote);
AServer server = Mockito.mock(AServer.class);
when(serverManagementService.loadServer(parameters.getGuild())).thenReturn(server);
when(userNoteManagementService.loadNotesForServer(server)).thenReturn(userNotes);
NoteEntryModel firstConvertedNote = NoteEntryModel.builder().build();
NoteEntryModel secondConvertedNote = NoteEntryModel.builder().build();
NoteEntryModel firstConvertedNote = Mockito.mock(NoteEntryModel.class);
NoteEntryModel secondConvertedNote = Mockito.mock(NoteEntryModel.class);
CompletableFuture<List<NoteEntryModel>> convertedNotes = CompletableFuture.completedFuture(Arrays.asList(firstConvertedNote, secondConvertedNote));
when(userNotesConverter.fromNotes(userNotes)).thenReturn(convertedNotes);
CompletableFuture<CommandResult> result = testUnit.executeAsync(parameters);

View File

@@ -14,6 +14,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
@@ -42,7 +43,7 @@ public class UnMuteTest {
@Test
public void testUnMuteCommand() {
CommandContext parameters = CommandTestUtilities.getWithParameters(Arrays.asList(memberToUnMute));
AUserInAServer user = AUserInAServer.builder().build();
AUserInAServer user = Mockito.mock(AUserInAServer.class);
when(userInServerManagementService.loadOrCreateUser(memberToUnMute)).thenReturn(user);
when(muteService.unMuteUser(user)).thenReturn(CompletableFuture.completedFuture(null));
CompletableFuture<CommandResult> result = testUnit.executeAsync(parameters);
@@ -52,7 +53,7 @@ public class UnMuteTest {
@Test(expected = NoMuteFoundException.class)
public void testUnMuteCommandWithoutExistingMute() {
CommandContext parameters = CommandTestUtilities.getWithParameters(Arrays.asList(memberToUnMute));
AUserInAServer user = AUserInAServer.builder().build();
AUserInAServer user = Mockito.mock(AUserInAServer.class);
when(userInServerManagementService.loadOrCreateUser(memberToUnMute)).thenReturn(user);
when(muteService.unMuteUser(user)).thenThrow(new NoMuteFoundException());
testUnit.executeAsync(parameters);

View File

@@ -1,11 +1,9 @@
package dev.sheldan.abstracto.moderation.converter;
import dev.sheldan.abstracto.core.models.ServerSpecificId;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.MemberService;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.core.test.MockUtils;
import dev.sheldan.abstracto.moderation.models.database.UserNote;
import dev.sheldan.abstracto.moderation.models.template.commands.NoteEntryModel;
import dev.sheldan.abstracto.moderation.service.management.UserNoteManagementService;
@@ -44,6 +42,9 @@ public class UserNotesConverterTest {
@Mock
private UserNoteManagementService userNoteManagementService;
private static final Long SERVER_ID = 3L;
private static final Long USER_NOTE_ID = 4L;
@Test
public void testWithEmptyList() {
CompletableFuture<List<NoteEntryModel>> entryModels = testUnit.fromNotes(Collections.emptyList());
@@ -52,12 +53,13 @@ public class UserNotesConverterTest {
@Test
public void testWithSomeUserNotes() {
AServer server = MockUtils.getServer();
AUserInAServer userInAServer = MockUtils.getUserObject(4L, server);
AUserInAServer userInAServer = Mockito.mock(AUserInAServer.class);
Member member = Mockito.mock(Member.class);
when(memberService.getMemberInServerAsync(userInAServer)).thenReturn(CompletableFuture.completedFuture(member));
UserNote firstNote = UserNote.builder().userNoteId(new ServerSpecificId(3L, 4L)).user(userInAServer).build();
UserNote secondNote = UserNote.builder().userNoteId(new ServerSpecificId(3L, 5L)).user(userInAServer).build();
UserNote firstNote = Mockito.mock(UserNote.class);
when(firstNote.getUser()).thenReturn(userInAServer);
UserNote secondNote = Mockito.mock(UserNote.class);
when(secondNote.getUser()).thenReturn(userInAServer);
testUnit.fromNotes(Arrays.asList(firstNote, secondNote));
verify(self, times(1)).loadFullNotes(any());
}
@@ -70,13 +72,13 @@ public class UserNotesConverterTest {
UserNote note2 = Mockito.mock(UserNote.class);
when(note1.getUser()).thenReturn(userInAServer);
when(note2.getUser()).thenReturn(userInAServer);
ServerSpecificId firstUserNoteId = new ServerSpecificId(3L, 4L);
ServerSpecificId secondUserNoteId = new ServerSpecificId(3L, 5L);
ServerSpecificId firstUserNoteId = new ServerSpecificId(SERVER_ID, USER_NOTE_ID);
ServerSpecificId secondUserNoteId = new ServerSpecificId(SERVER_ID, USER_NOTE_ID + 1);
HashMap<ServerSpecificId, CompletableFuture<Member>> map = new HashMap<>();
map.put(firstUserNoteId, CompletableFuture.completedFuture(member));
map.put(secondUserNoteId, CompletableFuture.completedFuture(member));
when(userNoteManagementService.loadNote(4L, 3L)).thenReturn(note1);
when(userNoteManagementService.loadNote(5L, 3L)).thenReturn(note2);
when(userNoteManagementService.loadNote(SERVER_ID, USER_NOTE_ID)).thenReturn(note1);
when(userNoteManagementService.loadNote(SERVER_ID, USER_NOTE_ID + 1)).thenReturn(note2);
List<NoteEntryModel> models = testUnit.loadFullNotes(map);
Assert.assertEquals(2, models.size());
NoteEntryModel firstEntry = models.get(0);

View File

@@ -18,10 +18,7 @@ import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.*;
import java.util.concurrent.CompletableFuture;
import static org.mockito.Mockito.*;
@@ -56,21 +53,30 @@ public class WarnEntryConverterTest {
}
@Test
public void testWithSomeUserNotes() {
public void testWithSomeWarnings() {
AUserInAServer warnedUser = Mockito.mock(AUserInAServer.class);
AUserInAServer warningUser = Mockito.mock(AUserInAServer.class);
Member warnedMember = Mockito.mock(Member.class);
Member warningMember = Mockito.mock(Member.class);
when(memberService.getMemberInServerAsync(warnedUser)).thenReturn(CompletableFuture.completedFuture(warnedMember));
when(memberService.getMemberInServerAsync(warningUser)).thenReturn(CompletableFuture.completedFuture(warningMember));
Warning firstNote = Warning.builder().warnId(new ServerSpecificId(3L, 4L)).warnedUser(warnedUser).warningUser(warningUser).build();
Warning secondNote = Warning.builder().warnId(new ServerSpecificId(3L, 5L)).warnedUser(warnedUser).warningUser(warningUser).build();
testUnit.fromWarnings(Arrays.asList(firstNote, secondNote));
Warning firstWarning = Mockito.mock(Warning.class);
when(firstWarning.getWarningUser()).thenReturn(warningUser);
when(firstWarning.getWarnedUser()).thenReturn(warnedUser);
Warning secondWarning = Mockito.mock(Warning.class);
when(secondWarning.getWarningUser()).thenReturn(warningUser);
when(secondWarning.getWarnedUser()).thenReturn(warnedUser);
List<WarnEntry> loaded = new ArrayList<>();
when(self.loadFullWarnEntries(any())).thenReturn(loaded);
CompletableFuture<List<WarnEntry>> future = testUnit.fromWarnings(Arrays.asList(firstWarning, secondWarning));
List<WarnEntry> entries = future.join();
Assert.assertFalse(future.isCompletedExceptionally());
Assert.assertEquals(loaded, entries);
verify(self, times(1)).loadFullWarnEntries(any());
}
@Test
public void testLoadingFullNotes() {
public void testLoadingFullWarnings() {
AUserInAServer warnedUser = Mockito.mock(AUserInAServer.class);
AUserInAServer warningUser = Mockito.mock(AUserInAServer.class);
Member warningMember = Mockito.mock(Member.class);
@@ -82,7 +88,9 @@ public class WarnEntryConverterTest {
ServerSpecificId secondWarnId = new ServerSpecificId(SERVER_ID, WARN_ID_2);
when(warning2.getWarnId()).thenReturn(secondWarnId);
HashMap<ServerSpecificId, FutureMemberPair> map = new HashMap<>();
FutureMemberPair memberPair = FutureMemberPair.builder().firstMember(CompletableFuture.completedFuture(warningMember)).secondMember(CompletableFuture.completedFuture(warnedMember)).build();
FutureMemberPair memberPair = Mockito.mock(FutureMemberPair.class);
when(memberPair.getFirstMember()).thenReturn(CompletableFuture.completedFuture(warningMember));
when(memberPair.getSecondMember()).thenReturn(CompletableFuture.completedFuture(warnedMember));
map.put(firstWarnId, memberPair);
map.put(secondWarnId, memberPair);
when(warnManagementService.findById(WARN_ID_1, SERVER_ID)).thenReturn(warning1);

View File

@@ -3,11 +3,9 @@ package dev.sheldan.abstracto.moderation.job;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.FeatureFlagService;
import dev.sheldan.abstracto.core.service.management.ServerManagementService;
import dev.sheldan.abstracto.core.test.MockUtils;
import dev.sheldan.abstracto.moderation.config.features.WarningDecayFeature;
import dev.sheldan.abstracto.moderation.service.WarnService;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
@@ -41,14 +39,14 @@ public class WarnDecayJobTest {
@Mock
private WarnService warnService;
@Mock
private AServer firstServer;
@Mock
private AServer secondServer;
@Before
public void setup() {
this.firstServer = MockUtils.getServer(1L);
this.secondServer = MockUtils.getServer(2L);
}
private static final Long SERVER_ID = 1L;
private static final Long SERVER_ID_2 = 2L;
@Test
public void executeJobForNoServers() throws JobExecutionException {
@@ -60,6 +58,7 @@ public class WarnDecayJobTest {
@Test
public void executeJobForAEnabledServer() throws JobExecutionException {
when(firstServer.getId()).thenReturn(SERVER_ID);
when(serverManagementService.getAllServers()).thenReturn(Arrays.asList(firstServer));
when(featureFlagService.isFeatureEnabled(warningDecayFeature, firstServer)).thenReturn(true);
testUnit.executeInternal(null);
@@ -76,6 +75,7 @@ public class WarnDecayJobTest {
@Test
public void executeJobForMixedServers() throws JobExecutionException {
when(firstServer.getId()).thenReturn(SERVER_ID);
when(serverManagementService.getAllServers()).thenReturn(Arrays.asList(firstServer, secondServer));
when(featureFlagService.isFeatureEnabled(warningDecayFeature, firstServer)).thenReturn(true);
when(featureFlagService.isFeatureEnabled(warningDecayFeature, secondServer)).thenReturn(false);
@@ -85,6 +85,8 @@ public class WarnDecayJobTest {
@Test
public void executeJobForMultipleEnabledServers() throws JobExecutionException {
when(firstServer.getId()).thenReturn(SERVER_ID);
when(secondServer.getId()).thenReturn(SERVER_ID_2);
when(serverManagementService.getAllServers()).thenReturn(Arrays.asList(firstServer, secondServer));
when(featureFlagService.isFeatureEnabled(warningDecayFeature, firstServer)).thenReturn(true);
when(featureFlagService.isFeatureEnabled(warningDecayFeature, secondServer)).thenReturn(true);

View File

@@ -25,8 +25,8 @@ import java.util.Arrays;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import java.util.concurrent.TimeUnit;
import java.util.function.Consumer;
import static dev.sheldan.abstracto.core.test.MockUtils.mockQueueDoubleVoidConsumer;
import static org.mockito.Mockito.*;
@RunWith(MockitoJUnitRunner.class)
@@ -252,6 +252,17 @@ public class PurgeServiceBeanTest {
setupStatusMessageMocks();
}
public void mockQueueDoubleVoidConsumer(RestAction action) {
doAnswer(invocationOnMock -> {
Object consumerObj = invocationOnMock.getArguments()[0];
if(consumerObj instanceof Consumer) {
Consumer<Void> consumer = (Consumer) consumerObj;
consumer.accept(null);
}
return null;
}).when(action).queue(any(Consumer.class), any(Consumer.class));
}
private void setupFirstMessageHistoryMocks() {
List<Message> messagesToDelete = Arrays.asList(firstMessage, secondMessage);
when(history.getRetrievedHistory()).thenReturn(messagesToDelete);

View File

@@ -4,7 +4,6 @@ import dev.sheldan.abstracto.core.exception.ChannelNotInGuildException;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.service.ChannelService;
import dev.sheldan.abstracto.core.test.MockUtils;
import net.dv8tion.jda.api.entities.Guild;
import net.dv8tion.jda.api.entities.TextChannel;
import net.dv8tion.jda.api.managers.ChannelManager;
@@ -12,6 +11,7 @@ import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.time.Duration;
@@ -37,6 +37,9 @@ public class SlowModeServiceBeanTest {
@Mock
private TextChannel channel;
private static final Long SERVER_ID = 4L;
private static final Long CHANNEL_ID = 5L;
@Test
public void setSlowModeInTextChannel() {
when(channel.getGuild()).thenReturn(guild);
@@ -47,11 +50,14 @@ public class SlowModeServiceBeanTest {
@Test
public void testSlowModeInAChannel() {
AServer server = MockUtils.getServer();
AChannel aChannel = MockUtils.getTextChannel(server, 5L);
AServer server = Mockito.mock(AServer.class);
when(server.getId()).thenReturn(SERVER_ID);
AChannel aChannel = Mockito.mock(AChannel.class);
when(aChannel.getServer()).thenReturn(server);
when(aChannel.getId()).thenReturn(CHANNEL_ID);
Duration duration = Duration.ofMinutes(5);
when(channel.getGuild()).thenReturn(guild);
when(channelService.getTextChannelFromServerOptional(server.getId(), aChannel.getId())).thenReturn(Optional.of(channel));
when(channelService.getTextChannelFromServerOptional(SERVER_ID, CHANNEL_ID)).thenReturn(Optional.of(channel));
testUnit.setSlowMode(aChannel, duration);
verify(channelService, times(1)).setSlowModeInChannel(channel,(int) duration.getSeconds());
}
@@ -72,10 +78,13 @@ public class SlowModeServiceBeanTest {
@Test(expected = ChannelNotInGuildException.class)
public void testSlowModeInAChannelNotFound() {
AServer server = MockUtils.getServer();
AChannel aChannel = MockUtils.getTextChannel(server, 5L);
AServer server = Mockito.mock(AServer.class);
when(server.getId()).thenReturn(SERVER_ID);
AChannel aChannel = Mockito.mock(AChannel.class);
when(aChannel.getServer()).thenReturn(server);
when(aChannel.getId()).thenReturn(CHANNEL_ID);
Duration duration = Duration.ofMinutes(5);
when(channelService.getTextChannelFromServerOptional(server.getId(), aChannel.getId())).thenReturn(Optional.empty());
when(channelService.getTextChannelFromServerOptional(SERVER_ID, CHANNEL_ID)).thenReturn(Optional.empty());
testUnit.setSlowMode(aChannel, duration);
}
}

View File

@@ -4,9 +4,9 @@ import dev.sheldan.abstracto.core.models.AServerAChannelMessage;
import dev.sheldan.abstracto.core.models.ServerSpecificId;
import dev.sheldan.abstracto.core.models.database.AChannel;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUser;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.management.UserInServerManagementService;
import dev.sheldan.abstracto.core.test.MockUtils;
import dev.sheldan.abstracto.moderation.models.database.Mute;
import dev.sheldan.abstracto.moderation.repository.MuteRepository;
import net.dv8tion.jda.api.entities.Member;
@@ -38,17 +38,28 @@ public class MuteManagementServiceBeanTest {
@Captor
private ArgumentCaptor<Mute> muteArgumentCaptor;
private static final Long SERVER_ID = 1L;
private static final Long MUTE_ID = 2L;
@Test
public void testCreateMute() {
AServer server = MockUtils.getServer();
AServer server = Mockito.mock(AServer.class);
long messageId = 9L;
AChannel channel = MockUtils.getTextChannel(server, 8L);
AUserInAServer mutingUser = MockUtils.getUserObject(5L, server);
AUserInAServer mutedUser = MockUtils.getUserObject(7L, server);
AChannel channel = Mockito.mock(AChannel.class);
AUserInAServer mutingUser = Mockito.mock(AUserInAServer.class);
AUserInAServer mutedUser = Mockito.mock(AUserInAServer.class);
AUser user = Mockito.mock(AUser.class);
when(mutedUser.getUserReference()).thenReturn(user);
when(mutedUser.getServerReference()).thenReturn(server);
AUser secondUser = Mockito.mock(AUser.class);
when(mutingUser.getUserReference()).thenReturn(secondUser);
String reason = "reason";
String triggerKey = "key";
Instant unMuteDate = Instant.now();
AServerAChannelMessage muteMessage = AServerAChannelMessage.builder().server(server).channel(channel).messageId(messageId).build();
AServerAChannelMessage muteMessage = Mockito.mock(AServerAChannelMessage.class);
when(muteMessage.getMessageId()).thenReturn(messageId);
when(muteMessage.getServer()).thenReturn(server);
when(muteMessage.getChannel()).thenReturn(channel);
testUnit.createMute(mutedUser, mutingUser, reason, unMuteDate, muteMessage, triggerKey, 8L);
verify(muteRepository, times(1)).save(muteArgumentCaptor.capture());
@@ -65,36 +76,34 @@ public class MuteManagementServiceBeanTest {
@Test
public void testFindMute() {
Long id = 5L;
Long serverId = 7L;
Mute mute = Mute.builder().muteId(new ServerSpecificId(serverId, id)).build();
when(muteRepository.findByMuteId_IdAndMuteId_ServerId(id, serverId)).thenReturn(Optional.of(mute));
Optional<Mute> foundMuteOptional = testUnit.findMuteOptional(id, serverId);
Mute mute = Mockito.mock(Mute.class);
ServerSpecificId muteId = Mockito.mock(ServerSpecificId.class);
when(mute.getMuteId()).thenReturn(muteId);
when(muteId.getId()).thenReturn(MUTE_ID);
when(muteRepository.findByMuteId_IdAndMuteId_ServerId(MUTE_ID, SERVER_ID)).thenReturn(Optional.of(mute));
Optional<Mute> foundMuteOptional = testUnit.findMuteOptional(MUTE_ID, SERVER_ID);
Assert.assertTrue(foundMuteOptional.isPresent());
foundMuteOptional.ifPresent(foundMute -> Assert.assertEquals(id, foundMute.getMuteId().getId()));
foundMuteOptional.ifPresent(foundMute -> Assert.assertEquals(MUTE_ID, foundMute.getMuteId().getId()));
}
@Test
public void testFindNonExistingMute() {
Long id = 5L;
Long serverId = 7L;
when(muteRepository.findByMuteId_IdAndMuteId_ServerId(id, serverId)).thenReturn(Optional.empty());
Optional<Mute> foundMuteOptional = testUnit.findMuteOptional(id, serverId);
when(muteRepository.findByMuteId_IdAndMuteId_ServerId(MUTE_ID, SERVER_ID)).thenReturn(Optional.empty());
Optional<Mute> foundMuteOptional = testUnit.findMuteOptional(MUTE_ID, SERVER_ID);
Assert.assertFalse(foundMuteOptional.isPresent());
}
@Test
public void testSaveMute() {
Mute mute = Mute.builder().build();
Mute mute = Mockito.mock(Mute.class);
testUnit.saveMute(mute);
verify(muteRepository, times(1)).save(mute);
}
@Test
public void testGetMuteOfUser() {
AServer server = MockUtils.getServer();
AUserInAServer userInAServer = MockUtils.getUserObject(9L, server);
Mute mute = Mute.builder().build();
AUserInAServer userInAServer = Mockito.mock(AUserInAServer.class);
Mute mute = Mockito.mock(Mute.class);
when(muteRepository.findTopByMutedUserAndMuteEndedFalse(userInAServer)).thenReturn(mute);
Mute aMuteOf = testUnit.getAMuteOf(userInAServer);
Assert.assertEquals(mute, aMuteOf);
@@ -102,11 +111,10 @@ public class MuteManagementServiceBeanTest {
@Test
public void testGetMuteOfMember() {
AServer server = MockUtils.getServer();
AUserInAServer userInAServer = MockUtils.getUserObject(9L, server);
AUserInAServer userInAServer = Mockito.mock(AUserInAServer.class);
Mute mute = Mockito.mock(Mute.class);
Member member = Mockito.mock(Member.class);
when(userInServerManagementService.loadOrCreateUser(member)).thenReturn(userInAServer);
Mute mute = Mute.builder().build();
when(muteRepository.findTopByMutedUserAndMuteEndedFalse(userInAServer)).thenReturn(mute);
Mute aMuteOf = testUnit.getAMuteOf(member);
Assert.assertEquals(mute, aMuteOf);
@@ -114,14 +122,13 @@ public class MuteManagementServiceBeanTest {
@Test
public void testGetAllMutesOf() {
AServer server = MockUtils.getServer();
AUserInAServer userInAServer = MockUtils.getUserObject(9L, server);
Mute mute1 = Mute.builder().build();
Mute mute2 = Mute.builder().build();
when(muteRepository.findAllByMutedUserAndMuteEndedFalseOrderByMuteId_IdDesc(userInAServer)).thenReturn(Arrays.asList(mute1, mute2));
AUserInAServer userInAServer = Mockito.mock(AUserInAServer.class);
Mute mute = Mockito.mock(Mute.class);
Mute mute2 = Mockito.mock(Mute.class);
when(muteRepository.findAllByMutedUserAndMuteEndedFalseOrderByMuteId_IdDesc(userInAServer)).thenReturn(Arrays.asList(mute, mute2));
List<Mute> allMutesOf = testUnit.getAllMutesOf(userInAServer);
Assert.assertEquals(2, allMutesOf.size());
Assert.assertEquals(mute1, allMutesOf.get(0));
Assert.assertEquals(mute, allMutesOf.get(0));
Assert.assertEquals(mute2, allMutesOf.get(1));
}
@@ -136,8 +143,7 @@ public class MuteManagementServiceBeanTest {
}
private void checkExist(boolean value) {
AServer server = MockUtils.getServer();
AUserInAServer userInAServer = MockUtils.getUserObject(9L, server);
AUserInAServer userInAServer = Mockito.mock(AUserInAServer.class);
when(muteRepository.existsByMutedUserAndMuteEndedFalse(userInAServer)).thenReturn(value);
boolean result = testUnit.hasActiveMute(userInAServer);
Assert.assertEquals(value, result);

View File

@@ -2,15 +2,14 @@ package dev.sheldan.abstracto.moderation.service.management;
import dev.sheldan.abstracto.core.models.database.ARole;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.test.MockUtils;
import dev.sheldan.abstracto.moderation.models.database.MuteRole;
import dev.sheldan.abstracto.moderation.repository.MuteRoleRepository;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
@@ -27,16 +26,12 @@ public class MuteRoleManagementServiceBeanTest {
@Mock
private MuteRoleRepository muteRoleRepository;
@Mock
private AServer server;
@Before
public void setup() {
this.server = MockUtils.getServer();
}
@Test
public void testRetrieveMuteRoleForServer() {
MuteRole role = getMuteRole();
MuteRole role = Mockito.mock(MuteRole.class);
when(muteRoleRepository.findByRoleServer(server)).thenReturn(role);
MuteRole muteRole = testUnit.retrieveMuteRoleForServer(server);
Assert.assertEquals(role, muteRole);
@@ -50,14 +45,14 @@ public class MuteRoleManagementServiceBeanTest {
@Test
public void testCreateMuteRoleForServer() {
ARole role = ARole.builder().build();
ARole role = Mockito.mock(ARole.class);
MuteRole muteRoleForServer = testUnit.createMuteRoleForServer(server, role);
verifyRoleSaved(role, muteRoleForServer, 1);
}
@Test
public void testRetrieveRolesForServer() {
List<MuteRole> existingRoles = Arrays.asList(getMuteRole(), getMuteRole());
List<MuteRole> existingRoles = Arrays.asList(Mockito.mock(MuteRole.class), Mockito.mock(MuteRole.class));
when(muteRoleRepository.findAllByRoleServer(server)).thenReturn(existingRoles);
List<MuteRole> foundRoles = testUnit.retrieveMuteRolesForServer(server);
Assert.assertEquals(existingRoles.size(), foundRoles.size());
@@ -70,7 +65,7 @@ public class MuteRoleManagementServiceBeanTest {
@Test
public void testSetMuteRoleWithoutPrevious() {
ARole role = ARole.builder().build();
ARole role = Mockito.mock(ARole.class);
when(muteRoleRepository.existsByRoleServer(server)).thenReturn(false);
MuteRole muteRole = testUnit.setMuteRoleForServer(server, role);
verifyRoleSaved(role, muteRole, 1);
@@ -78,9 +73,10 @@ public class MuteRoleManagementServiceBeanTest {
@Test
public void testSetMuteRoleWithPrevious() {
ARole role = ARole.builder().build();
ARole role = Mockito.mock(ARole.class);
when(muteRoleRepository.existsByRoleServer(server)).thenReturn(true);
MuteRole existingRole = getMuteRole();
MuteRole existingRole = Mockito.mock(MuteRole.class);
when(existingRole.getRole()).thenReturn(role);
when(muteRoleRepository.findByRoleServer(server)).thenReturn(existingRole);
MuteRole muteRole = testUnit.setMuteRoleForServer(server, role);
verifyRoleSaved(role, muteRole, 0);
@@ -88,13 +84,8 @@ public class MuteRoleManagementServiceBeanTest {
private void verifyRoleSaved(ARole role, MuteRole muteRoleForServer, Integer saveCount) {
Assert.assertEquals(role, muteRoleForServer.getRole());
Assert.assertEquals(server, muteRoleForServer.getRoleServer());
verify(muteRoleRepository, times(saveCount)).save(muteRoleForServer);
}
private MuteRole getMuteRole() {
return MuteRole.builder().roleServer(server).build();
}
}

View File

@@ -1,17 +1,17 @@
package dev.sheldan.abstracto.moderation.service.management;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUser;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.service.CounterService;
import dev.sheldan.abstracto.core.test.MockUtils;
import dev.sheldan.abstracto.moderation.models.database.UserNote;
import dev.sheldan.abstracto.moderation.repository.UserNoteRepository;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.util.Arrays;
@@ -33,40 +33,43 @@ public class UserNoteManagementServiceBeanTest {
private static final String NOTE_TEXT = "noteText";
private static final Long NOTE_ID = 5L;
private static final Long SERVER_ID = 1L;
@Mock
private AServer server;
private AUserInAServer userInAServer;
@Before
public void setup() {
this.server = MockUtils.getServer();
this.userInAServer = MockUtils.getUserObject(8L, server);
}
@Mock
private AUserInAServer userInAServer;
@Test
public void testCreateUserNote() {
AUser user = Mockito.mock(AUser.class);
when(userInAServer.getUserReference()).thenReturn(user);
when(userInAServer.getServerReference()).thenReturn(server);
UserNote userNote = testUnit.createUserNote(userInAServer, NOTE_TEXT);
verify(userNoteRepository, times(1)).save(userNote);
Assert.assertEquals(userNote.getUser(), userInAServer);
Assert.assertEquals(userNote.getNote(), NOTE_TEXT);
Assert.assertEquals(userInAServer, userNote.getUser());
Assert.assertEquals(NOTE_TEXT, userNote.getNote());
}
@Test
public void testDeleteNote() {
when(server.getId()).thenReturn(SERVER_ID);
testUnit.deleteNote(NOTE_ID, server);
verify(userNoteRepository, times(1)).deleteByUserNoteId_IdAndUserNoteId_ServerId(NOTE_ID, server.getId());
verify(userNoteRepository, times(1)).deleteByUserNoteId_IdAndUserNoteId_ServerId(NOTE_ID, SERVER_ID);
}
@Test
public void testNoteExists() {
when(userNoteRepository.existsByUserNoteId_IdAndUserNoteId_ServerId(NOTE_ID, server.getId())).thenReturn(true);
when(server.getId()).thenReturn(SERVER_ID);
when(userNoteRepository.existsByUserNoteId_IdAndUserNoteId_ServerId(NOTE_ID, SERVER_ID)).thenReturn(true);
Assert.assertTrue(testUnit.noteExists(NOTE_ID, server));
}
@Test
public void testLoadNotesForUser() {
UserNote note = UserNote.builder().build();
UserNote note2 = UserNote.builder().build();
UserNote note = Mockito.mock(UserNote.class);
UserNote note2 = Mockito.mock(UserNote.class);
List<UserNote> notes = Arrays.asList(note, note2);
when(userNoteRepository.findByUser(userInAServer)).thenReturn(notes);
List<UserNote> foundNotes = testUnit.loadNotesForUser(userInAServer);
@@ -80,8 +83,8 @@ public class UserNoteManagementServiceBeanTest {
@Test
public void testLoadNotesForServer() {
UserNote note = UserNote.builder().build();
UserNote note2 = UserNote.builder().build();
UserNote note = Mockito.mock(UserNote.class);
UserNote note2 = Mockito.mock(UserNote.class);
List<UserNote> notes = Arrays.asList(note, note2);
when(userNoteRepository.findByUser_ServerReference(server)).thenReturn(notes);
List<UserNote> foundNotes = testUnit.loadNotesForServer(server);

View File

@@ -2,16 +2,16 @@ package dev.sheldan.abstracto.moderation.service.management;
import dev.sheldan.abstracto.core.models.ServerSpecificId;
import dev.sheldan.abstracto.core.models.database.AServer;
import dev.sheldan.abstracto.core.models.database.AUser;
import dev.sheldan.abstracto.core.models.database.AUserInAServer;
import dev.sheldan.abstracto.core.test.MockUtils;
import dev.sheldan.abstracto.moderation.models.database.Warning;
import dev.sheldan.abstracto.moderation.repository.WarnRepository;
import org.junit.Assert;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.Mockito;
import org.mockito.junit.MockitoJUnitRunner;
import java.time.Instant;
@@ -30,19 +30,22 @@ public class WarnManagementServiceBeanTest {
@Mock
private WarnRepository warnRepository;
@Mock
private AUserInAServer warnedUser;
@Mock
private AServer server;
@Before
public void setup() {
this.server = MockUtils.getServer();
this.warnedUser = MockUtils.getUserObject(5L, server);
}
private static final Long SERVER_ID = 1L;
private static final Long WARN_ID = 2L;
@Test
public void testCreateWarning() {
AUserInAServer warningUser = MockUtils.getUserObject(7L, server);
AUserInAServer warningUser = Mockito.mock(AUserInAServer.class);
AUser user = Mockito.mock(AUser.class);
when(warningUser.getServerReference()).thenReturn(server);
when(warningUser.getUserReference()).thenReturn(user);
when(warnedUser.getUserReference()).thenReturn(user);
String reason = "REASON";
Warning warning = testUnit.createWarning(warnedUser, warningUser, reason, 8L);
Assert.assertEquals(warningUser, warning.getWarningUser());
@@ -55,7 +58,7 @@ public class WarnManagementServiceBeanTest {
@Test
public void testRetrieveWarningsOlderThan() {
Instant date = Instant.now();
List<Warning> existingWarnings = Arrays.asList(getWarning(), getWarning());
List<Warning> existingWarnings = Arrays.asList(Mockito.mock(Warning.class), Mockito.mock(Warning.class));
when(warnRepository.findAllByWarnedUser_ServerReferenceAndDecayedFalseAndWarnDateLessThan(server, date)).thenReturn(existingWarnings);
List<Warning> activeWarningsInServerOlderThan = testUnit.getActiveWarningsInServerOlderThan(server, date);
checkFoundWarns(existingWarnings, activeWarningsInServerOlderThan);
@@ -71,7 +74,7 @@ public class WarnManagementServiceBeanTest {
@Test
public void testGetAllWarningsOfUser() {
List<Warning> existingWarnings = Arrays.asList(getWarning(), getWarning());
List<Warning> existingWarnings = Arrays.asList(Mockito.mock(Warning.class), Mockito.mock(Warning.class));
when(warnRepository.findByWarnedUser(warnedUser)).thenReturn(existingWarnings);
List<Warning> foundWarnings = testUnit.getAllWarnsForUser(warnedUser);
checkFoundWarns(existingWarnings, foundWarnings);
@@ -79,7 +82,7 @@ public class WarnManagementServiceBeanTest {
@Test
public void testGetAllWarningsOfServer() {
List<Warning> existingWarnings = Arrays.asList(getWarning(), getWarning());
List<Warning> existingWarnings = Arrays.asList(Mockito.mock(Warning.class), Mockito.mock(Warning.class));
when(warnRepository.findAllByWarnedUser_ServerReference(server)).thenReturn(existingWarnings);
List<Warning> foundWarnings = testUnit.getAllWarningsOfServer(server);
checkFoundWarns(existingWarnings, foundWarnings);
@@ -95,11 +98,9 @@ public class WarnManagementServiceBeanTest {
@Test
public void testFindByIdExisting() {
Long warnId = 6L;
Long serverId = 8L;
Warning existingWarning = getWarning();
when(warnRepository.findByWarnId_IdAndWarnId_ServerId(warnId, serverId)).thenReturn(Optional.ofNullable(existingWarning));
Optional<Warning> warningOptional = testUnit.findByIdOptional(warnId, serverId);
Warning existingWarning = Mockito.mock(Warning.class);
when(warnRepository.findByWarnId_IdAndWarnId_ServerId(WARN_ID, SERVER_ID)).thenReturn(Optional.ofNullable(existingWarning));
Optional<Warning> warningOptional = testUnit.findByIdOptional(WARN_ID, SERVER_ID);
Assert.assertTrue(warningOptional.isPresent());
warningOptional.ifPresent(foundWarning -> Assert.assertEquals(existingWarning, foundWarning));
}
@@ -107,15 +108,18 @@ public class WarnManagementServiceBeanTest {
@Test
public void testFindByIdNotExisting() {
Long warnId = 6L;
Long serverId = 8L;
when(warnRepository.findByWarnId_IdAndWarnId_ServerId(warnId, serverId)).thenReturn(Optional.ofNullable(null));
Optional<Warning> warningOptional = testUnit.findByIdOptional(warnId, serverId);
when(warnRepository.findByWarnId_IdAndWarnId_ServerId(warnId, SERVER_ID)).thenReturn(Optional.ofNullable(null));
Optional<Warning> warningOptional = testUnit.findByIdOptional(warnId, SERVER_ID);
Assert.assertFalse(warningOptional.isPresent());
}
@Test
public void testDeleteWarning() {
Warning warning = getWarning();
Warning warning = Mockito.mock(Warning.class);
ServerSpecificId warnId = Mockito.mock(ServerSpecificId.class);
when(warnId.getServerId()).thenReturn(SERVER_ID);
when(warnId.getId()).thenReturn(WARN_ID);
when(warning.getWarnId()).thenReturn(warnId);
testUnit.deleteWarning(warning);
verify(warnRepository, times(1)).delete(warning);
}
@@ -130,7 +134,5 @@ public class WarnManagementServiceBeanTest {
}
}
private Warning getWarning() {
return Warning.builder().warnId(new ServerSpecificId(3L, 4L)).build();
}
}