Add tests for MakerInfoProvider

These are going to be handy soon,
as I'm about to add Caffeine for caching.
This commit is contained in:
P0nk
2023-03-04 20:50:29 +01:00
parent 6073b20d65
commit f2ca67aba4
3 changed files with 132 additions and 0 deletions

View File

@@ -13,6 +13,9 @@ public class MakerInfoProvider {
private final Map<Integer, MakerRecipe> recipeCache = new ConcurrentHashMap<>();
public MakerInfoProvider(MakerDao makerDao) {
if (makerDao == null) {
throw new IllegalArgumentException("MakerDao is null");
}
this.makerDao = makerDao;
}

View File

@@ -0,0 +1,125 @@
package database.maker;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import testutil.AnyValues;
import java.util.Optional;
import static org.junit.jupiter.api.Assertions.*;
import static org.mockito.Mockito.*;
class MakerInfoProviderTest {
@Mock
private MakerDao makerDao;
private MakerInfoProvider makerInfoProvider;
@BeforeEach
void reset() {
MockitoAnnotations.openMocks(this);
this.makerInfoProvider = new MakerInfoProvider(makerDao);
}
@Test
void requireNonNullDao() {
assertThrows(IllegalArgumentException.class, () -> new MakerInfoProvider(null));
}
@Test
void getReagentFromDb() {
int itemId = 5783;
MakerReagent reagent = createReagent(itemId);
when(makerDao.getReagent(anyInt())).thenReturn(Optional.of(reagent));
Optional<MakerReagent> retrievedReagent = makerInfoProvider.getMakerReagent(itemId);
assertTrue(retrievedReagent.isPresent());
assertEquals(reagent, retrievedReagent.get());
}
@Test
void getCachedReagent() {
int itemId = 90123444;
MakerReagent reagent = createReagent(itemId);
when(makerDao.getReagent(anyInt())).thenReturn(Optional.of(reagent));
Optional<MakerReagent> firstReagent = makerInfoProvider.getMakerReagent(itemId);
Optional<MakerReagent> secondReagent = makerInfoProvider.getMakerReagent(itemId);
assertTrue(firstReagent.isPresent());
assertEquals(reagent, firstReagent.get());
assertTrue(secondReagent.isPresent());
assertEquals(reagent, secondReagent.get());
verify(makerDao, times(1)).getReagent(itemId);
}
private MakerReagent createReagent(int itemId) {
return new MakerReagent(itemId, "incPAD", 3);
}
@Test
void getRecipeFromDb() {
int itemId = 43893;
MakerRecipe recipe = createRecipe(itemId);
when(makerDao.getRecipe(itemId)).thenReturn(Optional.of(recipe));
Optional<MakerRecipe> retrievedRecipe = makerInfoProvider.getMakerRecipe(itemId);
assertTrue(retrievedRecipe.isPresent());
assertEquals(recipe, retrievedRecipe.get());
}
@Test
void getRecipeFromDb_notFound() {
when(makerDao.getRecipe(anyInt())).thenReturn(Optional.empty());
Optional<MakerRecipe> recipe = makerInfoProvider.getMakerRecipe(AnyValues.integer());
assertTrue(recipe.isEmpty());
}
@Test
void getCachedRecipe() {
int itemId = 10848;
MakerRecipe recipe = createRecipe(itemId);
when(makerDao.getRecipe(anyInt())).thenReturn(Optional.of(recipe));
Optional<MakerRecipe> firstRecipe = makerInfoProvider.getMakerRecipe(itemId);
Optional<MakerRecipe> secondRecipe = makerInfoProvider.getMakerRecipe(itemId);
assertTrue(firstRecipe.isPresent());
assertEquals(recipe, firstRecipe.get());
assertTrue(secondRecipe.isPresent());
assertEquals(recipe, secondRecipe.get());
verify(makerDao, times(1)).getRecipe(itemId);
}
private MakerRecipe createRecipe(int itemId) {
return new MakerRecipe(itemId, (short) 0, (short) 45, (short) 1, 100_000, null, null, null, (short) 1, (short) 1);
}
@Test
void getStimulant() {
int catalyst = 4031200;
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));
Optional<Integer> stimulant = makerInfoProvider.getStimulant(AnyValues.integer());
assertTrue(stimulant.isPresent());
assertEquals(catalyst, stimulant.get());
}
@Test
void getStimulant_noRecipe() {
when(makerDao.getRecipe(anyInt())).thenReturn(Optional.empty());
Optional<Integer> stimulant = makerInfoProvider.getStimulant(AnyValues.integer());
assertTrue(stimulant.isEmpty());
}
}

View File

@@ -8,6 +8,10 @@ public class AnyValues {
return "string";
}
public static int integer() {
return 17;
}
public static DaoException daoException() {
return new DaoException(string(), new RuntimeException());
}