-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: improve Deliverable interface and introduce None strategy
- NullObject pattern used to cover initial state of the ShoppingCart
- Loading branch information
Showing
6 changed files
with
93 additions
and
0 deletions.
There are no files selected for viewing
6 changes: 6 additions & 0 deletions
6
src/main/java/io/github/akadir/casestudy/delivery/Deliverable.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,7 +1,13 @@ | ||
package io.github.akadir.casestudy.delivery; | ||
|
||
import io.github.akadir.casestudy.delivery.strategy.DeliveryStrategy; | ||
|
||
public interface Deliverable { | ||
int getProductCount(); | ||
|
||
int getDeliveryCount(); | ||
|
||
DeliveryStrategy getDeliveryStrategy(); | ||
|
||
void setDeliveryStrategy(DeliveryStrategy deliveryStrategy); | ||
} |
10 changes: 10 additions & 0 deletions
10
src/main/java/io/github/akadir/casestudy/delivery/strategy/None.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
package io.github.akadir.casestudy.delivery.strategy; | ||
|
||
import io.github.akadir.casestudy.delivery.Deliverable; | ||
|
||
public class None implements DeliveryStrategy { | ||
@Override | ||
public double calculateDeliveryCost(Deliverable deliverable) { | ||
return 0; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
35 changes: 35 additions & 0 deletions
35
src/test/java/io/github/akadir/casestudy/delivery/strategy/NoneTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
package io.github.akadir.casestudy.delivery.strategy; | ||
|
||
import io.github.akadir.casestudy.product.model.Product; | ||
import io.github.akadir.casestudy.shopping.service.ShoppingCartService; | ||
import io.github.akadir.casestudy.shopping.service.impl.ConcreteShoppingCartServiceImpl; | ||
import org.junit.Test; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import static org.assertj.core.api.Assertions.assertThat; | ||
|
||
public class NoneTest { | ||
|
||
@Test | ||
public void whenNoneDeliveryCostCalculatedThenExpectZeroCost() { | ||
DeliveryStrategy deliveryStrategy = new None(); | ||
|
||
ShoppingCartService shoppingCartService = new ConcreteShoppingCartServiceImpl(); | ||
|
||
Map<Product, Integer> productsBackup = new HashMap<>(shoppingCartService.getProducts()); | ||
|
||
assertThat(deliveryStrategy.calculateDeliveryCost(shoppingCartService.getShoppingCart())) | ||
.as("Should return zero for shopping cart object") | ||
.isZero(); | ||
|
||
assertThat(deliveryStrategy.calculateDeliveryCost(null)) | ||
.as("Should return zero for null") | ||
.isZero(); | ||
|
||
assertThat(shoppingCartService.getProducts()) | ||
.as("Shopping cart should not be modified in delivery cost calculation") | ||
.isEqualTo(productsBackup); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters