From 8bd653a1b520be84c100f42538454d553057515f Mon Sep 17 00:00:00 2001 From: TheBusyBiscuit Date: Fri, 12 Jun 2020 18:55:22 +0200 Subject: [PATCH] Added Unit Tests for the Cooler Listener --- .../tests/listeners/TestCoolerListener.java | 83 +++++++++++++++++++ 1 file changed, 83 insertions(+) create mode 100644 src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/listeners/TestCoolerListener.java diff --git a/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/listeners/TestCoolerListener.java b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/listeners/TestCoolerListener.java new file mode 100644 index 000000000..3c608704b --- /dev/null +++ b/src/test/java/io/github/thebusybiscuit/slimefun4/testing/tests/listeners/TestCoolerListener.java @@ -0,0 +1,83 @@ +package io.github.thebusybiscuit.slimefun4.testing.tests.listeners; + +import org.bukkit.Color; +import org.bukkit.Material; +import org.bukkit.NamespacedKey; +import org.bukkit.entity.Player; +import org.bukkit.event.entity.FoodLevelChangeEvent; +import org.bukkit.inventory.ItemStack; +import org.bukkit.potion.PotionEffect; +import org.bukkit.potion.PotionEffectType; +import org.junit.jupiter.api.AfterAll; +import org.junit.jupiter.api.Assertions; +import org.junit.jupiter.api.BeforeAll; +import org.junit.jupiter.api.Test; + +import be.seeseemelk.mockbukkit.MockBukkit; +import be.seeseemelk.mockbukkit.ServerMock; +import io.github.thebusybiscuit.cscorelib2.item.CustomItem; +import io.github.thebusybiscuit.slimefun4.api.player.PlayerBackpack; +import io.github.thebusybiscuit.slimefun4.api.player.PlayerProfile; +import io.github.thebusybiscuit.slimefun4.implementation.items.backpacks.Cooler; +import io.github.thebusybiscuit.slimefun4.implementation.items.food.Juice; +import io.github.thebusybiscuit.slimefun4.implementation.listeners.BackpackListener; +import io.github.thebusybiscuit.slimefun4.implementation.listeners.CoolerListener; +import io.github.thebusybiscuit.slimefun4.testing.TestUtilities; +import me.mrCookieSlime.Slimefun.SlimefunPlugin; +import me.mrCookieSlime.Slimefun.Lists.RecipeType; +import me.mrCookieSlime.Slimefun.Objects.Category; +import me.mrCookieSlime.Slimefun.api.SlimefunItemStack; + +public class TestCoolerListener { + + private static ServerMock server; + private static CoolerListener listener; + + private static Cooler cooler; + private static Juice juice; + + @BeforeAll + public static void load() { + server = MockBukkit.mock(); + SlimefunPlugin plugin = MockBukkit.load(SlimefunPlugin.class); + + Category category = new Category(new NamespacedKey(plugin, "cooler_test"), new CustomItem(Material.SNOWBALL, "Mr. Freeze")); + SlimefunItemStack item = new SlimefunItemStack("TEST_COOLER", Material.SNOWBALL, "&6Test Cooler", "", "&7ID: "); + cooler = new Cooler(18, category, item, RecipeType.NULL, new ItemStack[9]); + cooler.register(plugin); + + SlimefunItemStack juiceItem = new SlimefunItemStack("TEST_JUICE", Color.RED, new PotionEffect(PotionEffectType.HEALTH_BOOST, 6, 0), "&4Test Juice"); + juice = new Juice(category, juiceItem, RecipeType.NULL, new ItemStack[9]); + juice.register(plugin); + + listener = new CoolerListener(plugin, cooler); + } + + @AfterAll + public static void unload() { + MockBukkit.unmock(); + } + + @Test + public void testOnlyJuiceAllowance() { + Assertions.assertFalse(cooler.isItemAllowed(new ItemStack(Material.DIAMOND), null)); + Assertions.assertFalse(cooler.isItemAllowed(cooler.getItem(), cooler)); + Assertions.assertTrue(cooler.isItemAllowed(juice.getItem(), juice)); + } + + @Test + public void testCoolerUsage() throws InterruptedException { + Player player = server.addPlayer(); + PlayerProfile profile = TestUtilities.awaitProfile(player); + PlayerBackpack backpack = profile.createBackpack(cooler.getSize()); + backpack.getInventory().setItem(0, juice.getItem().clone()); + + ItemStack personalCooler = cooler.getItem().clone(); + new BackpackListener().setBackpackId(player, personalCooler, 1, backpack.getId()); + player.getInventory().setItem(7, personalCooler); + + FoodLevelChangeEvent event = new FoodLevelChangeEvent(player, 16); + listener.onHungerLoss(event); + Assertions.assertTrue(player.hasPotionEffect(PotionEffectType.HEALTH_BOOST)); + } +}