From 5e9ed0aa84b527e66f5b9548176858c1af1d34ec Mon Sep 17 00:00:00 2001 From: Ben Leers Date: Mon, 29 Jun 2020 11:05:49 +0200 Subject: [PATCH] refactor: add specialized classes for handling each case --- Java/src/main/java/com/gildedrose/GRItem.java | 97 ------------- .../main/java/com/gildedrose/GildedRose.java | 7 +- .../com/gildedrose/item/AgedBrieGRItem.java | 22 +++ .../item/BackstagePassesGRItem.java | 35 +++++ .../com/gildedrose/item/ConjuredGRItem.java | 26 ++++ .../main/java/com/gildedrose/item/GRItem.java | 28 ++++ .../com/gildedrose/item/GRItemFactory.java | 40 ++++++ .../ItemQualityExceedsMaxValueException.java | 2 +- .../ItemQualityIsNegativeException.java | 2 +- .../com/gildedrose/item/RegularGRItem.java | 22 +++ .../com/gildedrose/item/SulfurasGRItem.java | 15 ++ .../com/gildedrose/GRItemConstructorTest.java | 22 --- .../gildedrose/GRItemUpdateQualityTest.java | 132 ------------------ .../gildedrose/GRItemValidateItemTest.java | 33 ----- .../GuildedRoseConstructorTest.java | 17 --- .../item/AgedBrieGRItemUpdateQualityTest.java | 49 +++++++ ...ackstagePassesGRItemUpdateQualityTest.java | 74 ++++++++++ .../item/ConjuredGRItemUpdateQualityTest.java | 55 ++++++++ .../item/GRItemFactoryCreateTest.java | 52 +++++++ .../item/GRItemValidateItemTest.java | 35 +++++ .../item/RegularGRItemUpdateQualityTest.java | 49 +++++++ .../item/SulfurasGRItemUpdateQualityTest.java | 25 ++++ 22 files changed, 534 insertions(+), 305 deletions(-) delete mode 100644 Java/src/main/java/com/gildedrose/GRItem.java create mode 100644 Java/src/main/java/com/gildedrose/item/AgedBrieGRItem.java create mode 100644 Java/src/main/java/com/gildedrose/item/BackstagePassesGRItem.java create mode 100644 Java/src/main/java/com/gildedrose/item/ConjuredGRItem.java create mode 100644 Java/src/main/java/com/gildedrose/item/GRItem.java create mode 100644 Java/src/main/java/com/gildedrose/item/GRItemFactory.java rename Java/src/main/java/com/gildedrose/{ => item}/ItemQualityExceedsMaxValueException.java (88%) rename Java/src/main/java/com/gildedrose/{ => item}/ItemQualityIsNegativeException.java (87%) create mode 100644 Java/src/main/java/com/gildedrose/item/RegularGRItem.java create mode 100644 Java/src/main/java/com/gildedrose/item/SulfurasGRItem.java delete mode 100644 Java/src/test/java/com/gildedrose/GRItemConstructorTest.java delete mode 100644 Java/src/test/java/com/gildedrose/GRItemUpdateQualityTest.java delete mode 100644 Java/src/test/java/com/gildedrose/GRItemValidateItemTest.java delete mode 100644 Java/src/test/java/com/gildedrose/GuildedRoseConstructorTest.java create mode 100644 Java/src/test/java/com/gildedrose/item/AgedBrieGRItemUpdateQualityTest.java create mode 100644 Java/src/test/java/com/gildedrose/item/BackstagePassesGRItemUpdateQualityTest.java create mode 100644 Java/src/test/java/com/gildedrose/item/ConjuredGRItemUpdateQualityTest.java create mode 100644 Java/src/test/java/com/gildedrose/item/GRItemFactoryCreateTest.java create mode 100644 Java/src/test/java/com/gildedrose/item/GRItemValidateItemTest.java create mode 100644 Java/src/test/java/com/gildedrose/item/RegularGRItemUpdateQualityTest.java create mode 100644 Java/src/test/java/com/gildedrose/item/SulfurasGRItemUpdateQualityTest.java diff --git a/Java/src/main/java/com/gildedrose/GRItem.java b/Java/src/main/java/com/gildedrose/GRItem.java deleted file mode 100644 index 8132a3b5..00000000 --- a/Java/src/main/java/com/gildedrose/GRItem.java +++ /dev/null @@ -1,97 +0,0 @@ -package com.gildedrose; - -public class GRItem { - - private static final String AGED_BRIE = "Aged Brie"; - private static final String SULFURAS = "Sulfuras, Hand of Ragnaros"; - private static final int MAX_QUALITY = 50; - private static final int MIN_QUALITY = 0; - - private final Item item; - - public GRItem(Item item) { - this.item = item; - } - - public GRItem(String name, int sellIn, int quality) { - this.item = new Item(name, sellIn, quality); - } - - public String getName() { - return item.name; - } - - public int getSellIn() { - return item.sellIn; - } - - public int getQuality() { - return item.quality; - } - - public void updateQuality() { - if (!item.name.equals(AGED_BRIE) - && !item.name.equals("Backstage passes to a TAFKAL80ETC concert")) { - if (item.quality > 0) { - if (!item.name.equals(SULFURAS)) { - item.quality = item.quality - 1; - } - } - } else { - if (item.quality < MAX_QUALITY) { - item.quality = item.quality + 1; - - if (item.name.equals("Backstage passes to a TAFKAL80ETC concert")) { - if (item.sellIn < 11) { - if (item.quality < MAX_QUALITY) { - item.quality = item.quality + 1; - } - } - - if (item.sellIn < 6) { - if (item.quality < MAX_QUALITY) { - item.quality = item.quality + 1; - } - } - } - } - } - - if (!item.name.equals(SULFURAS)) { - item.sellIn = item.sellIn - 1; - } - - if (item.sellIn < 0) { - if (!item.name.equals(AGED_BRIE)) { - if (!item.name.equals("Backstage passes to a TAFKAL80ETC concert")) { - if (item.quality > MIN_QUALITY) { - if (!item.name.equals(SULFURAS)) { - item.quality = item.quality - 1; - } - } - } else { - item.quality = 0; - } - } else { - if (item.quality < MAX_QUALITY) { - item.quality = item.quality + 1; - } - } - } - } - - public static void validateItem(Item item) { - if (item.quality < GRItem.MIN_QUALITY) { - throw new ItemQualityIsNegativeException(item.name); - } else if (item.quality > GRItem.MAX_QUALITY && !item.name.equals(SULFURAS)) { - throw new ItemQualityExceedsMaxValueException(item.name); - } - } - - @Override - public String toString() { - return "GRItem{" + - "item=" + item + - '}'; - } -} diff --git a/Java/src/main/java/com/gildedrose/GildedRose.java b/Java/src/main/java/com/gildedrose/GildedRose.java index 3942c4de..285c9527 100644 --- a/Java/src/main/java/com/gildedrose/GildedRose.java +++ b/Java/src/main/java/com/gildedrose/GildedRose.java @@ -1,16 +1,19 @@ package com.gildedrose; +import com.gildedrose.item.GRItem; +import com.gildedrose.item.GRItemFactory; + import java.util.Arrays; class GildedRose { + Item[] items; public GildedRose(Item[] items) { - Arrays.stream(items).forEach(GRItem::validateItem); this.items = items; } public void updateQuality() { - Arrays.stream(items).map(GRItem::new).forEach(GRItem::updateQuality); + Arrays.stream(items).map(GRItemFactory::create).forEach(GRItem::updateQuality); } } \ No newline at end of file diff --git a/Java/src/main/java/com/gildedrose/item/AgedBrieGRItem.java b/Java/src/main/java/com/gildedrose/item/AgedBrieGRItem.java new file mode 100644 index 00000000..79747770 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/item/AgedBrieGRItem.java @@ -0,0 +1,22 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; + +public class AgedBrieGRItem extends GRItem { + + AgedBrieGRItem(Item item) { + super(item); + } + + @Override + public void updateQuality() { + if (item.quality < MAX_QUALITY) { + item.quality = item.quality + 1; + } + item.sellIn = item.sellIn - 1; + + if (item.quality < MAX_QUALITY && item.sellIn < 0) { + item.quality = item.quality + 1; + } + } +} diff --git a/Java/src/main/java/com/gildedrose/item/BackstagePassesGRItem.java b/Java/src/main/java/com/gildedrose/item/BackstagePassesGRItem.java new file mode 100644 index 00000000..fdee4b52 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/item/BackstagePassesGRItem.java @@ -0,0 +1,35 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; + +public class BackstagePassesGRItem extends GRItem { + + BackstagePassesGRItem(Item item) { + super(item); + } + + @Override + public void updateQuality() { + if (item.quality < MAX_QUALITY) { + item.quality = item.quality + calculateQualityIncrement(); + } + + item.sellIn = item.sellIn - 1; + + if (item.sellIn < 0) { + item.quality = MIN_QUALITY; + } else if (item.quality > MAX_QUALITY) { + item.quality = MAX_QUALITY; + } + } + + private int calculateQualityIncrement() { + if (item.sellIn < 6) { + return 3; + } else if (item.sellIn < 11) { + return 2; + } else { + return 1; + } + } +} diff --git a/Java/src/main/java/com/gildedrose/item/ConjuredGRItem.java b/Java/src/main/java/com/gildedrose/item/ConjuredGRItem.java new file mode 100644 index 00000000..d302fa9f --- /dev/null +++ b/Java/src/main/java/com/gildedrose/item/ConjuredGRItem.java @@ -0,0 +1,26 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; + +public class ConjuredGRItem extends GRItem { + + public ConjuredGRItem(Item item) { + super(item); + } + + @Override + public void updateQuality() { + if (item.quality > MIN_QUALITY) { + item.quality = item.quality - 2; + } + item.sellIn = item.sellIn - 1; + + if (item.sellIn < 0 && item.quality > MIN_QUALITY) { + item.quality = item.quality - 2; + } + + if (item.quality < MIN_QUALITY) { + item.quality = MIN_QUALITY; + } + } +} diff --git a/Java/src/main/java/com/gildedrose/item/GRItem.java b/Java/src/main/java/com/gildedrose/item/GRItem.java new file mode 100644 index 00000000..630298e0 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/item/GRItem.java @@ -0,0 +1,28 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; + +import static com.gildedrose.item.GRItemFactory.SULFURAS; + +public abstract class GRItem { + + protected static final int MAX_QUALITY = 50; + protected static final int MIN_QUALITY = 0; + + protected final Item item; + + GRItem(Item item) { + validateItem(item); + this.item = item; + } + + public abstract void updateQuality(); + + private static void validateItem(Item item) { + if (item.quality < GRItem.MIN_QUALITY) { + throw new ItemQualityIsNegativeException(item.name); + } else if (item.quality > GRItem.MAX_QUALITY && !item.name.equals(SULFURAS)) { + throw new ItemQualityExceedsMaxValueException(item.name); + } + } +} diff --git a/Java/src/main/java/com/gildedrose/item/GRItemFactory.java b/Java/src/main/java/com/gildedrose/item/GRItemFactory.java new file mode 100644 index 00000000..efb180ff --- /dev/null +++ b/Java/src/main/java/com/gildedrose/item/GRItemFactory.java @@ -0,0 +1,40 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; + +public class GRItemFactory { + public static final String AGED_BRIE = "Aged Brie"; + public static final String SULFURAS = "Sulfuras, Hand of Ragnaros"; + public static final String BACKSTAGE_PASSES = "Backstage passes"; + public static final String CONJURED = "Conjured"; + + public static GRItem create(Item item) { + if (isAgedBrie(item)) { + return new AgedBrieGRItem(item); + } else if (isBackstagePasses(item)) { + return new BackstagePassesGRItem(item); + } else if (isConjuredItem(item)) { + return new ConjuredGRItem(item); + } else if(isSulfuras(item)) { + return new SulfurasGRItem(item); + } + return new RegularGRItem(item); + } + + private static boolean isSulfuras(Item item) { + return item.name.equals(SULFURAS); + } + + private static boolean isConjuredItem(Item item) { + return item.name.startsWith(CONJURED); + } + + private static boolean isBackstagePasses(Item item) { + return item.name.startsWith(BACKSTAGE_PASSES); + } + + private static boolean isAgedBrie(Item item) { + return AGED_BRIE.equals(item.name); + } + +} diff --git a/Java/src/main/java/com/gildedrose/ItemQualityExceedsMaxValueException.java b/Java/src/main/java/com/gildedrose/item/ItemQualityExceedsMaxValueException.java similarity index 88% rename from Java/src/main/java/com/gildedrose/ItemQualityExceedsMaxValueException.java rename to Java/src/main/java/com/gildedrose/item/ItemQualityExceedsMaxValueException.java index d80fbd42..3d2cf685 100644 --- a/Java/src/main/java/com/gildedrose/ItemQualityExceedsMaxValueException.java +++ b/Java/src/main/java/com/gildedrose/item/ItemQualityExceedsMaxValueException.java @@ -1,4 +1,4 @@ -package com.gildedrose; +package com.gildedrose.item; public class ItemQualityExceedsMaxValueException extends RuntimeException { diff --git a/Java/src/main/java/com/gildedrose/ItemQualityIsNegativeException.java b/Java/src/main/java/com/gildedrose/item/ItemQualityIsNegativeException.java similarity index 87% rename from Java/src/main/java/com/gildedrose/ItemQualityIsNegativeException.java rename to Java/src/main/java/com/gildedrose/item/ItemQualityIsNegativeException.java index 1d9bf8ff..7e288638 100644 --- a/Java/src/main/java/com/gildedrose/ItemQualityIsNegativeException.java +++ b/Java/src/main/java/com/gildedrose/item/ItemQualityIsNegativeException.java @@ -1,4 +1,4 @@ -package com.gildedrose; +package com.gildedrose.item; public class ItemQualityIsNegativeException extends RuntimeException { diff --git a/Java/src/main/java/com/gildedrose/item/RegularGRItem.java b/Java/src/main/java/com/gildedrose/item/RegularGRItem.java new file mode 100644 index 00000000..45c9845d --- /dev/null +++ b/Java/src/main/java/com/gildedrose/item/RegularGRItem.java @@ -0,0 +1,22 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; + +public class RegularGRItem extends GRItem { + + RegularGRItem(Item item) { + super(item); + } + + @Override + public void updateQuality() { + if (item.quality > MIN_QUALITY) { + item.quality = item.quality - 1; + } + item.sellIn = item.sellIn - 1; + + if (item.sellIn < 0 && item.quality > MIN_QUALITY) { + item.quality = item.quality - 1; + } + } +} diff --git a/Java/src/main/java/com/gildedrose/item/SulfurasGRItem.java b/Java/src/main/java/com/gildedrose/item/SulfurasGRItem.java new file mode 100644 index 00000000..f6b1e655 --- /dev/null +++ b/Java/src/main/java/com/gildedrose/item/SulfurasGRItem.java @@ -0,0 +1,15 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; + +public class SulfurasGRItem extends GRItem { + + public SulfurasGRItem(Item item) { + super(item); + } + + @Override + public void updateQuality() { + + } +} diff --git a/Java/src/test/java/com/gildedrose/GRItemConstructorTest.java b/Java/src/test/java/com/gildedrose/GRItemConstructorTest.java deleted file mode 100644 index c7a20d67..00000000 --- a/Java/src/test/java/com/gildedrose/GRItemConstructorTest.java +++ /dev/null @@ -1,22 +0,0 @@ -package com.gildedrose; - -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThat; - -public class GRItemConstructorTest { - - private static final String NAME = "+5 Dexterity Vest"; - private static final int SELL_IN = 10; - private static final int QUALITY = 20; - - @Test - public void constructsCorrectly() { - Item item = new Item(NAME, SELL_IN, QUALITY); - GRItem grItem = new GRItem(item); - - assertThat(grItem.getName()).isEqualTo(NAME); - assertThat(grItem.getSellIn()).isEqualTo(SELL_IN); - assertThat(grItem.getQuality()).isEqualTo(QUALITY); - } -} diff --git a/Java/src/test/java/com/gildedrose/GRItemUpdateQualityTest.java b/Java/src/test/java/com/gildedrose/GRItemUpdateQualityTest.java deleted file mode 100644 index 2b92d135..00000000 --- a/Java/src/test/java/com/gildedrose/GRItemUpdateQualityTest.java +++ /dev/null @@ -1,132 +0,0 @@ -package com.gildedrose; - -import org.junit.jupiter.api.Test; -import org.junit.jupiter.params.ParameterizedTest; -import org.junit.jupiter.params.provider.ValueSource; - -import static org.apache.commons.lang3.RandomStringUtils.random; -import static org.assertj.core.api.Assertions.assertThat; - -public class GRItemUpdateQualityTest { - private static final String AGED_BRIE = "Aged Brie"; - private static final String SULFURAS = "Sulfuras, Hand of Ragnaros"; - private static final String BACKSTAGE_PASSES = "Backstage passes to a TAFKAL80ETC concert"; - private static final String CONJURED = "Conjured Mana Cake"; - - @Test - public void lowersTheSellInValue() { - GRItem item = new GRItem(random(5), 10, 20); - item.updateQuality(); - - assertThat(item.getSellIn()).isEqualTo(9); - } - - @Test - public void qualityDegrades() { - GRItem item = new GRItem(random(5), 10, 20); - item.updateQuality(); - - assertThat(item.getQuality()).isEqualTo(19); - } - - @Test - public void qualityDegradesTwiceAsFastWhenSellByDatePassed() { - GRItem item = new GRItem(random(5), 0, 20); - item.updateQuality(); - - assertThat(item.getQuality()).isEqualTo(18); - } - - @Test - public void qualityIsNeverNegative() { - GRItem item = new GRItem(random(5), 10, 0); - item.updateQuality(); - - assertThat(item.getQuality()).isEqualTo(0); - } - - @Test - public void qualityOfAgedBrieIncreases() { - GRItem item = new GRItem(AGED_BRIE, 10, 10); - item.updateQuality(); - - assertThat(item.getQuality()).isEqualTo(11); - } - - @Test - public void qualityOfAgedBrieIncreasesEvenWhenSellInIsNegative() { - GRItem item = new GRItem(AGED_BRIE, -1, 10); - item.updateQuality(); - - assertThat(item.getQuality()).isEqualTo(12); - } - - @Test - public void qualityIncreasesNeverHigherThan50() { - GRItem item = new GRItem(AGED_BRIE, 10, 50); - item.updateQuality(); - - assertThat(item.getQuality()).isEqualTo(50); - } - - @ParameterizedTest(name = "with quality {0}") - @ValueSource(ints = {-1, 0, 1, 20, 50}) - public void qualityOfSulfurasNeverAlters(int quality) { - GRItem item = new GRItem(SULFURAS, 10, quality); - item.updateQuality(); - - assertThat(item.getQuality()).isEqualTo(quality); - } - - @Test - public void sellInOfSulfurasNeverAlters() { - GRItem item = new GRItem(SULFURAS, 10, 20); - item.updateQuality(); - - assertThat(item.getSellIn()).isEqualTo(10); - } - - @ParameterizedTest(name = "with sellIn {0}") - @ValueSource(ints = {10, 9, 8, 7, 6}) - public void qualityOfBackstagePassesIncreaseBy2WhenSellIn10DaysOrLess(int sellIn) { - GRItem item = new GRItem(BACKSTAGE_PASSES, sellIn, 10); - item.updateQuality(); - - assertThat(item.getQuality()).isEqualTo(12); - } - - @ParameterizedTest(name = "with sellIn {0}") - @ValueSource(ints = {5, 4, 3, 2, 1}) - public void qualityOfBackstagePassesIncreaseBy3WhenSellIn5DaysOrLess(int sellIn) { - GRItem item = new GRItem(BACKSTAGE_PASSES, sellIn, 10); - item.updateQuality(); - - assertThat(item.getQuality()).isEqualTo(13); - } - - @ParameterizedTest(name = "with sellIn {0}") - @ValueSource(ints = {0, -1}) - public void qualityOfBackstagePassesIsZeroAfterConcert(int sellIn) { - GRItem item = new GRItem(BACKSTAGE_PASSES, sellIn, 10); - item.updateQuality(); - - assertThat(item.getQuality()).isEqualTo(0); - } - - @Test - public void conjuredItemDegradesTwiceAsFast() { - GRItem item = new GRItem(CONJURED, 10, 10); - item.updateQuality(); - - assertThat(item.getQuality()).isEqualTo(8); - } - - @ParameterizedTest(name = "with quality {0}") - @ValueSource(ints = {0, 1, 2}) - public void conjuredItemDegradesNotBelow0(int quality) { - GRItem item = new GRItem(CONJURED, 10, quality); - item.updateQuality(); - - assertThat(item.getQuality()).isEqualTo(0); - } -} diff --git a/Java/src/test/java/com/gildedrose/GRItemValidateItemTest.java b/Java/src/test/java/com/gildedrose/GRItemValidateItemTest.java deleted file mode 100644 index 957aabba..00000000 --- a/Java/src/test/java/com/gildedrose/GRItemValidateItemTest.java +++ /dev/null @@ -1,33 +0,0 @@ -package com.gildedrose; - -import org.junit.jupiter.api.Test; - -import static org.apache.commons.lang3.RandomStringUtils.random; -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -class GRItemValidateItemTest { - - private static final String SULFURAS = "Sulfuras, Hand of Ragnaros"; - - @Test - public void throwsExceptionWhenItemValueIsNegative() { - Item item = new Item(random(5), 10, -1); - - assertThatThrownBy(() -> GRItem.validateItem(item)).isInstanceOf(ItemQualityIsNegativeException.class); - } - - @Test - public void throwsExceptionWhenItemValueExceedsMaxValue() { - Item item = new Item(random(5), 10, 51); - - assertThatThrownBy(() -> GRItem.validateItem(item)).isInstanceOf(ItemQualityExceedsMaxValueException.class); - } - - - @Test - public void doesNotThrowExceptionWhenItemValueExceedsMaxValueAndItemIsSulfuras() { - Item item = new Item(SULFURAS, 10, 51); - - GRItem.validateItem(item); - } -} \ No newline at end of file diff --git a/Java/src/test/java/com/gildedrose/GuildedRoseConstructorTest.java b/Java/src/test/java/com/gildedrose/GuildedRoseConstructorTest.java deleted file mode 100644 index fdb42eca..00000000 --- a/Java/src/test/java/com/gildedrose/GuildedRoseConstructorTest.java +++ /dev/null @@ -1,17 +0,0 @@ -package com.gildedrose; - -import org.junit.jupiter.api.Test; - -import static org.assertj.core.api.Assertions.assertThatThrownBy; - -public class GuildedRoseConstructorTest { - - @Test - public void validatesInitialItemQualities() { - Item[] items = new Item[]{ - new Item("+5 Dexterity Vest", 10, -1) - }; - - assertThatThrownBy(() -> new GildedRose(items)).isInstanceOf(ItemQualityIsNegativeException.class); - } -} diff --git a/Java/src/test/java/com/gildedrose/item/AgedBrieGRItemUpdateQualityTest.java b/Java/src/test/java/com/gildedrose/item/AgedBrieGRItemUpdateQualityTest.java new file mode 100644 index 00000000..75becda4 --- /dev/null +++ b/Java/src/test/java/com/gildedrose/item/AgedBrieGRItemUpdateQualityTest.java @@ -0,0 +1,49 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; +import com.gildedrose.item.GRItem; +import com.gildedrose.item.GRItemFactory; +import org.junit.jupiter.api.Test; + +import static com.gildedrose.item.GRItemFactory.AGED_BRIE; +import static org.apache.commons.lang3.RandomStringUtils.random; +import static org.assertj.core.api.Assertions.assertThat; + +public class AgedBrieGRItemUpdateQualityTest { + + @Test + public void lowersTheSellInValue() { + Item item = new Item(random(5), 10, 20); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.sellIn).isEqualTo(9); + } + + @Test + public void qualityOfAgedBrieIncreases() { + Item item = new Item(AGED_BRIE, 10, 10); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(11); + } + + @Test + public void qualityOfAgedBrieIncreasesDoubleFastWhenSellInIsNegative() { + Item item = new Item(AGED_BRIE, -1, 10); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(12); + } + + @Test + public void qualityIncreasesNeverHigherThan50() { + Item item = new Item(AGED_BRIE, 10, 50); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(50); + } +} diff --git a/Java/src/test/java/com/gildedrose/item/BackstagePassesGRItemUpdateQualityTest.java b/Java/src/test/java/com/gildedrose/item/BackstagePassesGRItemUpdateQualityTest.java new file mode 100644 index 00000000..ecc4f86a --- /dev/null +++ b/Java/src/test/java/com/gildedrose/item/BackstagePassesGRItemUpdateQualityTest.java @@ -0,0 +1,74 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static com.gildedrose.item.GRItemFactory.BACKSTAGE_PASSES; +import static org.apache.commons.lang3.RandomStringUtils.random; +import static org.assertj.core.api.Assertions.assertThat; + +public class BackstagePassesGRItemUpdateQualityTest { + + private static final String BACKSTAGE_PASSES_RANDOM = BACKSTAGE_PASSES + " " + random(5); + + @Test + public void lowersTheSellInValue() { + Item item = new Item(BACKSTAGE_PASSES_RANDOM, 10, 20); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.sellIn).isEqualTo(9); + } + + @Test + public void qualityOfBackstagePassesIncreaseBy1WhenSellInisHigherThan10() { + Item item = new Item(BACKSTAGE_PASSES_RANDOM, 11, 10); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(11); + } + + @ParameterizedTest(name = "with sellIn {0}") + @ValueSource(ints = {10, 9, 8, 7, 6}) + public void qualityOfBackstagePassesIncreaseBy2WhenSellIn10DaysOrLess(int sellIn) { + Item item = new Item(BACKSTAGE_PASSES_RANDOM, sellIn, 10); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(12); + } + + @ParameterizedTest(name = "with sellIn {0}") + @ValueSource(ints = {5, 4, 3, 2, 1}) + public void qualityOfBackstagePassesIncreaseBy3WhenSellIn5DaysOrLess(int sellIn) { + Item item = new Item(BACKSTAGE_PASSES_RANDOM, sellIn, 10); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(13); + } + + @ParameterizedTest(name = "with sellIn {0}") + @ValueSource(ints = {0, -1}) + public void qualityOfBackstagePassesIsZeroAfterConcert(int sellIn) { + Item item = new Item(BACKSTAGE_PASSES_RANDOM, sellIn, 10); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(0); + } + + @ParameterizedTest(name = "with sellIn {0}") + @ValueSource(ints = {15, 9, 3}) + public void qualityOfBackstagePassesNeverIncreasesAboveMax(int sellIn) { + Item item = new Item(BACKSTAGE_PASSES_RANDOM, sellIn, 49); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(50); + } + +} diff --git a/Java/src/test/java/com/gildedrose/item/ConjuredGRItemUpdateQualityTest.java b/Java/src/test/java/com/gildedrose/item/ConjuredGRItemUpdateQualityTest.java new file mode 100644 index 00000000..55ae1704 --- /dev/null +++ b/Java/src/test/java/com/gildedrose/item/ConjuredGRItemUpdateQualityTest.java @@ -0,0 +1,55 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; +import com.gildedrose.item.GRItem; +import com.gildedrose.item.GRItemFactory; +import org.junit.jupiter.api.Test; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static com.gildedrose.item.GRItemFactory.CONJURED; +import static org.apache.commons.lang3.RandomStringUtils.random; +import static org.assertj.core.api.Assertions.assertThat; + +public class ConjuredGRItemUpdateQualityTest { + + private static final String CONJURED_STUFF = CONJURED + " Stuff"; + + @Test + public void lowersTheSellInValue() { + Item item = new Item(CONJURED_STUFF, 10, 20); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.sellIn).isEqualTo(9); + } + + @Test + public void qualityDegradesBy2() { + Item item = new Item(CONJURED_STUFF, 10, 20); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(18); + } + + @Test + public void qualityDegradesTwiceAsFastWhenSellByDatePassed() { + Item item = new Item(CONJURED_STUFF, 0, 20); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(16); + } + + @ParameterizedTest(name = "with quality {0}") + @ValueSource(ints = {0, 1, 2}) + public void conjuredItemDegradesNotBelow0(int quality) { + Item item = new Item(CONJURED_STUFF, 10, quality); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(0); + } + +} diff --git a/Java/src/test/java/com/gildedrose/item/GRItemFactoryCreateTest.java b/Java/src/test/java/com/gildedrose/item/GRItemFactoryCreateTest.java new file mode 100644 index 00000000..3c6f97d7 --- /dev/null +++ b/Java/src/test/java/com/gildedrose/item/GRItemFactoryCreateTest.java @@ -0,0 +1,52 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; +import com.gildedrose.item.*; +import org.junit.jupiter.api.Test; + +import static com.gildedrose.item.GRItemFactory.*; +import static org.apache.commons.lang3.RandomStringUtils.random; +import static org.assertj.core.api.Assertions.assertThat; + +public class GRItemFactoryCreateTest { + + @Test + public void createsRegularGRItem() { + Item item = new Item(random(5), 10, 20); + GRItem grItem = GRItemFactory.create(item); + + assertThat(grItem).isInstanceOf(RegularGRItem.class); + } + + @Test + public void createsConjuredGRItem() { + Item item = new Item(CONJURED + " " + random(5), 10, 20); + GRItem grItem = GRItemFactory.create(item); + + assertThat(grItem).isInstanceOf(ConjuredGRItem.class); + } + + @Test + public void createsBackstagePassesGRItem() { + Item item = new Item(BACKSTAGE_PASSES + " " + random(5), 10, 20); + GRItem grItem = GRItemFactory.create(item); + + assertThat(grItem).isInstanceOf(BackstagePassesGRItem.class); + } + + @Test + public void createsAgedBrieGRItem() { + Item item = new Item(AGED_BRIE, 10, 20); + GRItem grItem = GRItemFactory.create(item); + + assertThat(grItem).isInstanceOf(AgedBrieGRItem.class); + } + + @Test + public void createsSulfurasGRItem() { + Item item = new Item(SULFURAS, 10, 20); + GRItem grItem = GRItemFactory.create(item); + + assertThat(grItem).isInstanceOf(SulfurasGRItem.class); + } +} diff --git a/Java/src/test/java/com/gildedrose/item/GRItemValidateItemTest.java b/Java/src/test/java/com/gildedrose/item/GRItemValidateItemTest.java new file mode 100644 index 00000000..7890c8b5 --- /dev/null +++ b/Java/src/test/java/com/gildedrose/item/GRItemValidateItemTest.java @@ -0,0 +1,35 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; +import org.junit.jupiter.api.Test; + +import static org.apache.commons.lang3.RandomStringUtils.random; +import static org.apache.commons.lang3.RandomUtils.nextInt; +import static org.assertj.core.api.Assertions.assertThatThrownBy; + +class GRItemValidateItemTest { + + private static final String SULFURAS = "Sulfuras, Hand of Ragnaros"; + + @Test + public void throwsExceptionWhenItemValueIsNegative() { + Item item = new Item(random(5), nextInt(0, 10), -1); + + assertThatThrownBy(() -> GRItemFactory.create(item)).isInstanceOf(ItemQualityIsNegativeException.class); + } + + @Test + public void throwsExceptionWhenItemValueExceedsMaxValue() { + Item item = new Item(random(5), nextInt(0, 10), 51); + + assertThatThrownBy(() -> GRItemFactory.create(item)).isInstanceOf(ItemQualityExceedsMaxValueException.class); + } + + + @Test + public void doesNotThrowExceptionWhenItemValueExceedsMaxValueAndItemIsSulfuras() { + Item item = new Item(SULFURAS, nextInt(0, 10), 51); + + GRItemFactory.create(item); + } +} \ No newline at end of file diff --git a/Java/src/test/java/com/gildedrose/item/RegularGRItemUpdateQualityTest.java b/Java/src/test/java/com/gildedrose/item/RegularGRItemUpdateQualityTest.java new file mode 100644 index 00000000..901f896a --- /dev/null +++ b/Java/src/test/java/com/gildedrose/item/RegularGRItemUpdateQualityTest.java @@ -0,0 +1,49 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; +import com.gildedrose.item.GRItem; +import com.gildedrose.item.GRItemFactory; +import org.junit.jupiter.api.Test; + +import static org.apache.commons.lang3.RandomStringUtils.random; +import static org.assertj.core.api.Assertions.assertThat; + +public class RegularGRItemUpdateQualityTest { + + @Test + public void lowersTheSellInValue() { + Item item = new Item(random(5), 10, 20); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.sellIn).isEqualTo(9); + } + + @Test + public void qualityDegradesBy1() { + Item item = new Item(random(5), 10, 20); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(19); + } + + @Test + public void qualityDegradesTwiceAsFastWhenSellByDatePassed() { + Item item = new Item(random(5), 0, 20); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(18); + } + + @Test + public void qualityIsNeverNegative() { + Item item = new Item(random(5), 10, 0); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.quality).isEqualTo(0); + } + +} diff --git a/Java/src/test/java/com/gildedrose/item/SulfurasGRItemUpdateQualityTest.java b/Java/src/test/java/com/gildedrose/item/SulfurasGRItemUpdateQualityTest.java new file mode 100644 index 00000000..3a2da81b --- /dev/null +++ b/Java/src/test/java/com/gildedrose/item/SulfurasGRItemUpdateQualityTest.java @@ -0,0 +1,25 @@ +package com.gildedrose.item; + +import com.gildedrose.Item; +import com.gildedrose.item.GRItem; +import com.gildedrose.item.GRItemFactory; +import org.junit.jupiter.params.ParameterizedTest; +import org.junit.jupiter.params.provider.ValueSource; + +import static com.gildedrose.item.GRItemFactory.SULFURAS; +import static org.assertj.core.api.Assertions.assertThat; + +public class SulfurasGRItemUpdateQualityTest { + + @ParameterizedTest(name = "with quality {0}") + @ValueSource(ints = {0, 1, 20, 50}) + public void doesNothing(int quality) { + Item item = new Item(SULFURAS, 10, quality); + GRItem grItem = GRItemFactory.create(item); + grItem.updateQuality(); + + assertThat(item.sellIn).isEqualTo(10); + assertThat(item.quality).isEqualTo(quality); + } +} +