Rename ...Dao -> ...Repository

This commit is contained in:
P0nk
2024-09-14 12:06:09 +02:00
parent abbec8120e
commit 08eeeb54dc
17 changed files with 103 additions and 103 deletions

View File

@@ -1,10 +0,0 @@
package database;
import org.jdbi.v3.core.JdbiException;
public class DaoException extends JdbiException {
public DaoException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -0,0 +1,10 @@
package database;
import org.jdbi.v3.core.JdbiException;
public class DatabaseException extends JdbiException {
public DatabaseException(String message, Throwable cause) {
super(message, cause);
}
}

View File

@@ -3,7 +3,7 @@ package database.character;
import client.Character; import client.Character;
import client.Client; import client.Client;
import client.MonsterBook; import client.MonsterBook;
import database.monsterbook.MonsterCardDao; import database.monsterbook.MonsterCardRepository;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
@@ -12,10 +12,10 @@ import java.util.Optional;
public class CharacterLoader { public class CharacterLoader {
private static final Logger log = LoggerFactory.getLogger(CharacterLoader.class); private static final Logger log = LoggerFactory.getLogger(CharacterLoader.class);
private final MonsterCardDao monsterCardDao; private final MonsterCardRepository monsterCardRepository;
public CharacterLoader(MonsterCardDao monsterCardDao) { public CharacterLoader(MonsterCardRepository monsterCardRepository) {
this.monsterCardDao = monsterCardDao; this.monsterCardRepository = monsterCardRepository;
} }
public Optional<Character> loadForChannel(int chrId, Client client) { public Optional<Character> loadForChannel(int chrId, Client client) {
@@ -33,7 +33,7 @@ public class CharacterLoader {
} }
private MonsterBook loadMonsterBook(int chrId) { private MonsterBook loadMonsterBook(int chrId) {
var monsterCards = monsterCardDao.load(chrId); var monsterCards = monsterCardRepository.load(chrId);
return new MonsterBook(monsterCards); return new MonsterBook(monsterCards);
} }
} }

View File

@@ -1,7 +1,7 @@
package database.character; package database.character;
import client.Character; import client.Character;
import database.monsterbook.MonsterCardDao; import database.monsterbook.MonsterCardRepository;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import tools.DatabaseConnection; import tools.DatabaseConnection;
@@ -11,10 +11,10 @@ import java.sql.SQLException;
public class CharacterSaver { public class CharacterSaver {
private static final Logger log = LoggerFactory.getLogger(CharacterSaver.class); private static final Logger log = LoggerFactory.getLogger(CharacterSaver.class);
private final MonsterCardDao monsterCardDao; private final MonsterCardRepository monsterCardRepository;
public CharacterSaver(MonsterCardDao monsterCardDao) { public CharacterSaver(MonsterCardRepository monsterCardRepository) {
this.monsterCardDao = monsterCardDao; this.monsterCardRepository = monsterCardRepository;
} }
public void save(Character chr) { public void save(Character chr) {
@@ -42,7 +42,7 @@ public class CharacterSaver {
} }
// Saving monster cards to both MySQL and Postgres for now // Saving monster cards to both MySQL and Postgres for now
monsterCardDao.save(chr.getId(), chr.getMonsterBook().getCards()); monsterCardRepository.save(chr.getId(), chr.getMonsterBook().getCards());
} }
} }

View File

@@ -10,20 +10,20 @@ import java.util.List;
import java.util.Optional; import java.util.Optional;
public class DropProvider { public class DropProvider {
private final DropDao dropDao; private final DropRepository dropRepository;
private final Cache<Integer, List<MonsterDrop>> monsterDropCache = Caffeine.newBuilder().build(); private final Cache<Integer, List<MonsterDrop>> monsterDropCache = Caffeine.newBuilder().build();
private final Cache<Integer, List<MonsterGlobalDropEntry>> globalContinentDropCache = Caffeine.newBuilder().build(); private final Cache<Integer, List<MonsterGlobalDropEntry>> globalContinentDropCache = Caffeine.newBuilder().build();
private volatile List<GlobalMonsterDrop> globalMonsterDrops = null; private volatile List<GlobalMonsterDrop> globalMonsterDrops = null;
public DropProvider(DropDao dropDao) { public DropProvider(DropRepository dropRepository) {
if (dropDao == null) { if (dropRepository == null) {
throw new IllegalArgumentException("DropDao must not be null"); throw new IllegalArgumentException("DropDao must not be null");
} }
this.dropDao = dropDao; this.dropRepository = dropRepository;
} }
private List<MonsterDrop> getMonsterDrops(int monsterId) { private List<MonsterDrop> getMonsterDrops(int monsterId) {
return monsterDropCache.get(monsterId, dropDao::getMonsterDrops); return monsterDropCache.get(monsterId, dropRepository::getMonsterDrops);
} }
public List<MonsterDropEntry> getMonsterDropEntries(int monsterId) { public List<MonsterDropEntry> getMonsterDropEntries(int monsterId) {
@@ -61,7 +61,7 @@ public class DropProvider {
} }
private void loadGlobalDrops() { private void loadGlobalDrops() {
this.globalMonsterDrops = dropDao.getGlobalMonsterDrops(); this.globalMonsterDrops = dropRepository.getGlobalMonsterDrops();
} }
// TODO: Temporary. MonsterDropEntry should be removed. // TODO: Temporary. MonsterDropEntry should be removed.

View File

@@ -1,16 +1,16 @@
package database.drop; package database.drop;
import database.DaoException; import database.DatabaseException;
import database.PgDatabaseConnection; import database.PgDatabaseConnection;
import org.jdbi.v3.core.Handle; import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.JdbiException; import org.jdbi.v3.core.JdbiException;
import java.util.List; import java.util.List;
public class DropDao { public class DropRepository {
private final PgDatabaseConnection connection; private final PgDatabaseConnection connection;
public DropDao(PgDatabaseConnection connection) { public DropRepository(PgDatabaseConnection connection) {
this.connection = connection; this.connection = connection;
} }
@@ -24,7 +24,7 @@ public class DropDao {
.mapTo(MonsterDrop.class) .mapTo(MonsterDrop.class)
.list(); .list();
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed to get monster drops for id %d".formatted(monsterId), e); throw new DatabaseException("Failed to get monster drops for id %d".formatted(monsterId), e);
} }
} }
@@ -36,7 +36,7 @@ public class DropDao {
.mapTo(GlobalMonsterDrop.class) .mapTo(GlobalMonsterDrop.class)
.list(); .list();
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed to get global monster drops", e); throw new DatabaseException("Failed to get global monster drops", e);
} }
} }
} }

