aboutsummaryrefslogtreecommitdiffstats
path: root/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java
diff options
context:
space:
mode:
Diffstat (limited to 'test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java')
-rw-r--r--test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java131
1 files changed, 131 insertions, 0 deletions
diff --git a/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java b/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java
new file mode 100644
index 0000000..d233ddf
--- /dev/null
+++ b/test/eu/equalparts/cardbase/cardstorage/ReferenceCardContainerTest.java
@@ -0,0 +1,131 @@
+package eu.equalparts.cardbase.cardstorage;
+
+import static org.junit.Assert.assertEquals;
+
+import org.junit.AfterClass;
+import org.junit.Before;
+import org.junit.BeforeClass;
+import org.junit.Rule;
+import org.junit.Test;
+import org.junit.rules.ExpectedException;
+
+import com.fasterxml.jackson.databind.ObjectMapper;
+
+import eu.equalparts.cardbase.cards.Card;
+
+/**
+ * This test class tests storage-by-reference behaviour only.
+ * For standalone storage behaviour tests, see {@code StandaloneCardContainerTest}.
+ *
+ * @author Eduardo Pedroni
+ *
+ */
+public class ReferenceCardContainerTest {
+ private ReferenceCardContainer uut;
+ private static Card testCard;
+
+ @Rule
+ public final ExpectedException exception = ExpectedException.none();
+
+ @BeforeClass
+ public static void setUpBeforeClass() throws Exception {
+ ObjectMapper mapper = new ObjectMapper();
+ testCard = mapper.readValue(ReferenceCardContainerTest.class.getResourceAsStream("/shivandragon.json"), Card.class);
+ }
+
+ @AfterClass
+ public static void tearDownAfterClass() throws Exception {
+ }
+
+ @Before
+ public void setUp() throws Exception {
+ uut = new ReferenceCardContainer() {
+ };
+ }
+
+ /***********************************************************************************
+ * Adding card tests, happy path
+ ***********************************************************************************/
+ @Test
+ public void newCardIsAdded() throws Exception {
+ uut.addCard(testCard, 1);
+
+ assertEquals("Container should have contained 1 test card.", 1, uut.getCount(testCard));
+ }
+
+ @Test
+ public void existingCardIsIncremented() throws Exception {
+ uut.addCard(testCard, 2);
+ uut.addCard(testCard, 4);
+
+ assertEquals("Container should have contained 6 test cards.", 6, uut.getCount(testCard));
+ }
+
+ /*
+ * Edge cases
+ */
+ @Test
+ public void cardAddedIsNull() throws Exception {
+ exception.expect(NullPointerException.class);
+ uut.addCard(null, 0);
+ }
+
+ /***********************************************************************************
+ * Removing card tests, happy path
+ ***********************************************************************************/
+ @Test
+ public void cardRemoveCountIsLessThanCardCount() throws Exception {
+ uut.addCard(testCard, 5);
+
+ int removed = uut.removeCard(testCard, 3);
+
+ assertEquals("Card count was not updated correctly.", 2, uut.getCount(testCard));
+ assertEquals("Container reports wrong removed count.", 3, removed);
+ }
+
+ @Test
+ public void cardRemoveCountIsEqualToCardCount() throws Exception {
+ uut.addCard(testCard, 5);
+
+ int removed = uut.removeCard(testCard, 5);
+
+ assertEquals("Card was not removed from container.", 0, uut.getCount(testCard));
+ assertEquals("Container reports wrong removed count.", 5, removed);
+ }
+
+ @Test
+ public void cardRemoveCountIsGreaterThanCardCount() throws Exception {
+ uut.addCard(testCard, 3);
+
+ int removed = uut.removeCard(testCard, 5);
+
+ assertEquals("Card was not removed from container.", 0, uut.getCount(testCard));
+ assertEquals("Container reports wrong removed count.", 3, removed);
+ }
+
+ /*
+ * Edge cases
+ */
+ @Test
+ public void removedCardIsNull() throws Exception {
+ exception.expect(NullPointerException.class);
+ uut.removeCard(null, 0);
+ }
+
+ @Test
+ public void removedCardIsNotInCardbase() throws Exception {
+ int removed = uut.removeCard(testCard, 1);
+
+ assertEquals("Removed count should be 0.", 0, removed);
+ }
+
+ @Test
+ public void removedCountIsLessThanZero() throws Exception {
+ uut.addCard(testCard, 3);
+
+ int removed = uut.removeCard(testCard, -4);
+
+ assertEquals("Card count in container should be unchanged.", 3, uut.getCount(testCard));
+ assertEquals("Container reports wrong removed count.", 0, removed);
+ }
+}