diff --git a/src/main/java/de/raidcraft/rcinventory/database/TDatabaseInventory.java b/src/main/java/de/raidcraft/rcinventory/database/TDatabaseInventory.java index 7e33538..37f6502 100644 --- a/src/main/java/de/raidcraft/rcinventory/database/TDatabaseInventory.java +++ b/src/main/java/de/raidcraft/rcinventory/database/TDatabaseInventory.java @@ -8,9 +8,7 @@ import lombok.Setter; import net.silthus.ebean.BaseEntity; -import javax.persistence.Entity; -import javax.persistence.Lob; -import javax.persistence.Table; +import javax.persistence.*; import java.io.IOException; import java.util.List; import java.util.UUID; @@ -25,6 +23,7 @@ public class TDatabaseInventory extends BaseEntity { private UUID holderId; @Lob + @Basic(fetch= FetchType.EAGER) private String serializedInventory; private Float saturation; private Float exp; @@ -82,7 +81,7 @@ public boolean equals(Object obj) { TDatabaseInventory other = (TDatabaseInventory)obj; - if(holderId != other.getHolderId()) return false; + if(!holderId.equals(other.getHolderId())) return false; if(!serializedInventory.equals(other.getSerializedInventory())) return false; if(!saturation.equals(other.getSaturation())) return false; if(!health.equals(other.getHealth())) return false; diff --git a/src/main/java/de/raidcraft/rcinventory/util/BukkitSerialization.java b/src/main/java/de/raidcraft/rcinventory/util/BukkitSerialization.java index 2dd6c37..422960e 100644 --- a/src/main/java/de/raidcraft/rcinventory/util/BukkitSerialization.java +++ b/src/main/java/de/raidcraft/rcinventory/util/BukkitSerialization.java @@ -1,5 +1,7 @@ package de.raidcraft.rcinventory.util; +import de.raidcraft.rcinventory.RCInventory; +import org.bukkit.Material; import org.bukkit.inventory.ItemStack; import org.bukkit.inventory.PlayerInventory; import org.bukkit.util.io.BukkitObjectInputStream; @@ -21,6 +23,10 @@ public static String playerInventoryToBase64(PlayerInventory playerInventory) th } public static String itemStackArrayToBase64(ItemStack[] items) throws IllegalStateException { + if(RCInventory.isTesting()) { + return "no-serialization-due-to-testing"; + } + try { ByteArrayOutputStream outputStream = new ByteArrayOutputStream(); BukkitObjectOutputStream dataOutput = new BukkitObjectOutputStream(outputStream); @@ -43,6 +49,11 @@ public static String itemStackArrayToBase64(ItemStack[] items) throws IllegalSta } public static ItemStack[] itemStackArrayFromBase64(String data) throws IOException { + if(RCInventory.isTesting()) { + ItemStack[] dummyContent = { new ItemStack(Material.COBBLESTONE ) }; + return dummyContent; + } + try { ByteArrayInputStream inputStream = new ByteArrayInputStream(Base64Coder.decodeLines(data)); BukkitObjectInputStream dataInput = new BukkitObjectInputStream(inputStream); diff --git a/src/test/java/de/raidcraft/rcinventory/IntegrationTest.java b/src/test/java/de/raidcraft/rcinventory/IntegrationTest.java index fc72adf..88fad9a 100644 --- a/src/test/java/de/raidcraft/rcinventory/IntegrationTest.java +++ b/src/test/java/de/raidcraft/rcinventory/IntegrationTest.java @@ -2,8 +2,10 @@ import be.seeseemelk.mockbukkit.MockBukkit; import be.seeseemelk.mockbukkit.ServerMock; +import de.raidcraft.rcinventory.database.TDatabaseInventory; import org.bukkit.entity.Player; import org.junit.jupiter.api.*; +import static org.assertj.core.api.Assertions.assertThat; public class IntegrationTest { @@ -23,6 +25,46 @@ void tearDown() { MockBukkit.unmock(); } + @Nested + @DisplayName("InventoryManager") + class InventoryManagerTests { + + @BeforeEach + void setUp() { + TDatabaseInventory.find.all().forEach(entry -> entry.delete()); + } + + @Test + @DisplayName("save-inventory") + void storeInventory() { + + Player player = server.addPlayer(); + + // Database must be empty + assertThat(TDatabaseInventory.find.query().findCount() == 0).isTrue(); + + // Add inventory + plugin.getInventoryManager().savePlayerInventory(player); + + // Check if added + assertThat(TDatabaseInventory.find.query().findCount() == 1).isTrue(); + + // Add again -> Due to missing changes must not be saved + plugin.getInventoryManager().savePlayerInventory(player); + + // Must still be '1' + assertThat(TDatabaseInventory.find.query().findCount() == 1).isTrue(); + + Player player2 = server.addPlayer(); + + // Add another players inventory + plugin.getInventoryManager().savePlayerInventory(player2); + + // Must be '2' + assertThat(TDatabaseInventory.find.query().findCount() == 2).isTrue(); + } + } + @Nested @DisplayName("Commands") class Commands { @@ -35,20 +77,14 @@ void setUp() { } @Nested - @DisplayName("/template:admin") + @DisplayName("/rcinventories") class AdminCommands { - @Nested - @DisplayName("foo bar") - class add { - - @Test - @Disabled - @DisplayName("should work") - void shouldWork() { + @Test + @DisplayName("reload") + void reload() { - server.dispatchCommand(server.getConsoleSender(),"rc:template add foo " + player.getName() + " bar"); - } + server.dispatchCommand(server.getConsoleSender(),"rcinventory reload"); } } }