View File

@@ -11,24 +11,24 @@ import java.util.Optional;
@ThreadSafe @ThreadSafe
public class MakerInfoProvider { public class MakerInfoProvider {
private final MakerDao makerDao; private final MakerRepository makerRepository;
private final Cache<Integer, Optional<MakerReagent>> reagentCache = Caffeine.newBuilder().build(); private final Cache<Integer, Optional<MakerReagent>> reagentCache = Caffeine.newBuilder().build();
private final Cache<Integer, Optional<MakerRecipe>> recipeCache = Caffeine.newBuilder().build(); private final Cache<Integer, Optional<MakerRecipe>> recipeCache = Caffeine.newBuilder().build();
private final Cache<Integer, List<MakerIngredient>> ingredientsCache = Caffeine.newBuilder().build(); private final Cache<Integer, List<MakerIngredient>> ingredientsCache = Caffeine.newBuilder().build();
public MakerInfoProvider(MakerDao makerDao) { public MakerInfoProvider(MakerRepository makerRepository) {
if (makerDao == null) { if (makerRepository == null) {
throw new IllegalArgumentException("MakerDao must not be null"); throw new IllegalArgumentException("MakerDao must not be null");
} }
this.makerDao = makerDao; this.makerRepository = makerRepository;
} }
public Optional<MakerReagent> getMakerReagent(int itemId) { public Optional<MakerReagent> getMakerReagent(int itemId) {
return reagentCache.get(itemId, makerDao::getReagent); return reagentCache.get(itemId, makerRepository::getReagent);
} }
public Optional<MakerRecipe> getMakerRecipe(int itemId) { public Optional<MakerRecipe> getMakerRecipe(int itemId) {
return recipeCache.get(itemId, makerDao::getRecipe); return recipeCache.get(itemId, makerRepository::getRecipe);
} }
public Optional<Integer> getStimulant(int itemId) { public Optional<Integer> getStimulant(int itemId) {
@@ -36,7 +36,7 @@ public class MakerInfoProvider {
} }
public List<MakerIngredient> getIngredients(int recipeItemId) { public List<MakerIngredient> getIngredients(int recipeItemId) {
return ingredientsCache.get(recipeItemId, makerDao::getIngredients); return ingredientsCache.get(recipeItemId, makerRepository::getIngredients);
} }
public Optional<MakerDisassemblyInfo> getDisassemblyInfo(int itemId) { public Optional<MakerDisassemblyInfo> getDisassemblyInfo(int itemId) {

View File

@@ -1,6 +1,6 @@
package database.maker; package database.maker;
import database.DaoException; import database.DatabaseException;
import database.PgDatabaseConnection; import database.PgDatabaseConnection;
import org.jdbi.v3.core.Handle; import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.JdbiException; import org.jdbi.v3.core.JdbiException;
@@ -8,10 +8,10 @@ import org.jdbi.v3.core.JdbiException;
import java.util.List; import java.util.List;
import java.util.Optional; import java.util.Optional;
public class MakerDao { public class MakerRepository {
private final PgDatabaseConnection connection; private final PgDatabaseConnection connection;
public MakerDao(PgDatabaseConnection connection) { public MakerRepository(PgDatabaseConnection connection) {
this.connection = connection; this.connection = connection;
} }
@@ -25,7 +25,7 @@ public class MakerDao {
.mapTo(MakerReagent.class) .mapTo(MakerReagent.class)
.findOne(); .findOne();
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed to get maker reagent with item id: %d".formatted(itemId), e); throw new DatabaseException("Failed to get maker reagent with item id: %d".formatted(itemId), e);
} }
} }
@@ -39,7 +39,7 @@ public class MakerDao {
.mapTo(MakerRecipe.class) .mapTo(MakerRecipe.class)
.findOne(); .findOne();
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed to get maker recipe with item id: %d".formatted(itemId), e); throw new DatabaseException("Failed to get maker recipe with item id: %d".formatted(itemId), e);
} }
} }
@@ -53,7 +53,7 @@ public class MakerDao {
.mapTo(MakerIngredient.class) .mapTo(MakerIngredient.class)
.list(); .list();
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed to get maker ingredients for recipe item id %d".formatted(recipeItemId), e); throw new DatabaseException("Failed to get maker ingredients for recipe item id %d".formatted(recipeItemId), e);
} }
} }
} }

