mirror of
https://github.com/emilybache/GildedRose-Refactoring-Kata.git
synced 2026-02-15 22:41:30 +00:00
refactor: add specialized classes for handling each case
This commit is contained in:
parent
1e257bc659
commit
5e9ed0aa84
@ -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 +
|
|
||||||
'}';
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -1,16 +1,19 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose;
|
||||||
|
|
||||||
|
import com.gildedrose.item.GRItem;
|
||||||
|
import com.gildedrose.item.GRItemFactory;
|
||||||
|
|
||||||
import java.util.Arrays;
|
import java.util.Arrays;
|
||||||
|
|
||||||
class GildedRose {
|
class GildedRose {
|
||||||
|
|
||||||
Item[] items;
|
Item[] items;
|
||||||
|
|
||||||
public GildedRose(Item[] items) {
|
public GildedRose(Item[] items) {
|
||||||
Arrays.stream(items).forEach(GRItem::validateItem);
|
|
||||||
this.items = items;
|
this.items = items;
|
||||||
}
|
}
|
||||||
|
|
||||||
public void updateQuality() {
|
public void updateQuality() {
|
||||||
Arrays.stream(items).map(GRItem::new).forEach(GRItem::updateQuality);
|
Arrays.stream(items).map(GRItemFactory::create).forEach(GRItem::updateQuality);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
22
Java/src/main/java/com/gildedrose/item/AgedBrieGRItem.java
Normal file
22
Java/src/main/java/com/gildedrose/item/AgedBrieGRItem.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
26
Java/src/main/java/com/gildedrose/item/ConjuredGRItem.java
Normal file
26
Java/src/main/java/com/gildedrose/item/ConjuredGRItem.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
28
Java/src/main/java/com/gildedrose/item/GRItem.java
Normal file
28
Java/src/main/java/com/gildedrose/item/GRItem.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
40
Java/src/main/java/com/gildedrose/item/GRItemFactory.java
Normal file
40
Java/src/main/java/com/gildedrose/item/GRItemFactory.java
Normal file
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose.item;
|
||||||
|
|
||||||
public class ItemQualityExceedsMaxValueException extends RuntimeException {
|
public class ItemQualityExceedsMaxValueException extends RuntimeException {
|
||||||
|
|
||||||
@ -1,4 +1,4 @@
|
|||||||
package com.gildedrose;
|
package com.gildedrose.item;
|
||||||
|
|
||||||
public class ItemQualityIsNegativeException extends RuntimeException {
|
public class ItemQualityIsNegativeException extends RuntimeException {
|
||||||
|
|
||||||
22
Java/src/main/java/com/gildedrose/item/RegularGRItem.java
Normal file
22
Java/src/main/java/com/gildedrose/item/RegularGRItem.java
Normal file
@ -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;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
15
Java/src/main/java/com/gildedrose/item/SulfurasGRItem.java
Normal file
15
Java/src/main/java/com/gildedrose/item/SulfurasGRItem.java
Normal file
@ -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() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@ -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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Loading…
Reference in New Issue
Block a user