From 33fffa777c57d5f34ce0688ff155604c4ad9455c Mon Sep 17 00:00:00 2001 From: Jakub Pilimon Date: Sun, 11 Mar 2018 17:09:11 +0100 Subject: [PATCH] integration test for adjusting demand + minor cleanup --- .../com/bottega/factory/ProductTrait.groovy | 10 +- .../DemandAdjustmentIntegrationSpec.groovy | 99 +++++++++++++++++++ .../command/DemandAdjustmentEntity.java | 6 ++ .../persistence/DocumentEntity.java | 2 +- .../demand/forecasting/AdjustDemand.java | 4 +- .../demand/forecasting/Adjustment.java | 2 +- 6 files changed, 118 insertions(+), 5 deletions(-) create mode 100644 app-monolith/src/test/groovy/pl/com/bottega/factory/integration/DemandAdjustmentIntegrationSpec.groovy diff --git a/app-monolith/src/test/groovy/pl/com/bottega/factory/ProductTrait.groovy b/app-monolith/src/test/groovy/pl/com/bottega/factory/ProductTrait.groovy index 7ae2b03..0d89580 100644 --- a/app-monolith/src/test/groovy/pl/com/bottega/factory/ProductTrait.groovy +++ b/app-monolith/src/test/groovy/pl/com/bottega/factory/ProductTrait.groovy @@ -1,7 +1,10 @@ package pl.com.bottega.factory +import pl.com.bottega.factory.demand.forecasting.AdjustDemand +import pl.com.bottega.factory.demand.forecasting.Adjustment import pl.com.bottega.factory.demand.forecasting.Demand import pl.com.bottega.factory.demand.forecasting.Document +import pl.com.bottega.factory.demand.forecasting.command.DemandAdjustmentEntity import pl.com.bottega.factory.demand.forecasting.persistence.DocumentEntity import pl.com.bottega.factory.product.management.ProductDescription import pl.com.bottega.factory.product.management.ProductDescriptionEntity @@ -30,6 +33,11 @@ trait ProductTrait { results.put(date, Demand.of(level)) date = date.plusDays(1) } - new Document(created, refNo, results) + return new Document(created, refNo, results) + } + + DemandAdjustmentEntity strongAdjustment(String refNo, Map adjustments) { + return new DemandAdjustmentEntity(refNo, "agent", new AdjustDemand(refNo, adjustments)) + } } diff --git a/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/DemandAdjustmentIntegrationSpec.groovy b/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/DemandAdjustmentIntegrationSpec.groovy new file mode 100644 index 0000000..9c93caa --- /dev/null +++ b/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/DemandAdjustmentIntegrationSpec.groovy @@ -0,0 +1,99 @@ +package pl.com.bottega.factory.integration + +import org.springframework.beans.factory.annotation.Autowired +import org.springframework.boot.test.context.SpringBootTest +import org.springframework.boot.test.web.client.TestRestTemplate +import org.springframework.context.annotation.Bean +import org.springframework.context.annotation.Configuration +import org.springframework.core.ParameterizedTypeReference +import org.springframework.hateoas.Resources +import org.springframework.http.HttpMethod +import org.springframework.http.ResponseEntity +import pl.com.bottega.factory.AppConfiguration +import pl.com.bottega.factory.ProductTrait +import pl.com.bottega.factory.demand.forecasting.Adjustment +import pl.com.bottega.factory.demand.forecasting.Demand +import pl.com.bottega.factory.demand.forecasting.command.DemandAdjustmentEntity +import pl.com.bottega.factory.demand.forecasting.persistence.DocumentEntity +import pl.com.bottega.factory.demand.forecasting.projection.CurrentDemandEntity +import pl.com.bottega.factory.product.management.ProductDescriptionEntity +import pl.com.bottega.tools.IntegrationTest +import spock.lang.Specification + +import java.time.Clock +import java.time.LocalDate +import java.time.ZoneId + +import static java.time.Instant.from +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT + +@IntegrationTest +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = AppConfiguration) +class DemandAdjustmentIntegrationSpec extends Specification implements ProductTrait { + + public static final String PRODUCT_REF_NO = "3009001" + public static final LocalDate ANY_DATE = LocalDate.now() + + @Autowired TestRestTemplate restTemplate + + def 'receiving future adjustment should result in demand being changed for specific date'() { + given: + productDescriptionIsSuccessfullyCreated(PRODUCT_REF_NO) + when: + callOffDocumentIsSuccessfullyRequested(PRODUCT_REF_NO, ANY_DATE, 100, 200, 300) + then: + thereIsDemand(ANY_DATE.plusDays(1), 200) + when: + adjustmentIsSuccessfullyRequested(ANY_DATE.plusDays(1), 400) + then: + thereIsDemand(ANY_DATE.plusDays(1), 400) + + } + + void productDescriptionIsSuccessfullyCreated(String refNo) { + ResponseEntity response = restTemplate + .postForEntity("/product-descriptions", productDescription(refNo), ProductDescriptionEntity) + assert response.statusCode.is2xxSuccessful() + } + + void callOffDocumentIsSuccessfullyRequested(String refNo, LocalDate date, long ... levels) { + ResponseEntity response = restTemplate + .postForEntity("/demand-documents", documentFor(refNo, date, levels), DocumentEntity) + assert response.statusCode.is2xxSuccessful() + } + + void thereIsDemand(LocalDate date, long expectedLevel) { + Collection demands = + demandsForProductStartingFromDateAreRequested(PRODUCT_REF_NO, ANY_DATE.minusDays(1)) + assert demands.find { it.date == date && it.level == expectedLevel } + } + + Collection demandsForProductStartingFromDateAreRequested(String refNo, LocalDate date) { + ResponseEntity> res = restTemplate + .exchange("/demand-forecasts/search/refNos?refNo={refNo}&date={date}", + HttpMethod.GET, + null, + new ParameterizedTypeReference>() {}, + ["refNo": refNo, "date": date]) + assert res.statusCode.is2xxSuccessful() + return res.getBody().getContent() + } + + void adjustmentIsSuccessfullyRequested(LocalDate date, long levelExpected) { + Map adjustments = [:] + adjustments.put(date, new Adjustment(Demand.of(levelExpected), true)) + ResponseEntity response = restTemplate + .postForEntity("/demand-adjustments", strongAdjustment(PRODUCT_REF_NO, adjustments), DemandAdjustmentEntity) + assert response.statusCode.is2xxSuccessful() + } + + + @Configuration + static class TestConfiguration { + + @Bean + Clock clock() { + return Clock.fixed(from(ANY_DATE), ZoneId.systemDefault()) + } + } +} \ No newline at end of file diff --git a/demand-forecasting-adapters/src/main/java/pl/com/bottega/factory/demand/forecasting/command/DemandAdjustmentEntity.java b/demand-forecasting-adapters/src/main/java/pl/com/bottega/factory/demand/forecasting/command/DemandAdjustmentEntity.java index c74e679..e739e28 100644 --- a/demand-forecasting-adapters/src/main/java/pl/com/bottega/factory/demand/forecasting/command/DemandAdjustmentEntity.java +++ b/demand-forecasting-adapters/src/main/java/pl/com/bottega/factory/demand/forecasting/command/DemandAdjustmentEntity.java @@ -24,6 +24,12 @@ public class DemandAdjustmentEntity implements Serializable { @Convert(converter = AdjustDemandAsJson.class) private AdjustDemand adjustment; + DemandAdjustmentEntity(String note, String customerRepresentative, AdjustDemand adjustment) { + this.note = note; + this.customerRepresentative = customerRepresentative; + this.adjustment = adjustment; + } + @Setter private LocalDate cleanAfter; diff --git a/demand-forecasting-adapters/src/main/java/pl/com/bottega/factory/demand/forecasting/persistence/DocumentEntity.java b/demand-forecasting-adapters/src/main/java/pl/com/bottega/factory/demand/forecasting/persistence/DocumentEntity.java index 67aab6d..99e3db4 100644 --- a/demand-forecasting-adapters/src/main/java/pl/com/bottega/factory/demand/forecasting/persistence/DocumentEntity.java +++ b/demand-forecasting-adapters/src/main/java/pl/com/bottega/factory/demand/forecasting/persistence/DocumentEntity.java @@ -34,7 +34,7 @@ public class DocumentEntity implements Serializable { private LocalDate cleanAfter; public DocumentEntity(String originalUri, String storedUri, Document document) { - saved = Instant.now(); + this.saved = Instant.now(); this.originalUri = originalUri; this.storedUri = storedUri; this.document = document; diff --git a/demand-forecasting-model/src/main/java/pl/com/bottega/factory/demand/forecasting/AdjustDemand.java b/demand-forecasting-model/src/main/java/pl/com/bottega/factory/demand/forecasting/AdjustDemand.java index c54bce8..87e3794 100644 --- a/demand-forecasting-model/src/main/java/pl/com/bottega/factory/demand/forecasting/AdjustDemand.java +++ b/demand-forecasting-model/src/main/java/pl/com/bottega/factory/demand/forecasting/AdjustDemand.java @@ -1,6 +1,6 @@ package pl.com.bottega.factory.demand.forecasting; -import lombok.AllArgsConstructor; +import lombok.Value; import pl.com.bottega.factory.demand.forecasting.DailyDemand.Result; import java.time.LocalDate; @@ -11,7 +11,7 @@ import java.util.Optional; import java.util.function.BiFunction; import java.util.stream.Collectors; -@AllArgsConstructor +@Value public class AdjustDemand { private final String refNo; private final Map adjustments; diff --git a/demand-forecasting-model/src/main/java/pl/com/bottega/factory/demand/forecasting/Adjustment.java b/demand-forecasting-model/src/main/java/pl/com/bottega/factory/demand/forecasting/Adjustment.java index 7d76eb1..4d3dc1f 100644 --- a/demand-forecasting-model/src/main/java/pl/com/bottega/factory/demand/forecasting/Adjustment.java +++ b/demand-forecasting-model/src/main/java/pl/com/bottega/factory/demand/forecasting/Adjustment.java @@ -3,7 +3,7 @@ package pl.com.bottega.factory.demand.forecasting; import lombok.Value; @Value -class Adjustment { +public class Adjustment { Demand demand; boolean strong;