View File

@@ -1,6 +1,6 @@
package database.monsterbook; package database.monsterbook;
import database.DaoException; import database.DatabaseException;
import database.PgDatabaseConnection; import database.PgDatabaseConnection;
import org.jdbi.v3.core.Handle; import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.JdbiException; import org.jdbi.v3.core.JdbiException;
@@ -8,10 +8,10 @@ import org.jdbi.v3.core.statement.PreparedBatch;
import java.util.List; import java.util.List;
public class MonsterCardDao { public class MonsterCardRepository {
private final PgDatabaseConnection connection; private final PgDatabaseConnection connection;
public MonsterCardDao(PgDatabaseConnection connection) { public MonsterCardRepository(PgDatabaseConnection connection) {
this.connection = connection; this.connection = connection;
} }
@@ -25,7 +25,7 @@ public class MonsterCardDao {
.mapTo(MonsterCard.class) .mapTo(MonsterCard.class)
.list(); .list();
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed to find monster cards (chrId %d)".formatted(chrId), e); throw new DatabaseException("Failed to find monster cards (chrId %d)".formatted(chrId), e);
} }
} }
@@ -44,7 +44,7 @@ public class MonsterCardDao {
}); });
batch.execute(); batch.execute();
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed to save monster cards (chrId %d)".formatted(chrId), e); throw new DatabaseException("Failed to save monster cards (chrId %d)".formatted(chrId), e);
} }
} }
} }

View File

@@ -1,6 +1,6 @@
package database.note; package database.note;
import database.DaoException; import database.DatabaseException;
import database.PgDatabaseConnection; import database.PgDatabaseConnection;
import model.Note; import model.Note;
import org.jdbi.v3.core.Handle; import org.jdbi.v3.core.Handle;
@@ -29,7 +29,7 @@ public class NoteDao {
.bind(5, 0) .bind(5, 0)
.execute(); .execute();
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed to save note: %s".formatted(note.toString()), e); throw new DatabaseException("Failed to save note: %s".formatted(note.toString()), e);
} }
} }
@@ -46,7 +46,7 @@ public class NoteDao {
.mapTo(Note.class) .mapTo(Note.class)
.list(); .list();
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed to find notes sent to: %s".formatted(receiver), e); throw new DatabaseException("Failed to find notes sent to: %s".formatted(receiver), e);
} }
} }
@@ -60,7 +60,7 @@ public class NoteDao {
return note; return note;
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed to delete note with id: %d".formatted(id), e); throw new DatabaseException("Failed to delete note with id: %d".formatted(id), e);
} }
} }
@@ -76,7 +76,7 @@ public class NoteDao {
.mapTo(Note.class) .mapTo(Note.class)
.findOne(); .findOne();
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed find note with id %s".formatted(id), e); throw new DatabaseException("Failed find note with id %s".formatted(id), e);
} }
return note; return note;
} }
@@ -90,7 +90,7 @@ public class NoteDao {
.bind(0, id) .bind(0, id)
.execute(); .execute();
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed to delete note with id %d".formatted(id), e); throw new DatabaseException("Failed to delete note with id %d".formatted(id), e);
} }
} }
} }

View File

