introducing command object for applying reviews

This commit is contained in:
Michał Michaluk
2018-03-10 09:58:28 +01:00
parent efee8c58a4
commit 6110fcaeb3
10 changed files with 71 additions and 28 deletions

View File

@@ -0,0 +1,28 @@
package pl.com.bottega.factory.demand.forecasting;
import lombok.Value;
import java.util.Collections;
@Value
public class ApplyReviewDecision {
ReviewRequired.ToReview review;
ReviewDecision decision;
boolean requireAdjustment() {
return decision != ReviewDecision.IGNORE;
}
AdjustDemand toAdjustment() {
return new AdjustDemand(review.getRefNo(),
Collections.singletonMap(
review.getDate(),
Adjustment.weak(decision.toAdjustment(review))
)
);
}
public String getRefNo() {
return review.getRefNo();
}
}

View File

@@ -1,7 +1,6 @@
package pl.com.bottega.factory.demand.forecasting;
import lombok.AllArgsConstructor;
import pl.com.bottega.factory.demand.forecasting.ReviewRequired.ToReview;
@AllArgsConstructor
public class DemandService {
@@ -24,9 +23,9 @@ public class DemandService {
repository.save(model);
}
public void review(ToReview review, ReviewDecision decision) {
ProductDemand model = repository.get(review.getRefNo());
model.review(review, decision);
public void review(ApplyReviewDecision reviewDecision) {
ProductDemand model = repository.get(reviewDecision.getRefNo());
model.review(reviewDecision);
repository.save(model);
}
}

View File

@@ -59,9 +59,9 @@ class ProductDemand {
}
}
void review(ToReview review, ReviewDecision decision) {
if (decision.requireAdjustment()) {
adjust(decision.toAdjustment(review));
void review(ApplyReviewDecision reviewDecision) {
if (reviewDecision.requireAdjustment()) {
adjust(reviewDecision.toAdjustment());
}
}

View File

@@ -3,7 +3,6 @@ package pl.com.bottega.factory.demand.forecasting;
import lombok.AllArgsConstructor;
import pl.com.bottega.factory.demand.forecasting.ReviewRequired.ToReview;
import java.util.Collections;
import java.util.function.Function;
@AllArgsConstructor
@@ -15,19 +14,10 @@ public enum ReviewDecision {
private final Function<ToReview, Demand> pick;
public AdjustDemand toAdjustment(ToReview review) {
if (this == IGNORE) {
public Demand toAdjustment(ToReview review) {
if (this == ReviewDecision.IGNORE) {
throw new IllegalStateException("can't convert " + this + " to adjustment");
}
return new AdjustDemand(review.getRefNo(),
Collections.singletonMap(
review.getDate(),
Adjustment.weak(pick.apply(review))
)
);
}
public boolean requireAdjustment() {
return this != IGNORE;
return pick.apply(review);
}
}

View File

@@ -58,14 +58,14 @@ class DemandServiceSpec extends Specification implements ProductDemandTrait {
def "Repository interactions while review processing"() {
given:
def today = LocalDate.now(builder.clock)
def demand = demand(0, 2800)
def demand = demand(2800)
.stronglyAdjusted((today): 3500)
.build()
def review = review(today, 0, 3500, 2800)
def review = reviewDecision(review(today, 0, 3500, 2800), PICK_NEW)
repo.get(review.refNo) >> demand
when:
service.review(review, PICK_NEW)
service.review(review)
then:
1 * repo.save(demand)

View File

@@ -87,4 +87,9 @@ class ProductDemandBuilder {
Demand.of(strongAdjustment),
Demand.of(newDocumented))
}
ApplyReviewDecision reviewDecision(ToReview review,
ReviewDecision decision) {
new ApplyReviewDecision(review, decision)
}
}

View File

@@ -43,4 +43,8 @@ trait ProductDemandTrait {
long newDocumented) {
return builder.review(date, previousDocumented, strongAdjustment, newDocumented)
}
ApplyReviewDecision reviewDecision(ToReview review, ReviewDecision decision) {
builder.reviewDecision(review, decision)
}
}

View File

@@ -38,7 +38,10 @@ class ReviewProcessingSpec extends Specification implements ProductDemandTrait {
.build()
when:
demand.review(review(tomorrow, 0, 3500, 2800), IGNORE)
demand.review(reviewDecision(
review(tomorrow, 0, 3500, 2800),
IGNORE
))
then:
0 * events.emit(_ as DemandedLevelsChanged)
@@ -53,7 +56,10 @@ class ReviewProcessingSpec extends Specification implements ProductDemandTrait {
.build()
when:
demand.review(review(tomorrow, 0, 3500, 2800), PICK_NEW)
demand.review(reviewDecision(
review(tomorrow, 0, 3500, 2800),
PICK_NEW
))
then:
1 * events.emit(levelChanged([], [3500, 2800]))
@@ -68,7 +74,10 @@ class ReviewProcessingSpec extends Specification implements ProductDemandTrait {
.build()
when:
demand.review(review(tomorrow, 0, 3500, 2800), PICK_PREVIOUS)
demand.review(reviewDecision(
review(tomorrow, 0, 3500, 2800),
PICK_PREVIOUS
))
then:
1 * events.emit(levelChanged([], [3500, 0]))
@@ -83,7 +92,10 @@ class ReviewProcessingSpec extends Specification implements ProductDemandTrait {
.build()
when:
demand.review(review(tomorrow, 0, 3500, 2800), MAKE_ADJUSTMENT_WEAK)
demand.review(reviewDecision(
review(tomorrow, 0, 3500, 2800),
MAKE_ADJUSTMENT_WEAK
))
then:
0 * events.emit(_ as DemandedLevelsChanged)