Skip to content

Commit

Permalink
Added tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
tastybento committed Dec 2, 2023
1 parent 68fadef commit b842531
Show file tree
Hide file tree
Showing 2 changed files with 69 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Objects;
import java.util.Optional;
Expand Down Expand Up @@ -203,36 +202,48 @@ private boolean loadGeneratorData(GeneratorDataObject generatorData) {
* This method allows to store single generatorTier object.
*
* @param generatorTier object that must be saved in database.
* @return CompletableFuture<Boolean> to indicate if it is done
*/
public void saveGeneratorTier(GeneratorTierObject generatorTier) {
this.generatorTierDatabase.saveObjectAsync(generatorTier);
public CompletableFuture<Boolean> saveGeneratorTier(GeneratorTierObject generatorTier) {
return this.generatorTierDatabase.saveObjectAsync(generatorTier);
}

/**
* This method allows to store single generatorBundle object.
*
* @param generatorBundle object that must be saved in database.
* @return CompletableFuture<Boolean> to indicate if it is done
*/
public void saveGeneratorBundle(GeneratorBundleObject generatorBundle) {
this.generatorBundleDatabase.saveObjectAsync(generatorBundle);
public CompletableFuture<Boolean> saveGeneratorBundle(GeneratorBundleObject generatorBundle) {
return this.generatorBundleDatabase.saveObjectAsync(generatorBundle);
}

/**
* This method allows to store single generatorData object.
*
* @param generatorData object that must be saved in database.
* @return CompletableFuture<Boolean> to indicate if it is done
*/
public void saveGeneratorData(GeneratorDataObject generatorData) {
this.generatorDataDatabase.saveObjectAsync(generatorData);
public CompletableFuture<Boolean> saveGeneratorData(GeneratorDataObject generatorData) {
return this.generatorDataDatabase.saveObjectAsync(generatorData);
}

/**
* Save generator tiers from cache into database
*
* @return CompletableFuture<Boolean> to indicate if it is done
*/
public void save() {
this.generatorTierCache.values().forEach(this::saveGeneratorTier);
this.generatorBundleCache.values().forEach(this::saveGeneratorBundle);
this.generatorDataCache.values().forEach(this::saveGeneratorData);
public CompletableFuture<Boolean> save() {
List<CompletableFuture<Boolean>> futures = this.generatorTierCache.values().stream()
.map(this::saveGeneratorTier).collect(Collectors.toList());
futures.addAll(this.generatorBundleCache.values().stream().map(this::saveGeneratorBundle)
.collect(Collectors.toList()));
futures.addAll(
this.generatorDataCache.values().stream().map(this::saveGeneratorData).collect(Collectors.toList()));

return CompletableFuture.allOf(futures.toArray(new CompletableFuture[0])).thenApply(v ->
// Return true if all futures completed successfully
futures.stream().allMatch(CompletableFuture::join));
}

/**
Expand All @@ -242,7 +253,7 @@ public void save() {
* @param gameMode GameMode addon which generators must be removed.
*/
public void wipeGameModeGenerators(GameModeAddon gameMode) {
final String objectKey = gameMode.getDescription().getName().toLowerCase(Locale.ENGLISH);
final String objectKey = gameMode.getDescription().getName().toLowerCase();

// Collect all generators
List<String> keySet = new ArrayList<>(this.generatorTierCache.keySet());
Expand Down Expand Up @@ -842,7 +853,6 @@ public void checkGeneratorUnlockStatus(Island island, @Nullable User user, @Null
// If level is null, check value from addon.
final long islandLevel = level == null ? this.getIslandLevel(island) : level;
final User owner = island.isSpawn() ? null : User.getInstance(island.getOwner());

this.getIslandGeneratorTiers(island.getWorld(), dataObject).stream().
// Filter out default generators. They are always unlocked and active.
filter(generator -> !generator.isDefaultGenerator()).
Expand Down Expand Up @@ -914,7 +924,6 @@ public void unlockGenerator(@NotNull GeneratorDataObject dataObject, @Nullable U
}
return;
}

// Create and call bukkit event to check if unlocking should be cancelled.
GeneratorUnlockEvent event = new GeneratorUnlockEvent(generator, user, island);
Bukkit.getPluginManager().callEvent(event);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
import static org.mockito.ArgumentMatchers.anyString;
import static org.mockito.Mockito.atLeast;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

Expand All @@ -19,6 +20,7 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.util.Comparator;
import java.util.Optional;
import java.util.UUID;
import java.util.concurrent.CompletableFuture;
import java.util.logging.Logger;
Expand All @@ -27,6 +29,7 @@
import org.bukkit.Location;
import org.bukkit.World;
import org.bukkit.entity.Player;
import org.bukkit.plugin.PluginManager;
import org.jetbrains.annotations.Nullable;
import org.junit.After;
import org.junit.Before;
Expand Down Expand Up @@ -98,6 +101,8 @@ public class StoneGeneratorManagerTest {
private IslandsManager im;
@Mock
private IslandWorldManager iwm;
@Mock
private PluginManager pim;

@SuppressWarnings("unchecked")
@BeforeClass
Expand Down Expand Up @@ -179,10 +184,16 @@ public void setUp() throws Exception {
when(addon.getDescription()).thenReturn(desc);
when(gameModeAddon.getDescription()).thenReturn(desc);

// Island
when(island.getOwner()).thenReturn(uuid);

// Island manager
when(im.getIsland(world, uuid)).thenReturn(island);
when(addon.getIslands()).thenReturn(im);

// IWM
when(iwm.getAddon(world)).thenReturn(Optional.of(gameModeAddon));
when(iwm.getPermissionPrefix(any())).thenReturn("bskyblock.");
when(plugin.getIWM()).thenReturn(iwm);

// Location
Expand All @@ -192,6 +203,11 @@ public void setUp() throws Exception {
RanksManager rm = new RanksManager();
when(plugin.getRanksManager()).thenReturn(rm);

// Bukkit
PowerMockito.mockStatic(Bukkit.class, Mockito.RETURNS_MOCKS);
when(Bukkit.getPlayer(uuid)).thenReturn(p);
when(Bukkit.getPluginManager()).thenReturn(pim);

sgm = new StoneGeneratorManager(addon);

// Addon Manager
Expand Down Expand Up @@ -350,7 +366,8 @@ public void testLoadGeneratorBundleOverwritePass() {
*/
@Test
public void testSaveGeneratorTier() {
sgm.saveGeneratorTier(generatorTier);
CompletableFuture<Boolean> cf = sgm.saveGeneratorTier(generatorTier);
assertTrue(cf.isDone());
}

/**
Expand All @@ -359,7 +376,8 @@ public void testSaveGeneratorTier() {
*/
@Test
public void testSaveGeneratorBundle() {
sgm.saveGeneratorBundle(generatorBundle);
CompletableFuture<Boolean> cf = sgm.saveGeneratorBundle(generatorBundle);
assertTrue(cf.isDone());
}

/**
Expand All @@ -368,7 +386,8 @@ public void testSaveGeneratorBundle() {
*/
@Test
public void testSaveGeneratorData() {
sgm.saveGeneratorData(generatorData);
CompletableFuture<Boolean> cf = sgm.saveGeneratorData(generatorData);
assertTrue(cf.isDone());
}

/**
Expand All @@ -377,7 +396,8 @@ public void testSaveGeneratorData() {
*/
@Test
public void testSave() {
sgm.save();
CompletableFuture<Boolean> cf = sgm.save();
assertTrue(cf.isDone());
}

/**
Expand All @@ -387,6 +407,8 @@ public void testSave() {
@Test
public void testWipeGameModeGenerators() {
sgm.wipeGameModeGenerators(gameModeAddon);
verify(addon).log("All generators for magiccobblegenerator are removed!");
verify(addon).log("All bundles for magiccobblegenerator are removed!");
}

/**
Expand All @@ -396,6 +418,7 @@ public void testWipeGameModeGenerators() {
@Test
public void testWipeIslandData() {
sgm.wipeIslandData(gameModeAddon);
verify(addon).log("All island data for MagicCobbleGenerator are removed!");
}

/**
Expand All @@ -405,6 +428,7 @@ public void testWipeIslandData() {
@Test
public void testWipeGeneratorTier() {
sgm.wipeGeneratorTier(generatorTier);
// Does not seem testable...
}

/**
Expand Down Expand Up @@ -485,7 +509,9 @@ public void testGetBundleById() {
*/
@Test
public void testWipeBundle() {
sgm.loadGeneratorBundle(generatorBundle, false, user);
sgm.wipeBundle(generatorBundle);
verify(h).deleteID(uuid.toString());
}

/**
Expand All @@ -494,7 +520,9 @@ public void testWipeBundle() {
*/
@Test
public void testLoadUserIslands() {
sgm.addWorld(world);
sgm.loadUserIslands(uuid);
verify(island, times(18)).getOwner();
}

/**
Expand All @@ -503,7 +531,10 @@ public void testLoadUserIslands() {
*/
@Test
public void testValidateIslandData() {
assertNull(sgm.validateIslandData(island));
sgm.addWorld(world);
@Nullable
GeneratorDataObject gdo = sgm.validateIslandData(island);
assertNotNull(gdo);
}

/**
Expand All @@ -513,6 +544,7 @@ public void testValidateIslandData() {
@Test
public void testCheckGeneratorUnlockStatus() {
sgm.checkGeneratorUnlockStatus(island, user, 10L);
verify(island, times(2)).isSpawn();
}

/**
Expand Down Expand Up @@ -540,6 +572,8 @@ public void testGetGeneratorDataUserWorld() {
@Test
public void testUnlockGenerator() {
sgm.unlockGenerator(generatorData, user, island, generatorTier);
verify(user).sendMessage(
"stone-generator.conversations.prefixstone-generator.messages.generator-cannot-be-unlocked");
}

/**
Expand All @@ -548,7 +582,7 @@ public void testUnlockGenerator() {
*/
@Test
public void testDeactivateGenerator() {
sgm.deactivateGenerator(user, generatorData, generatorTier);
assertFalse(sgm.deactivateGenerator(user, generatorData, generatorTier));
}

/**
Expand All @@ -567,6 +601,7 @@ public void testCanActivateGenerator() {
@Test
public void testActivateGeneratorUserIslandGeneratorDataObjectGeneratorTierObject() {
sgm.activateGenerator(user, island, generatorData, generatorTier);
// TODO: add a test
}

/**
Expand All @@ -575,16 +610,8 @@ public void testActivateGeneratorUserIslandGeneratorDataObjectGeneratorTierObjec
*/
@Test
public void testActivateGeneratorUserIslandGeneratorDataObjectGeneratorTierObjectBoolean() {
sgm.activateGenerator(user, island, generatorData, generatorTier, false);
}

/**
* Test method for
* {@link world.bentobox.magiccobblestonegenerator.managers.StoneGeneratorManager#activateGenerator(world.bentobox.bentobox.api.user.User, world.bentobox.bentobox.database.objects.Island, world.bentobox.magiccobblestonegenerator.database.objects.GeneratorDataObject, world.bentobox.magiccobblestonegenerator.database.objects.GeneratorTierObject, boolean)}.
*/
@Test
public void testActivateGeneratorUserIslandGeneratorDataObjectGeneratorTierObjectBooleanBypass() {
sgm.activateGenerator(user, island, generatorData, generatorTier, true);
// TODO: add a test
}

/**
Expand All @@ -603,6 +630,7 @@ public void testCanPurchaseGenerator() {
@Test
public void testPurchaseGeneratorUserIslandGeneratorDataObjectGeneratorTierObject() {
sgm.purchaseGenerator(user, island, generatorData, generatorTier);
// TODO: add a test
}

/**
Expand All @@ -611,16 +639,8 @@ public void testPurchaseGeneratorUserIslandGeneratorDataObjectGeneratorTierObjec
*/
@Test
public void testPurchaseGeneratorUserIslandGeneratorDataObjectGeneratorTierObjectBoolean() {
sgm.purchaseGenerator(user, island, generatorData, generatorTier, false);
}

/**
* Test method for
* {@link world.bentobox.magiccobblestonegenerator.managers.StoneGeneratorManager#purchaseGenerator(world.bentobox.bentobox.api.user.User, world.bentobox.bentobox.database.objects.Island, world.bentobox.magiccobblestonegenerator.database.objects.GeneratorDataObject, world.bentobox.magiccobblestonegenerator.database.objects.GeneratorTierObject, boolean)}.
*/
@Test
public void testPurchaseGeneratorUserIslandGeneratorDataObjectGeneratorTierObjectBooleanBypass() {
sgm.purchaseGenerator(user, island, generatorData, generatorTier, true);
// TODO: add a test
}

/**
Expand All @@ -630,6 +650,7 @@ public void testPurchaseGeneratorUserIslandGeneratorDataObjectGeneratorTierObjec
@Test
public void testWipeGeneratorDataString() {
sgm.wipeGeneratorData(uuid.toString());
// TODO: add a test
}

/**
Expand All @@ -639,6 +660,7 @@ public void testWipeGeneratorDataString() {
@Test
public void testWipeGeneratorDataGeneratorDataObject() {
sgm.wipeGeneratorData(generatorData);
// TODO: add a test
}

/**
Expand Down

0 comments on commit b842531

Please sign in to comment.