@@ -1,6 +1,6 @@
package database.shop; package database.shop;
import database.DaoException; import database.DatabaseException;
import database.PgDatabaseConnection; import database.PgDatabaseConnection;
import org.jdbi.v3.core.Handle; import org.jdbi.v3.core.Handle;
import org.jdbi.v3.core.JdbiException; import org.jdbi.v3.core.JdbiException;
@@ -25,7 +25,7 @@ public class ShopDao {
.mapTo(Shop.class) .mapTo(Shop.class)
.findOne(); .findOne();
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed to get shop with id %d".formatted(shopId), e); throw new DatabaseException("Failed to get shop with id %d".formatted(shopId), e);
} }
} }
@@ -40,7 +40,7 @@ public class ShopDao {
.mapTo(ShopItem.class) .mapTo(ShopItem.class)
.list(); .list();
} catch (JdbiException e) { } catch (JdbiException e) {
throw new DaoException("Failed to get shop items for shop %d".formatted(shopId), e); throw new DatabaseException("Failed to get shop items for shop %d".formatted(shopId), e);
} }
} }
} }

View File

@@ -45,12 +45,12 @@ import database.PgDatabaseConfig;
import database.PgDatabaseConnection; import database.PgDatabaseConnection;
import database.character.CharacterLoader; import database.character.CharacterLoader;
import database.character.CharacterSaver; import database.character.CharacterSaver;
import database.drop.DropDao;
import database.drop.DropProvider; import database.drop.DropProvider;
import database.maker.MakerDao; import database.drop.DropRepository;
import database.maker.MakerInfoProvider; import database.maker.MakerInfoProvider;
import database.maker.MakerRepository;
import database.migration.FlywayRunner; import database.migration.FlywayRunner;
import database.monsterbook.MonsterCardDao; import database.monsterbook.MonsterCardRepository;
import database.note.NoteDao; import database.note.NoteDao;
import database.shop.ShopDao; import database.shop.ShopDao;
import net.ChannelDependencies; import net.ChannelDependencies;
@@ -1002,15 +1002,15 @@ public class Server {
} }
private ChannelDependencies registerChannelDependencies(PgDatabaseConnection connection) { private ChannelDependencies registerChannelDependencies(PgDatabaseConnection connection) {
MonsterCardDao monsterCardDao = new MonsterCardDao(connection); MonsterCardRepository monsterCardRepository = new MonsterCardRepository(connection);
CharacterLoader characterLoader = new CharacterLoader(monsterCardDao); CharacterLoader characterLoader = new CharacterLoader(monsterCardRepository);
CharacterSaver characterSaver = new CharacterSaver(monsterCardDao); CharacterSaver characterSaver = new CharacterSaver(monsterCardRepository);
TransitionService transitionService = new TransitionService(characterSaver); TransitionService transitionService = new TransitionService(characterSaver);
BanService banService = new BanService(transitionService); BanService banService = new BanService(transitionService);
NoteService noteService = new NoteService(new NoteDao(connection)); NoteService noteService = new NoteService(new NoteDao(connection));
MakerProcessor makerProcessor = new MakerProcessor(new MakerInfoProvider(new MakerDao(connection))); MakerProcessor makerProcessor = new MakerProcessor(new MakerInfoProvider(new MakerRepository(connection)));
FredrickProcessor fredrickProcessor = new FredrickProcessor(noteService); FredrickProcessor fredrickProcessor = new FredrickProcessor(noteService);
DropProvider dropProvider = new DropProvider(new DropDao(connection)); DropProvider dropProvider = new DropProvider(new DropRepository(connection));
ShopFactory shopFactory = new ShopFactory(new ShopDao(connection)); ShopFactory shopFactory = new ShopFactory(new ShopDao(connection));
CommandContext commandContext = new CommandContext(null, dropProvider, shopFactory, CommandContext commandContext = new CommandContext(null, dropProvider, shopFactory,
characterSaver, transitionService); characterSaver, transitionService);

View File

@@ -1,7 +1,7 @@
package service; package service;
import client.Character; import client.Character;
import database.DaoException; import database.DatabaseException;
import database.note.NoteDao; import database.note.NoteDao;
import model.Note; import model.Note;
import net.packet.out.ShowNotesPacket; import net.packet.out.ShowNotesPacket;
@@ -52,7 +52,7 @@ public class NoteService {
try { try {
noteDao.save(note); noteDao.save(note);
return true; return true;
} catch (DaoException e) { } catch (DatabaseException e) {
log.error("Failed to send note {}", note, e); log.error("Failed to send note {}", note, e);
return false; return false;
} }
@@ -80,7 +80,7 @@ public class NoteService {
final List<Note> notes; final List<Note> notes;
try { try {
notes = noteDao.findAllByTo(to); notes = noteDao.findAllByTo(to);
} catch (DaoException e) { } catch (DatabaseException e) {
log.error("Failed to find notes sent to chr name {}", to, e); log.error("Failed to find notes sent to chr name {}", to, e);
return Collections.emptyList(); return Collections.emptyList();
} }
@@ -101,7 +101,7 @@ public class NoteService {
public Optional<Note> delete(int noteId) { public Optional<Note> delete(int noteId) {
try { try {
return noteDao.delete(noteId); return noteDao.delete(noteId);
} catch (DaoException e) { } catch (DatabaseException e) {
log.error("Failed to discard note with id {}", noteId, e); log.error("Failed to discard note with id {}", noteId, e);
return Optional.empty(); return Optional.empty();
} }

View File

@@ -21,19 +21,19 @@ import static org.mockito.Mockito.when;
class DropProviderTest { class DropProviderTest {
@Mock @Mock
private DropDao dropDao; private DropRepository dropRepository;
private DropProvider dropProvider; private DropProvider dropProvider;
@BeforeEach @BeforeEach
void reset() { void reset() {
MockitoAnnotations.openMocks(this); MockitoAnnotations.openMocks(this);
this.dropProvider = new DropProvider(dropDao); this.dropProvider = new DropProvider(dropRepository);
} }
@Test @Test
void getMonsterDropEntries_noDrops() { void getMonsterDropEntries_noDrops() {
when(dropDao.getMonsterDrops(anyInt())).thenReturn(Collections.emptyList()); when(dropRepository.getMonsterDrops(anyInt())).thenReturn(Collections.emptyList());
List<MonsterDropEntry> dropEntries = dropProvider.getMonsterDropEntries(489340); List<MonsterDropEntry> dropEntries = dropProvider.getMonsterDropEntries(489340);
@@ -43,7 +43,7 @@ class DropProviderTest {
@Test @Test
void getMonsterDropEntries() { void getMonsterDropEntries() {
MonsterDrop snailShellDrop = snailShellDrop(); MonsterDrop snailShellDrop = snailShellDrop();
when(dropDao.getMonsterDrops(anyInt())).thenReturn(List.of(snailShellDrop)); when(dropRepository.getMonsterDrops(anyInt())).thenReturn(List.of(snailShellDrop));
List<MonsterDropEntry> dropEntries = dropProvider.getMonsterDropEntries(100100); List<MonsterDropEntry> dropEntries = dropProvider.getMonsterDropEntries(100100);
@@ -58,7 +58,7 @@ class DropProviderTest {
@Test @Test
void getCachedMonsterDropEntries() { void getCachedMonsterDropEntries() {
when(dropDao.getMonsterDrops(anyInt())).thenReturn(List.of(snailShellDrop())); when(dropRepository.getMonsterDrops(anyInt())).thenReturn(List.of(snailShellDrop()));
int monsterId = 100100; int monsterId = 100100;
List<MonsterDropEntry> dropEntries1 = dropProvider.getMonsterDropEntries(monsterId); List<MonsterDropEntry> dropEntries1 = dropProvider.getMonsterDropEntries(monsterId);
@@ -73,7 +73,7 @@ class DropProviderTest {
assertEquals(dropEntry1.Maximum, dropEntry2.Maximum); assertEquals(dropEntry1.Maximum, dropEntry2.Maximum);
assertEquals(dropEntry1.questid, dropEntry2.questid); assertEquals(dropEntry1.questid, dropEntry2.questid);
assertEquals(dropEntry1.chance, dropEntry2.chance); assertEquals(dropEntry1.chance, dropEntry2.chance);
verify(dropDao, times(1)).getMonsterDrops(anyInt()); verify(dropRepository, times(1)).getMonsterDrops(anyInt());
} }
private MonsterDrop snailShellDrop() { private MonsterDrop snailShellDrop() {
@@ -83,7 +83,7 @@ class DropProviderTest {
@Test @Test
void getCachedGlobalDropEntries() { void getCachedGlobalDropEntries() {
GlobalMonsterDrop globalDrop = new GlobalMonsterDrop(2049100, -1, 2, 3, null, 450); GlobalMonsterDrop globalDrop = new GlobalMonsterDrop(2049100, -1, 2, 3, null, 450);
when(dropDao.getGlobalMonsterDrops()).thenReturn(List.of(globalDrop)); when(dropRepository.getGlobalMonsterDrops()).thenReturn(List.of(globalDrop));
List<MonsterGlobalDropEntry> dropEntries1 = dropProvider.getRelevantGlobalDrops(AnyValues.integer()); List<MonsterGlobalDropEntry> dropEntries1 = dropProvider.getRelevantGlobalDrops(AnyValues.integer());
List<MonsterGlobalDropEntry> dropEntries2 = dropProvider.getRelevantGlobalDrops(AnyValues.integer()); List<MonsterGlobalDropEntry> dropEntries2 = dropProvider.getRelevantGlobalDrops(AnyValues.integer());
@@ -104,13 +104,13 @@ class DropProviderTest {
assertEquals(dropEntry1.questid, dropEntry2.questid); assertEquals(dropEntry1.questid, dropEntry2.questid);
assertEquals(450, dropEntry1.chance); assertEquals(450, dropEntry1.chance);
assertEquals(dropEntry1.chance, dropEntry2.chance); assertEquals(dropEntry1.chance, dropEntry2.chance);
verify(dropDao, times(1)).getGlobalMonsterDrops(); verify(dropRepository, times(1)).getGlobalMonsterDrops();
} }
@Test @Test
void getRelevantGlobalDrop() { void getRelevantGlobalDrop() {
GlobalMonsterDrop ossyriaDrop = new GlobalMonsterDrop(AnyValues.integer(), 2, AnyValues.integer(), AnyValues.integer(), AnyValues.integer(), AnyValues.integer()); GlobalMonsterDrop ossyriaDrop = new GlobalMonsterDrop(AnyValues.integer(), 2, AnyValues.integer(), AnyValues.integer(), AnyValues.integer(), AnyValues.integer());
when(dropDao.getGlobalMonsterDrops()).thenReturn(List.of(ossyriaDrop)); when(dropRepository.getGlobalMonsterDrops()).thenReturn(List.of(ossyriaDrop));
int ossyriaMapId = 200_000_200; int ossyriaMapId = 200_000_200;
List<MonsterGlobalDropEntry> dropEntries = dropProvider.getRelevantGlobalDrops(ossyriaMapId); List<MonsterGlobalDropEntry> dropEntries = dropProvider.getRelevantGlobalDrops(ossyriaMapId);
@@ -121,7 +121,7 @@ class DropProviderTest {
@Test @Test
void getRelevantGlobalDrop_wrongContinent() { void getRelevantGlobalDrop_wrongContinent() {
GlobalMonsterDrop ellinDrop = new GlobalMonsterDrop(AnyValues.integer(), 3, AnyValues.integer(), AnyValues.integer(), AnyValues.integer(), AnyValues.integer()); GlobalMonsterDrop ellinDrop = new GlobalMonsterDrop(AnyValues.integer(), 3, AnyValues.integer(), AnyValues.integer(), AnyValues.integer(), AnyValues.integer());
when(dropDao.getGlobalMonsterDrops()).thenReturn(List.of(ellinDrop)); when(dropRepository.getGlobalMonsterDrops()).thenReturn(List.of(ellinDrop));
int victoriaMapId = 102_000_000; int victoriaMapId = 102_000_000;
List<MonsterGlobalDropEntry> dropEntries = dropProvider.getRelevantGlobalDrops(victoriaMapId); List<MonsterGlobalDropEntry> dropEntries = dropProvider.getRelevantGlobalDrops(victoriaMapId);
@@ -132,28 +132,28 @@ class DropProviderTest {
@Test @Test
void clearCaches_shouldClearMonsterDrops() { void clearCaches_shouldClearMonsterDrops() {
MonsterDrop drop = snailShellDrop(); MonsterDrop drop = snailShellDrop();
when(dropDao.getMonsterDrops(anyInt())).thenReturn(List.of(drop)); when(dropRepository.getMonsterDrops(anyInt())).thenReturn(List.of(drop));
int monsterId = 100100; int monsterId = 100100;
List<MonsterDropEntry> drops1 = dropProvider.getMonsterDropEntries(monsterId); List<MonsterDropEntry> drops1 = dropProvider.getMonsterDropEntries(monsterId);
dropProvider.clearCaches(); dropProvider.clearCaches();
List<MonsterDropEntry> drops2 = dropProvider.getMonsterDropEntries(monsterId); List<MonsterDropEntry> drops2 = dropProvider.getMonsterDropEntries(monsterId);
verify(dropDao, times(2)).getMonsterDrops(anyInt()); verify(dropRepository, times(2)).getMonsterDrops(anyInt());
assertEquals(1, drops1.size()); assertEquals(1, drops1.size());
assertEquals(1, drops2.size()); assertEquals(1, drops2.size());
} }
@Test @Test
void clearCaches_shouldClearGlobalDrops() { void clearCaches_shouldClearGlobalDrops() {
when(dropDao.getGlobalMonsterDrops()).thenReturn(List.of(globalDrop())); when(dropRepository.getGlobalMonsterDrops()).thenReturn(List.of(globalDrop()));
int mapId = 100_000_123; int mapId = 100_000_123;
List<MonsterGlobalDropEntry> drops1 = dropProvider.getRelevantGlobalDrops(mapId); List<MonsterGlobalDropEntry> drops1 = dropProvider.getRelevantGlobalDrops(mapId);
dropProvider.clearCaches(); dropProvider.clearCaches();
List<MonsterGlobalDropEntry> drops2 = dropProvider.getRelevantGlobalDrops(mapId); List<MonsterGlobalDropEntry> drops2 = dropProvider.getRelevantGlobalDrops(mapId);
verify(dropDao, times(2)).getGlobalMonsterDrops(); verify(dropRepository, times(2)).getGlobalMonsterDrops();
assertEquals(1, drops1.size()); assertEquals(1, drops1.size());
assertEquals(1, drops2.size()); assertEquals(1, drops2.size());
} }

View File

@@ -22,14 +22,14 @@ import static org.mockito.Mockito.when;
class MakerInfoProviderTest { class MakerInfoProviderTest {
@Mock @Mock
private MakerDao makerDao; private MakerRepository makerRepository;
private MakerInfoProvider makerInfoProvider; private MakerInfoProvider makerInfoProvider;
@BeforeEach @BeforeEach
void reset() { void reset() {
MockitoAnnotations.openMocks(this); MockitoAnnotations.openMocks(this);
this.makerInfoProvider = new MakerInfoProvider(makerDao); this.makerInfoProvider = new MakerInfoProvider(makerRepository);
} }
@Test @Test
@@ -41,7 +41,7 @@ class MakerInfoProviderTest {
void getReagentFromDb() { void getReagentFromDb() {
int itemId = 5783; int itemId = 5783;
MakerReagent reagent = createReagent(itemId); MakerReagent reagent = createReagent(itemId);
when(makerDao.getReagent(anyInt())).thenReturn(Optional.of(reagent)); when(makerRepository.getReagent(anyInt())).thenReturn(Optional.of(reagent));
Optional<MakerReagent> retrievedReagent = makerInfoProvider.getMakerReagent(itemId); Optional<MakerReagent> retrievedReagent = makerInfoProvider.getMakerReagent(itemId);
@@ -53,7 +53,7 @@ class MakerInfoProviderTest {
void getCachedReagent() { void getCachedReagent() {
int itemId = 90123444; int itemId = 90123444;
MakerReagent reagent = createReagent(itemId); MakerReagent reagent = createReagent(itemId);
when(makerDao.getReagent(anyInt())).thenReturn(Optional.of(reagent)); when(makerRepository.getReagent(anyInt())).thenReturn(Optional.of(reagent));
Optional<MakerReagent> firstReagent = makerInfoProvider.getMakerReagent(itemId); Optional<MakerReagent> firstReagent = makerInfoProvider.getMakerReagent(itemId);
Optional<MakerReagent> secondReagent = makerInfoProvider.getMakerReagent(itemId); Optional<MakerReagent> secondReagent = makerInfoProvider.getMakerReagent(itemId);
@@ -62,7 +62,7 @@ class MakerInfoProviderTest {
assertEquals(reagent, firstReagent.get()); assertEquals(reagent, firstReagent.get());
assertTrue(secondReagent.isPresent()); assertTrue(secondReagent.isPresent());
assertEquals(reagent, secondReagent.get()); assertEquals(reagent, secondReagent.get());
verify(makerDao, times(1)).getReagent(itemId); verify(makerRepository, times(1)).getReagent(itemId);
} }
private MakerReagent createReagent(int itemId) { private MakerReagent createReagent(int itemId) {
@@ -73,7 +73,7 @@ class MakerInfoProviderTest {
void getRecipeFromDb() { void getRecipeFromDb() {
int itemId = 43893; int itemId = 43893;
MakerRecipe recipe = createRecipe(itemId); MakerRecipe recipe = createRecipe(itemId);
when(makerDao.getRecipe(itemId)).thenReturn(Optional.of(recipe)); when(makerRepository.getRecipe(itemId)).thenReturn(Optional.of(recipe));
Optional<MakerRecipe> retrievedRecipe = makerInfoProvider.getMakerRecipe(itemId); Optional<MakerRecipe> retrievedRecipe = makerInfoProvider.getMakerRecipe(itemId);
@@ -94,7 +94,7 @@ class MakerInfoProviderTest {
void getCachedRecipe() { void getCachedRecipe() {
int itemId = 10848; int itemId = 10848;
MakerRecipe recipe = createRecipe(itemId); MakerRecipe recipe = createRecipe(itemId);
when(makerDao.getRecipe(anyInt())).thenReturn(Optional.of(recipe)); when(makerRepository.getRecipe(anyInt())).thenReturn(Optional.of(recipe));
Optional<MakerRecipe> firstRecipe = makerInfoProvider.getMakerRecipe(itemId); Optional<MakerRecipe> firstRecipe = makerInfoProvider.getMakerRecipe(itemId);
Optional<MakerRecipe> secondRecipe = makerInfoProvider.getMakerRecipe(itemId); Optional<MakerRecipe> secondRecipe = makerInfoProvider.getMakerRecipe(itemId);
@@ -103,7 +103,7 @@ class MakerInfoProviderTest {
assertEquals(recipe, firstRecipe.get()); assertEquals(recipe, firstRecipe.get());
assertTrue(secondRecipe.isPresent()); assertTrue(secondRecipe.isPresent());
assertEquals(recipe, secondRecipe.get()); assertEquals(recipe, secondRecipe.get());
verify(makerDao, times(1)).getRecipe(itemId); verify(makerRepository, times(1)).getRecipe(itemId);
} }
private MakerRecipe createRecipe(int itemId) { private MakerRecipe createRecipe(int itemId) {
@@ -114,7 +114,7 @@ class MakerInfoProviderTest {
void getStimulant() { void getStimulant() {
int catalyst = 4031200; int catalyst = 4031200;
MakerRecipe recipeWithCatalyst = new MakerRecipe(0, (short) 0, (short) 0, (short) 0, 0, null, null, catalyst, (short) 0, (short) 0); MakerRecipe recipeWithCatalyst = new MakerRecipe(0, (short) 0, (short) 0, (short) 0, 0, null, null, catalyst, (short) 0, (short) 0);
when(makerDao.getRecipe(anyInt())).thenReturn(Optional.of(recipeWithCatalyst)); when(makerRepository.getRecipe(anyInt())).thenReturn(Optional.of(recipeWithCatalyst));
Optional<Integer> stimulant = makerInfoProvider.getStimulant(AnyValues.integer()); Optional<Integer> stimulant = makerInfoProvider.getStimulant(AnyValues.integer());
@@ -144,9 +144,9 @@ class MakerInfoProviderTest {
void getMakerItemCreateEntry() { void getMakerItemCreateEntry() {
final int itemId = 458945; final int itemId = 458945;
MakerRecipe recipe = createRecipe(itemId); MakerRecipe recipe = createRecipe(itemId);
when(makerDao.getRecipe(anyInt())).thenReturn(Optional.of(recipe)); when(makerRepository.getRecipe(anyInt())).thenReturn(Optional.of(recipe));
MakerIngredient ingredient = new MakerIngredient(1002003, (short) 5); MakerIngredient ingredient = new MakerIngredient(1002003, (short) 5);
when(makerDao.getIngredients(anyInt())).thenReturn(List.of(ingredient)); when(makerRepository.getIngredients(anyInt())).thenReturn(List.of(ingredient));
Optional<MakerItemFactory.MakerItemCreateEntry> optionalCreateEntry = makerInfoProvider.getMakerItemEntry(itemId); Optional<MakerItemFactory.MakerItemCreateEntry> optionalCreateEntry = makerInfoProvider.getMakerItemEntry(itemId);
@@ -166,6 +166,6 @@ class MakerInfoProviderTest {
} }
private void givenNoRecipe() { private void givenNoRecipe() {
when(makerDao.getRecipe(anyInt())).thenReturn(Optional.empty()); when(makerRepository.getRecipe(anyInt())).thenReturn(Optional.empty());
} }
} }

View File

@@ -25,7 +25,7 @@ import static org.mockito.Mockito.doThrow;
import static org.mockito.Mockito.never; import static org.mockito.Mockito.never;
import static org.mockito.Mockito.verify; import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when; import static org.mockito.Mockito.when;
import static testutil.AnyValues.daoException; import static testutil.AnyValues.dbException;
class NoteServiceTest { class NoteServiceTest {
@@ -78,7 +78,7 @@ class NoteServiceTest {
@Test @Test
void sendFailure() { void sendFailure() {
doThrow(daoException()).when(noteDao).save(any()); doThrow(dbException()).when(noteDao).save(any());
boolean success = noteService.sendNormal(AnyValues.string(), AnyValues.string(), AnyValues.string()); boolean success = noteService.sendNormal(AnyValues.string(), AnyValues.string(), AnyValues.string());
@@ -119,7 +119,7 @@ class NoteServiceTest {
@Test @Test
void showNotesFailure_shouldNotSendPacket() { void showNotesFailure_shouldNotSendPacket() {
var chr = Mocks.chr("mockChr"); var chr = Mocks.chr("mockChr");
when(noteDao.findAllByTo(any())).thenThrow(daoException()); when(noteDao.findAllByTo(any())).thenThrow(dbException());
noteService.show(chr); noteService.show(chr);
@@ -140,7 +140,7 @@ class NoteServiceTest {
@Test @Test
void deleteNoteFailure() { void deleteNoteFailure() {
when(noteDao.delete(anyInt())).thenThrow(daoException()); when(noteDao.delete(anyInt())).thenThrow(dbException());
Optional<Note> deletedNote = noteService.delete(4382); Optional<Note> deletedNote = noteService.delete(4382);

View File

@@ -1,6 +1,6 @@
package testutil; package testutil;
import database.DaoException; import database.DatabaseException;
public class AnyValues { public class AnyValues {
@@ -16,7 +16,7 @@ public class AnyValues {
return 4; return 4;
} }
public static DaoException daoException() { public static DatabaseException dbException() {
return new DaoException(string(), new RuntimeException()); return new DatabaseException(string(), new RuntimeException());
} }
} }