Merge pull request #8125 from eugenp/revert-8119-BAEL-3275-2

Revert "BAEL-3275: Using blocking queue for pub-sub"
This commit is contained in:
Eric Martin
2019-10-31 20:43:47 -05:00
committed by GitHub
parent db85c8f275
commit 3225470df5
20543 changed files with 1642750 additions and 0 deletions
@@ -0,0 +1,17 @@
package com.baeldung.ddd.order;
import java.util.Arrays;
import java.util.List;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
public class OrderFixtureUtils {
public static List<OrderLine> anyOrderLines() {
return Arrays.asList(new OrderLine(new Product(Money.of(CurrencyUnit.USD, 100)), 1));
}
public static List<OrderLine> orderLineItemsWorthNDollars(int totalCost) {
return Arrays.asList(new OrderLine(new Product(Money.of(CurrencyUnit.USD, totalCost)), 1));
}
}
@@ -0,0 +1,70 @@
package com.baeldung.ddd.order;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.catchThrowable;
import java.util.ArrayList;
import java.util.Arrays;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
class OrderUnitTest {
@DisplayName("given order with two items, when calculate total cost, then sum is returned")
@Test
void test0() throws Exception {
// given
OrderLine ol0 = new OrderLine(new Product(Money.of(CurrencyUnit.USD, 10.00)), 2);
OrderLine ol1 = new OrderLine(new Product(Money.of(CurrencyUnit.USD, 5.00)), 10);
Order order = new Order(Arrays.asList(ol0, ol1));
// when
Money totalCost = order.totalCost();
// then
assertThat(totalCost).isEqualTo(Money.of(CurrencyUnit.USD, 70.00));
}
@DisplayName("when create order without line items, then exception is thrown")
@Test
void test1() throws Exception {
// when
Throwable throwable = catchThrowable(() -> new Order(new ArrayList<>()));
// then
assertThat(throwable).isInstanceOf(IllegalArgumentException.class);
}
@DisplayName("given order with two line items, when add another line item, then total cost is updated")
@Test
void test2() throws Exception {
// given
OrderLine ol0 = new OrderLine(new Product(Money.of(CurrencyUnit.USD, 10.00)), 1);
OrderLine ol1 = new OrderLine(new Product(Money.of(CurrencyUnit.USD, 5.00)), 1);
Order order = new Order(Arrays.asList(ol0, ol1));
// when
order.addLineItem(new OrderLine(new Product(Money.of(CurrencyUnit.USD, 20.00)), 2));
// then
assertThat(order.totalCost()).isEqualTo(Money.of(CurrencyUnit.USD, 55));
}
@DisplayName("given order with three line items, when remove item, then total cost is updated")
@Test
void test3() throws Exception {
// given
OrderLine ol0 = new OrderLine(new Product(Money.of(CurrencyUnit.USD, 10.00)), 1);
OrderLine ol1 = new OrderLine(new Product(Money.of(CurrencyUnit.USD, 20.00)), 1);
OrderLine ol2 = new OrderLine(new Product(Money.of(CurrencyUnit.USD, 30.00)), 1);
Order order = new Order(Arrays.asList(ol0, ol1, ol2));
// when
order.removeLineItem(1);
// then
assertThat(order.totalCost()).isEqualTo(Money.of(CurrencyUnit.USD, 40.00));
}
}
@@ -0,0 +1,77 @@
package com.baeldung.ddd.order.doubledispatch;
import static org.assertj.core.api.Assertions.assertThat;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.baeldung.ddd.order.OrderFixtureUtils;
public class DoubleDispatchDiscountPolicyUnitTest {
// @formatter:off
@DisplayName(
"given regular order with items worth $100 total, " +
"when apply 10% discount policy, " +
"then cost after discount is $90"
)
// @formatter:on
@Test
void test() throws Exception {
// given
Order order = new Order(OrderFixtureUtils.orderLineItemsWorthNDollars(100));
SpecialDiscountPolicy discountPolicy = new SpecialDiscountPolicy() {
@Override
public double discount(Order order) {
return 0.10;
}
@Override
public double discount(SpecialOrder order) {
return 0;
}
};
// when
Money totalCostAfterDiscount = order.totalCost(discountPolicy);
// then
assertThat(totalCostAfterDiscount).isEqualTo(Money.of(CurrencyUnit.USD, 90));
}
// @formatter:off
@DisplayName(
"given special order eligible for extra discount with items worth $100 total, " +
"when apply 20% discount policy for extra discount orders, " +
"then cost after discount is $80"
)
// @formatter:on
@Test
void test1() throws Exception {
// given
boolean eligibleForExtraDiscount = true;
Order order = new SpecialOrder(OrderFixtureUtils.orderLineItemsWorthNDollars(100), eligibleForExtraDiscount);
SpecialDiscountPolicy discountPolicy = new SpecialDiscountPolicy() {
@Override
public double discount(Order order) {
return 0;
}
@Override
public double discount(SpecialOrder order) {
if (order.isEligibleForExtraDiscount())
return 0.20;
return 0.10;
}
};
// when
Money totalCostAfterDiscount = order.totalCost(discountPolicy);
// then
assertThat(totalCostAfterDiscount).isEqualTo(Money.of(CurrencyUnit.USD, 80.00));
}
}
@@ -0,0 +1,43 @@
package com.baeldung.ddd.order.doubledispatch;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.baeldung.ddd.order.doubledispatch.Order;
import com.baeldung.ddd.order.OrderFixtureUtils;
import com.baeldung.ddd.order.OrderLine;
import com.baeldung.ddd.order.doubledispatch.visitor.HtmlOrderViewCreator;
public class HtmlOrderViewCreatorUnitTest {
// @formatter:off
@DisplayName(
"given collection of regular and special orders, " +
"when create HTML view using visitor for each order, " +
"then the dedicated view is created for each order"
)
// @formatter:on
@Test
void test() throws Exception {
// given
List<OrderLine> anyOrderLines = OrderFixtureUtils.anyOrderLines();
List<Order> orders = Arrays.asList(new Order(anyOrderLines), new SpecialOrder(anyOrderLines));
HtmlOrderViewCreator htmlOrderViewCreator = new HtmlOrderViewCreator();
// when
orders.get(0)
.accept(htmlOrderViewCreator);
String regularOrderHtml = htmlOrderViewCreator.getHtml();
orders.get(1)
.accept(htmlOrderViewCreator);
String specialOrderHtml = htmlOrderViewCreator.getHtml();
// then
assertThat(regularOrderHtml).containsPattern("<p>Regular order total cost: .*</p>");
assertThat(specialOrderHtml).containsPattern("<h1>Special Order</h1><p>total cost: .*</p>");
}
}
@@ -0,0 +1,50 @@
package com.baeldung.ddd.order.doubledispatch;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.List;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.baeldung.ddd.order.doubledispatch.Order;
import com.baeldung.ddd.order.OrderFixtureUtils;
import com.baeldung.ddd.order.OrderLine;
import com.baeldung.ddd.order.doubledispatch.SpecialDiscountPolicy;
import com.baeldung.ddd.order.doubledispatch.SpecialOrder;
public class MethodOverloadExampleUnitTest {
// @formatter:off
@DisplayName(
"given discount policy accepting special orders, " +
"when apply the policy on special order declared as regular order, " +
"then regular discount method is used"
)
// @formatter:on
@Test
void test() throws Exception {
// given
SpecialDiscountPolicy specialPolicy = new SpecialDiscountPolicy() {
@Override
public double discount(Order order) {
return 0.01;
}
@Override
public double discount(SpecialOrder order) {
return 0.10;
}
};
Order specialOrder = new SpecialOrder(anyOrderLines());
// when
double discount = specialPolicy.discount(specialOrder);
// then
assertThat(discount).isEqualTo(0.01);
}
private List<OrderLine> anyOrderLines() {
return OrderFixtureUtils.anyOrderLines();
}
}
@@ -0,0 +1,37 @@
package com.baeldung.ddd.order.doubledispatch;
import static org.assertj.core.api.Assertions.assertThat;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import com.baeldung.ddd.order.OrderFixtureUtils;
public class SingleDispatchDiscountPolicyUnitTest {
// @formatter:off
@DisplayName(
"given two discount policies, " +
"when use these policies, " +
"then single dispatch chooses the implementation based on runtime type"
)
// @formatter:on
@Test
void test() throws Exception {
// given
DiscountPolicy flatPolicy = new FlatDiscountPolicy();
DiscountPolicy amountPolicy = new AmountBasedDiscountPolicy();
Order orderWorth501Dollars = orderWorthNDollars(501);
// when
double flatDiscount = flatPolicy.discount(orderWorth501Dollars);
double amountDiscount = amountPolicy.discount(orderWorth501Dollars);
// then
assertThat(flatDiscount).isEqualTo(0.01);
assertThat(amountDiscount).isEqualTo(0.1);
}
private Order orderWorthNDollars(int totalCost) {
return new Order(OrderFixtureUtils.orderLineItemsWorthNDollars(totalCost));
}
}
@@ -0,0 +1,40 @@
package com.baeldung.ddd.order.jpa;
import static org.assertj.core.api.Assertions.assertThat;
import java.math.BigDecimal;
import java.util.Arrays;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
@SpringJUnitConfig
@SpringBootTest
public class PersistOrderIntegrationTest {
@Autowired
private JpaOrderRepository repository;
@DisplayName("given order with two line items, when persist, then order is saved")
@Test
public void test() throws Exception {
// given
JpaOrder order = prepareTestOrderWithTwoLineItems();
// when
JpaOrder savedOrder = repository.save(order);
// then
JpaOrder foundOrder = repository.findById(savedOrder.getId())
.get();
assertThat(foundOrder.getOrderLines()).hasSize(2);
}
private JpaOrder prepareTestOrderWithTwoLineItems() {
JpaOrderLine ol0 = new JpaOrderLine(new JpaProduct(BigDecimal.valueOf(10.00), "USD"), 2);
JpaOrderLine ol1 = new JpaOrderLine(new JpaProduct(BigDecimal.valueOf(5.00), "USD"), 10);
return new JpaOrder(Arrays.asList(ol0, ol1));
}
}
@@ -0,0 +1,35 @@
package com.baeldung.ddd.order.jpa;
import static org.assertj.core.api.Assertions.assertThat;
import java.math.BigDecimal;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
public class ViolateOrderBusinessRulesUnitTest {
@DisplayName("given two non-zero order line items, when create an order with them, it's possible to set total cost to zero")
@Test
void test() throws Exception {
// given
// available products
JpaProduct lungChingTea = new JpaProduct(BigDecimal.valueOf(10.00), "USD");
JpaProduct gyokuroMiyazakiTea = new JpaProduct(BigDecimal.valueOf(20.00), "USD");
// Lung Ching tea order line
JpaOrderLine orderLine0 = new JpaOrderLine(lungChingTea, 2);
// Gyokuro Miyazaki tea order line
JpaOrderLine orderLine1 = new JpaOrderLine(gyokuroMiyazakiTea, 3);
// when
// create the order
JpaOrder order = new JpaOrder();
order.addLineItem(orderLine0);
order.addLineItem(orderLine1);
order.setTotalCost(BigDecimal.ZERO);
order.setCurrencyUnit("USD");
// then
// this doesn't look good...
assertThat(order.getTotalCost()).isEqualTo(BigDecimal.ZERO);
}
}
@@ -0,0 +1,50 @@
package com.baeldung.ddd.order.mongo;
import static org.assertj.core.api.Assertions.assertThat;
import java.util.Arrays;
import java.util.List;
import org.joda.money.CurrencyUnit;
import org.joda.money.Money;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.test.context.junit.jupiter.SpringJUnitConfig;
import com.baeldung.ddd.order.Order;
import com.baeldung.ddd.order.OrderLine;
import com.baeldung.ddd.order.Product;
@SpringJUnitConfig
@SpringBootTest
public class OrderMongoIntegrationTest {
@Autowired
private OrderMongoRepository repo;
@DisplayName("given order with two line items, when persist using mongo repository, then order is saved")
@Test
void test() throws Exception {
// given
Order order = prepareTestOrderWithTwoLineItems();
// when
repo.save(order);
// then
List<Order> foundOrders = repo.findAll();
assertThat(foundOrders).hasSize(1);
List<OrderLine> foundOrderLines = foundOrders.iterator()
.next()
.getOrderLines();
assertThat(foundOrderLines).hasSize(2);
assertThat(foundOrderLines).containsOnlyElementsOf(order.getOrderLines());
}
private Order prepareTestOrderWithTwoLineItems() {
OrderLine ol0 = new OrderLine(new Product(Money.of(CurrencyUnit.USD, 10.00)), 2);
OrderLine ol1 = new OrderLine(new Product(Money.of(CurrencyUnit.USD, 5.00)), 10);
return new Order(Arrays.asList(ol0, ol1));
}
}