Skip to content

Instantly share code, notes, and snippets.

@MarcinSwierczynski
Last active February 5, 2022 13:20
Show Gist options
  • Save MarcinSwierczynski/ec8607b937a73960e81ec9057daea28f to your computer and use it in GitHub Desktop.
Save MarcinSwierczynski/ec8607b937a73960e81ec9057daea28f to your computer and use it in GitHub Desktop.
Legacy Fighter - Week 2
package legacyfigher.dietary.newproducts;
import java.math.BigDecimal;
import java.util.UUID;
public class OldProduct {
UUID serialNumber = UUID.randomUUID();
Price price;
Quantity quantity;
private Description description;
public OldProduct(BigDecimal price, String desc, String longDesc, Integer counter) {
this.description = new Description(desc, longDesc);
this.price = new Price(price);
this.quantity = new Quantity(counter);
}
void decrementCounter() {
if (price.isInvalid()) {
throw new IllegalStateException("Invalid price");
}
this.quantity = quantity.decrease();
}
void incrementCounter() {
if (price.isInvalid()) {
throw new IllegalStateException("Invalid price");
}
this.quantity = quantity.increase();
}
void changePriceTo(BigDecimal newPrice) {
if (quantity.isEmpty()) {
throw new IllegalStateException("null counter");
}
this.price = new Price(newPrice);
}
void replaceCharFromDesc(String charToReplace, String replaceWith) {
this.description = description.replaceChars(charToReplace, replaceWith);
}
String formatDesc() {
return description.format();
}
}
class Quantity {
int value;
Quantity(Integer value) {
if (value == null || value < 0) {
throw new IllegalStateException("Quantity cannot be less then 0");
}
this.value = value;
}
boolean isEmpty() {
return value == 0;
}
Quantity increase() {
int value = this.value + 1;
return new Quantity(value);
}
Quantity decrease() {
int value = this.value - 1;
return new Quantity(value);
}
}
class Price {
final BigDecimal value;
Price(BigDecimal value) {
if (value == null) {
throw new IllegalStateException("new price null");
}
this.value = value;
}
boolean isInvalid() {
return value.signum() <= 0;
}
}
class Description {
final String desc;
final String longDesc;
Description(String desc, String longDesc) {
this.desc = desc;
this.longDesc = longDesc;
}
String format() {
if (isLongDescIsEmpty() || isDescEmpty()) {
return "";
}
return desc + " *** " + longDesc;
}
Description replaceChars(String charToReplace, String replaceWith) {
if (isLongDescIsEmpty() || isDescEmpty()) {
throw new IllegalStateException("null or empty desc");
}
return new Description(
desc.replace(charToReplace, replaceWith),
longDesc.replace(charToReplace, replaceWith)
);
}
private boolean isLongDescIsEmpty() {
return longDesc == null || longDesc.isEmpty();
}
private boolean isDescEmpty() {
return desc == null || desc.isEmpty();
}
}
package legacyfigher.dietary.newproducts;
import org.junit.jupiter.api.Test;
import static legacyfigher.dietary.newproducts.Fixtures.aProductWithDescription;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;
class ChangeCharactersInDescriptionTest {
@Test
void cannotReplaceCharsIfLongDescriptionIsEmpty() {
//given
OldProduct product = aProductWithDescription("", "desc");
//expect
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> product.replaceCharFromDesc("old", "new"));
}
@Test
void cannotReplaceCharsIfDescriptionIsEmpty() {
//given
OldProduct product = aProductWithDescription("desc", "");
//expect
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> product.replaceCharFromDesc("old", "new"));
}
@Test
void replaceCharsInBothDescriptions() {
//given
OldProduct product = aProductWithDescription("old desc", "old long desc");
//when
product.replaceCharFromDesc("old", "new");
//then
assertEquals("new desc *** new long desc", product.formatDesc());
}
}
package legacyfigher.dietary.newproducts;
import org.junit.jupiter.api.Test;
import static legacyfigher.dietary.newproducts.Fixtures.aProductWithDescription;
import static org.junit.jupiter.api.Assertions.assertEquals;
class ChangeOldProductDescriptionTest {
@Test
void returnsEmptyStringIfLongDescIsEmpty() {
OldProduct product = aProductWithDescription("desc", "");
String desc = product.formatDesc();
assertEquals("", desc);
}
@Test
void returnsEmptyStringIfShortDescIsEmpty() {
OldProduct product = aProductWithDescription("", "desc");
String desc = product.formatDesc();
assertEquals("", desc);
}
@Test
void returnsDescriptionIfShortAndLongDescAreNotEmpty() {
OldProduct product = aProductWithDescription("desc", "long desc");
String desc = product.formatDesc();
assertEquals("desc *** long desc", desc);
}
}
package legacyfigher.dietary.newproducts;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import static legacyfigher.dietary.newproducts.Fixtures.aProduct;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
class ChangePriceTest {
@Test
void cannotChangePriceIfProductIsNotAvailable() {
//given
OldProduct product = aProduct(BigDecimal.TEN, 0);
//expect
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> product.changePriceTo(BigDecimal.ONE));
}
@Test
void cannotChangePriceIfNewPriceIsNull() {
//given
OldProduct product = aProduct(BigDecimal.TEN, 1);
//expect
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(() -> product.changePriceTo(null));
}
@Test
void changePriceForExistingProduct() {
//given
OldProduct product = aProduct(BigDecimal.TEN, 5);
//when
product.changePriceTo(BigDecimal.ONE);
//then
assertEquals(BigDecimal.ONE, product.price.value);
}
@Test
void makeProductFree() {
//given
OldProduct product = aProduct(BigDecimal.TEN, 5);
//when
product.changePriceTo(BigDecimal.ZERO);
//then
assertEquals(BigDecimal.ZERO, product.price.value);
}
}
package legacyfigher.dietary.newproducts;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import static legacyfigher.dietary.newproducts.Fixtures.aProduct;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.assertEquals;
class DecrementCounterTest {
@Test
void cannotDecreaseQuantityIfTheResultWouldBeNegative() {
//given
OldProduct product = aProduct(BigDecimal.TEN, 0);
//expect
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(product::decrementCounter);
}
@Test
void cannotDecreaseQuantityIfPriceIsZero() {
//given
OldProduct product = aProduct(BigDecimal.ZERO, 2);
//expect
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(product::decrementCounter);
}
@Test
void cannotDecreaseQuantityIfPriceIsNegative() {
//given
OldProduct product = aProduct(BigDecimal.valueOf(-1), 2);
//expect
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(product::decrementCounter);
}
@Test
void decreaseQuantity() {
//given
OldProduct product = aProduct(BigDecimal.TEN, 5);
//when
product.decrementCounter();
//then
assertEquals(4, product.quantity.value);
}
}
package legacyfigher.dietary.newproducts;
import org.junit.jupiter.api.Test;
import java.math.BigDecimal;
import static legacyfigher.dietary.newproducts.Fixtures.aProduct;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;
class IncrementCounterTest {
@Test
void cannotIncreaseQuantityIfPriceIsZero() {
//given
OldProduct product = aProduct(BigDecimal.ZERO, 2);
//expect
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(product::incrementCounter);
}
@Test
void cannotIncreaseQuantityIfPriceIsNegative() {
//given
OldProduct product = aProduct(BigDecimal.valueOf(-1), 2);
//expect
assertThatExceptionOfType(IllegalStateException.class)
.isThrownBy(product::incrementCounter);
}
@Test
void increaseQuantity() {
//given
OldProduct product = aProduct(BigDecimal.TEN, 5);
//when
product.incrementCounter();
//then
assertEquals(6, product.quantity.value);
}
}
package legacyfigher.dietary.newproducts;
import java.math.BigDecimal;
class Fixtures {
static OldProduct aProductWithDescription(String desc, String longDesc) {
return new OldProduct(BigDecimal.ZERO, desc, longDesc, 0);
}
static OldProduct aProduct(BigDecimal price, Integer quantity) {
return new OldProduct(price, null, null, quantity);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment