From 557c1787c0e1a6fc03a1b2dfb64ef851ed969b09 Mon Sep 17 00:00:00 2001
From: Arturo Bernal Quality drops to 0 after the concert. Quality increases by 2 when there are 10 days or less and by 3 when there are 5 days or less. the value associated with the instance to decrement. the value associated with the instance to increment. The maximum size to which the quality can be. The {@link EncapsulatedItem} to update. Constructor for GenericUpdater. Constructor for GenericUpdater. Increment the quality of tge given {@link EncapsulatedItem}. Update the expired properties of tge given {@link EncapsulatedItem}. It depends on each product how to update the quality after the sale date Update the Quality properties of tge given {@link EncapsulatedItem}. Decrement the quality of tge given {@link EncapsulatedItem}. Update the sell in properties of tge given {@link EncapsulatedItem}. Factory class that is responsible for returning the right object type from a given item.
+ *
+ *
+ */
+public class AgedBrie
+ *
+ *
+ */
+public class BackStagePass
+ *
+ *
+ */
+public class Conjured
+ *
+ *
+ */
+public class Legendary
If the name of the item can't found in the {@link Articles} will be return a default implementation + * {@link GenericUpdater}
+ * + * These can be: + *Builder for the object {@link EncapsulatedItem}.
+ */ +public class EncapsulateItemBuilder { + + /** + * The name of the product. + */ + private String name; + /** + * SellIn value which denotes the number of days we have to sell the item. + */ + private int sellIn; + /** + * Quality value which denotes how valuable the item is. + */ + private int quality; + + /** + * Constructs a new instance of Encapsulate ItemBuilder which initialize + * the {@code name}, {@code sellIn} and {@code quality}. + */ + public EncapsulateItemBuilder() { + this.name = ""; + this.quality = 0; + this.sellIn = 0; + } + /** + *Set the name of the item.
+ */ + public EncapsulateItemBuilder named(final String name) { + this.name = name; + return this; + } + /** + *Set the quality value.
+ */ + public EncapsulateItemBuilder ofQuality(final int quality) { + this.quality = quality; + return this; + } + /** + *Set the sellIn value.
+ */ + public EncapsulateItemBuilder toSellIn(final int sellIn) { + this.sellIn = sellIn; + return this; + } + + /** + * Create the {@link EncapsulatedItem} object from all the previously set data. + */ + public EncapsulatedItem build() { + return new EncapsulatedItem(name, sellIn, quality); + } +} diff --git a/src/main/java/com/gildedrose/utils/EncapsulatedItem.java b/src/main/java/com/gildedrose/utils/EncapsulatedItem.java new file mode 100644 index 00000000..256f9b8a --- /dev/null +++ b/src/main/java/com/gildedrose/utils/EncapsulatedItem.java @@ -0,0 +1,85 @@ +package com.gildedrose.utils; + +import java.io.Serializable; + +/** + *Item copy object with all data encapsulated.
+ */ +public class EncapsulatedItem implements Serializable { + + private static final long serialVersionUID = 1989175797960240011L; + /** + * The name of the product. + */ + private String name; + /** + * SellIn value which denotes the number of days we have to sell the item. + */ + private int sellIn; + /** + * Quality value which denotes how valuable the item is. + */ + private int quality; + + /** + * Constructs a new instance of EncapsulateItem with the given {@code name}, {@code sellIn} and {@code quality}. + * @param name The name of the product. + * @param sellIn The number of days we have to sell the item + * @param quality The value which denotes how valuable the item is + */ + public EncapsulatedItem(final String name, final int sellIn, final int quality) { + this.setName(name); + this.setSellIn(sellIn); + this.setQuality(quality); + } + + /** + *Gets the name suitable for display.
+ * + * @return the name. + */ + public String getName() { + return name; + } + + /** + *Set the name of the item.
+ */ + public void setName(String name) { + this.name = name; + } + + /** + *Gets the sell in suitable for display.
+ * + * @return the sellIn value. + */ + public int getSellIn() { + return sellIn; + } + /** + *Set the sellIn value.
+ */ + public void setSellIn(final int sellIn) { + this.sellIn = sellIn; + } + /** + *Gets the quality suitable for display.
+ * + * @return the quality value. + */ + public int getQuality() { + return quality; + } + /** + *Set the quality value.
+ */ + public void setQuality(final int quality) { + this.quality = quality; + } + + @Override + public String toString() { + return this.getName() + ", " + this.getSellIn() + ", " + this.getQuality(); + } +} diff --git a/src/test/java/com/gildedrose/AgedBrieTest.java b/src/test/java/com/gildedrose/AgedBrieTest.java new file mode 100644 index 00000000..d752a7d8 --- /dev/null +++ b/src/test/java/com/gildedrose/AgedBrieTest.java @@ -0,0 +1,96 @@ +package com.gildedrose; + +import com.gildedrose.utils.Articles; +import com.gildedrose.utils.EncapsulatedItem; +import org.junit.jupiter.api.Test; + + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertThrows; + +/** + * Unit tests for methods of {@link com.gildedrose.product.category.AgedBrie} + * which been moved to their own test classes. + */ +class AgedBrieTest extends BaseTest{ + + @Test + void lowerQualityAndUpQuantityByOne() { + + // given + final int sellIn = 2; + final int quality = 2; + final EncapsulatedItem item = createItem(Articles.AGED_BRIE.getArticle(), sellIn, quality); + + // when + updateQuality(item); + + // then + assertAll( + () -> assertEquals(sellIn -1, item.getSellIn()), + () -> assertEquals(quality + 1, item.getQuality()) + ); + } + + @Test + void increaseQualityToDouble() { + + // given + final int sellIn = 0; + final int quality = 2; + final EncapsulatedItem item = createItem(Articles.AGED_BRIE.getArticle(), sellIn, quality); + + // when + updateQuality(item); + + // then + assertEquals(quality + 2, item.getQuality()); + } + + @Test + void tryUpdateQualityAboveMax() { + + // given + final int sellIn = 2; + final int quality = 50; + final EncapsulatedItem item = createItem(Articles.AGED_BRIE.getArticle(), sellIn, quality); + // when + updateQuality(item); + + // then + assertEquals(50, item.getQuality(), "The Quality of an item is never more than 50"); + } + + @Test + void toStringEncapsulateItem() { + // given + final int sellIn = 2; + final int quality = 2; + final EncapsulatedItem item = createItem(Articles.AGED_BRIE.getArticle(), sellIn, quality); + // when + updateQuality(item); + // then + assertEquals("Aged Brie, " + (sellIn - 1) + ", " + (quality + 1), item.toString()); + } + + @Test + void checkNullItems() { + // given + final int sellIn = 2; + final int quality = 2; + final EncapsulatedItem item = createItem(Articles.AGED_BRIE.getArticle(), sellIn, quality); + // when + // then + assertThrows(NullPointerException.class, () -> updateQuality(item, null)); + } + + @Test + void checkNullItemsInsideArray() { + // given + // when + // then + assertThrows(NullPointerException.class, () -> updateQuality((EncapsulatedItem) null)); + } + +} diff --git a/src/test/java/com/gildedrose/BackStagePassTest.java b/src/test/java/com/gildedrose/BackStagePassTest.java new file mode 100644 index 00000000..b3a154d2 --- /dev/null +++ b/src/test/java/com/gildedrose/BackStagePassTest.java @@ -0,0 +1,87 @@ +package com.gildedrose; + +import com.gildedrose.utils.Articles; +import com.gildedrose.utils.EncapsulatedItem; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Unit tests for methods of {@link com.gildedrose.product.category.BackStagePass} + * which been moved to their own test classes. + */ +public class BackStagePassTest extends BaseTest{ + + @Test + void lowerQualityAndUpQuantityByOne() { + + // given + final int sellIn = 15; + final int quality = 3; + final EncapsulatedItem item = createItem(Articles.TAFKAL_80ETC.getArticle(), sellIn, quality); + + // when + updateQuality(item); + + // then + assertEquals(quality + 1, item.getQuality()); + } + @Test + void increaseQualityToDouble() { + + // given + final int sellIn = 9; + final int quality = 3; + final EncapsulatedItem item = createItem(Articles.TAFKAL_80ETC.getArticle(), sellIn, quality); + + // when + updateQuality(item); + + // then + assertEquals(quality + 2, item.getQuality()); + } + @Test + void dropAfterConcert() { + + // given + final int sellIn = 0; + final int quality = 3; + final EncapsulatedItem item = createItem(Articles.TAFKAL_80ETC.getArticle(), sellIn, quality); + + // when + updateQuality(item); + + // then + assertEquals(sellIn, item.getQuality()); + } + + @Test + void increaseQualityToDoubleByThree() { + + // given + final int sellIn = 2; + final int quality = 2; + final EncapsulatedItem item = createItem(Articles.TAFKAL_80ETC.getArticle(), sellIn, quality); + + // when + updateQuality(item); + + // then + assertEquals(quality + 3, item.getQuality()); + } + + @Test + void tryUpdateQualityAboveMax() { + + // given + final int sellIn = 5; + final int quality = 50; + final EncapsulatedItem item = createItem(Articles.AGED_BRIE.getArticle(), sellIn, quality); + + // when + updateQuality(item); + + // then + assertEquals(50, item.getQuality(), "The Quality of an item is never more than 50"); + } +} diff --git a/src/test/java/com/gildedrose/BaseTest.java b/src/test/java/com/gildedrose/BaseTest.java new file mode 100644 index 00000000..60f9317a --- /dev/null +++ b/src/test/java/com/gildedrose/BaseTest.java @@ -0,0 +1,28 @@ +package com.gildedrose; + +import com.gildedrose.utils.EncapsulatedItem; +import com.gildedrose.utils.EncapsulateItemBuilder; + +public class BaseTest { + + /** + * Create a {@link EncapsulatedItem} object given the {@code code}, {@code sellIn} and {@code quality}. + * @param name The name of the product. + * @param sellIn The number of days we have to sell the item + * @param quality The value which denotes how valuable the item is + */ + protected EncapsulatedItem createItem(final String name, final int sellIn, final int quality){ + return new EncapsulateItemBuilder() + .named(name) + .ofQuality(quality) + .toSellIn(sellIn) + .build(); + } + /** + * Create the instance of {@link GildedRose} and update the Quality of the {@code items} + */ + protected void updateQuality(final EncapsulatedItem...items){ + new GildedRose(items).updateQuality(); + } + +} diff --git a/src/test/java/com/gildedrose/ConjuredTest.java b/src/test/java/com/gildedrose/ConjuredTest.java new file mode 100644 index 00000000..60d8f502 --- /dev/null +++ b/src/test/java/com/gildedrose/ConjuredTest.java @@ -0,0 +1,65 @@ +package com.gildedrose; + +import com.gildedrose.utils.Articles; +import com.gildedrose.utils.EncapsulatedItem; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Unit tests for methods of {@link com.gildedrose.product.category.Conjured} + * which been moved to their own test classes. + */ +class ConjuredTest extends BaseTest{ + + @Test + void lowerQualityByTwoAndUpQuantityByOne() { + + // given + final int sellIn = 4; + final int quality = 4; + final EncapsulatedItem item = createItem(Articles.CONJURED.getArticle(), sellIn, quality); + + // when + updateQuality(item); + + // then + assertAll( + () -> assertEquals(sellIn -1, item.getSellIn()), + () -> assertEquals(quality - 2, item.getQuality()) + ); + } + + @Test + void increaseQualityToDouble() { + + // given + final int sellIn = 0; + final int quality = 2; + final EncapsulatedItem item = createItem(Articles.AGED_BRIE.getArticle(), sellIn, quality); + + // when + updateQuality(item); + + // then + assertEquals(quality + 2, item.getQuality()); + } + + @Test + void tryUpdateQualityAboveMax() { + + // given + final int sellIn = 4; + final int quality = 50; + final EncapsulatedItem item = createItem(Articles.AGED_BRIE.getArticle(), sellIn, quality); + + // when + updateQuality(item); + + // then + assertEquals(50, item.getQuality(), "The Quality of an item is never more than 50"); + } + + +} diff --git a/src/test/java/com/gildedrose/GildedRoseTest.java b/src/test/java/com/gildedrose/GildedRoseTest.java deleted file mode 100644 index 7c5fc0a2..00000000 --- a/src/test/java/com/gildedrose/GildedRoseTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.gildedrose; - -import org.junit.jupiter.api.Test; - -import static org.junit.jupiter.api.Assertions.assertEquals; - -class GildedRoseTest { - - @Test - void foo() { - Item[] items = new Item[] { new Item("foo", 0, 0) }; - GildedRose app = new GildedRose(items); - app.updateQuality(); - assertEquals("foo", app.items[0].name); - } - -} diff --git a/src/test/java/com/gildedrose/LegendaryTest.java b/src/test/java/com/gildedrose/LegendaryTest.java new file mode 100644 index 00000000..cf7a61e3 --- /dev/null +++ b/src/test/java/com/gildedrose/LegendaryTest.java @@ -0,0 +1,63 @@ +package com.gildedrose; + +import com.gildedrose.utils.Articles; +import com.gildedrose.utils.EncapsulatedItem; +import org.junit.jupiter.api.Test; + +import static org.junit.jupiter.api.Assertions.assertAll; +import static org.junit.jupiter.api.Assertions.assertEquals; + +/** + * Unit tests for methods of {@link com.gildedrose.product.category.Legendary} + * which been moved to their own test classes. + */ +class LegendaryTest extends BaseTest{ + + @Test + void tryLowerQualityAndUpQuantityByOne() { + + // given + final int sellIn = 2; + final int quality = 2; + final EncapsulatedItem item = createItem(Articles.SULFURAS.getArticle(), sellIn, quality); + + // when + updateQuality(item); + + // then + assertAll( + () -> assertEquals(sellIn, item.getSellIn()), + () -> assertEquals(quality, item.getQuality()) + ); + } + + @Test + void increaseQualityToDouble() { + + // given + final int sellIn = 4; + final int quality = 80; + final EncapsulatedItem item = createItem(Articles.SULFURAS.getArticle(), sellIn, quality); + + // when + updateQuality(item); + + // then + assertEquals(quality, item.getQuality()); + } + + @Test + void tryIncreaseQuality() { + + // given + final int sellIn = -1; + final int quality = 80; + final EncapsulatedItem item = createItem(Articles.SULFURAS.getArticle(), sellIn, quality); + + // when + updateQuality(item); + + // then + assertEquals(quality, item.getQuality()); + } +} diff --git a/src/test/java/com/gildedrose/TexttestFixture.java b/src/test/java/com/gildedrose/TexttestFixture.java index d059c88f..088e5a71 100644 --- a/src/test/java/com/gildedrose/TexttestFixture.java +++ b/src/test/java/com/gildedrose/TexttestFixture.java @@ -1,20 +1,13 @@ package com.gildedrose; +import com.gildedrose.utils.EncapsulatedItem; +import com.gildedrose.utils.EncapsulateItemBuilder; + public class TexttestFixture { public static void main(String[] args) { System.out.println("OMGHAI!"); - Item[] items = new Item[] { - new Item("+5 Dexterity Vest", 10, 20), // - new Item("Aged Brie", 2, 0), // - new Item("Elixir of the Mongoose", 5, 7), // - new Item("Sulfuras, Hand of Ragnaros", 0, 80), // - new Item("Sulfuras, Hand of Ragnaros", -1, 80), - new Item("Backstage passes to a TAFKAL80ETC concert", 15, 20), - new Item("Backstage passes to a TAFKAL80ETC concert", 10, 49), - new Item("Backstage passes to a TAFKAL80ETC concert", 5, 49), - // this conjured item does not work properly yet - new Item("Conjured Mana Cake", 3, 6) }; + final EncapsulatedItem[] items = buildItems(); GildedRose app = new GildedRose(items); @@ -26,7 +19,7 @@ public class TexttestFixture { for (int i = 0; i < days; i++) { System.out.println("-------- day " + i + " --------"); System.out.println("name, sellIn, quality"); - for (Item item : items) { + for (EncapsulatedItem item : items) { System.out.println(item); } System.out.println(); @@ -34,4 +27,18 @@ public class TexttestFixture { } } + private static EncapsulatedItem[] buildItems() { + return new EncapsulatedItem[] { + new EncapsulateItemBuilder().named("+5 Dexterity Vest").toSellIn(10).ofQuality(20).build(), + new EncapsulateItemBuilder().named("Aged Brie").toSellIn(2).ofQuality(0).build(), + new EncapsulateItemBuilder().named("Elixir of the Mongoose").toSellIn(5).ofQuality(7).build(), + new EncapsulateItemBuilder().named("Sulfuras, Hand of Ragnaros").toSellIn(0).ofQuality(80).build(), + new EncapsulateItemBuilder().named("Sulfuras, Hand of Ragnaros").toSellIn(-1).ofQuality(80).build(), + new EncapsulateItemBuilder().named("Backstage passes to a TAFKAL80ETC concert").toSellIn(15).ofQuality(20).build(), + new EncapsulateItemBuilder().named("Backstage passes to a TAFKAL80ETC concert").toSellIn(10).ofQuality(49).build(), + new EncapsulateItemBuilder().named("Backstage passes to a TAFKAL80ETC concert").toSellIn(5).ofQuality(49).build(), + new EncapsulateItemBuilder().named("Conjured Mana Cake").toSellIn(3).ofQuality(6).build(),}; + + } + }