From dc4b79ecfc9df8e7311053600cd1a78f6751da09 Mon Sep 17 00:00:00 2001 From: Jakub Pilimon Date: Sun, 11 Mar 2018 18:24:48 +0100 Subject: [PATCH 1/3] bugfix -> tests did not include proper config --- .../integration/CallOffDocumentIntegrationSpec.groovy | 7 ++++--- .../integration/DemandAdjustmentIntegrationSpec.groovy | 6 ++++-- 2 files changed, 8 insertions(+), 5 deletions(-) diff --git a/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/CallOffDocumentIntegrationSpec.groovy b/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/CallOffDocumentIntegrationSpec.groovy index f897e6c..a866d8e 100644 --- a/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/CallOffDocumentIntegrationSpec.groovy +++ b/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/CallOffDocumentIntegrationSpec.groovy @@ -22,14 +22,15 @@ import java.time.LocalDate import java.time.ZoneId import static java.time.Instant.from +import static java.time.ZoneId.systemDefault import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT @IntegrationTest -@SpringBootTest(webEnvironment = RANDOM_PORT, classes = AppConfiguration) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = [AppConfiguration, TestConfiguration]) class CallOffDocumentIntegrationSpec extends Specification implements ProductTrait { public static final String PRODUCT_REF_NO = "3009000" - public static final LocalDate ANY_DATE = LocalDate.now() + public static final LocalDate ANY_DATE = LocalDate.of(2018, 1, 1) @Autowired TestRestTemplate restTemplate @@ -80,7 +81,7 @@ class CallOffDocumentIntegrationSpec extends Specification implements ProductTra @Bean Clock clock() { - return Clock.fixed(from(ANY_DATE), ZoneId.systemDefault()) + return Clock.fixed(from(ANY_DATE.atStartOfDay().atZone(systemDefault())), systemDefault()) } } } \ No newline at end of file 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 index 9c93caa..ff8c4e1 100644 --- 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 @@ -2,6 +2,7 @@ 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.context.TestConfiguration import org.springframework.boot.test.web.client.TestRestTemplate import org.springframework.context.annotation.Bean import org.springframework.context.annotation.Configuration @@ -25,10 +26,11 @@ import java.time.LocalDate import java.time.ZoneId import static java.time.Instant.from +import static java.time.ZoneId.systemDefault import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT @IntegrationTest -@SpringBootTest(webEnvironment = RANDOM_PORT, classes = AppConfiguration) +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = [AppConfiguration, TestConfiguration]) class DemandAdjustmentIntegrationSpec extends Specification implements ProductTrait { public static final String PRODUCT_REF_NO = "3009001" @@ -93,7 +95,7 @@ class DemandAdjustmentIntegrationSpec extends Specification implements ProductTr @Bean Clock clock() { - return Clock.fixed(from(ANY_DATE), ZoneId.systemDefault()) + return Clock.fixed(from(ANY_DATE.atStartOfDay().atZone(systemDefault())), systemDefault()) } } } \ No newline at end of file From a74c3c5627d3ee0e61552cd2e2298e5d5bcabb93 Mon Sep 17 00:00:00 2001 From: Jakub Pilimon Date: Sun, 11 Mar 2018 18:25:21 +0100 Subject: [PATCH 2/3] integration test for shortage prediction --- .../ShortageIntegrationSpec.groovy | 102 ++++++++++++++++++ .../monitoring/persistence/ShortagesDao.java | 3 +- 2 files changed, 104 insertions(+), 1 deletion(-) create mode 100644 app-monolith/src/test/groovy/pl/com/bottega/factory/integration/ShortageIntegrationSpec.groovy diff --git a/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/ShortageIntegrationSpec.groovy b/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/ShortageIntegrationSpec.groovy new file mode 100644 index 0000000..f06d817 --- /dev/null +++ b/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/ShortageIntegrationSpec.groovy @@ -0,0 +1,102 @@ +package src.test.groovy.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.context.annotation.Primary +import org.springframework.core.ParameterizedTypeReference +import org.springframework.hateoas.Resource +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.factory.shortages.prediction.calculation.Stock +import pl.com.bottega.factory.shortages.prediction.monitoring.persistence.ShortagesEntity +import pl.com.bottega.factory.warehouse.WarehouseService +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 java.time.ZoneId.systemDefault +import static org.springframework.boot.test.context.SpringBootTest.WebEnvironment.RANDOM_PORT + +@IntegrationTest +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = [AppConfiguration, TestConfiguration]) +class ShortageIntegrationSpec extends Specification implements ProductTrait { + + public static final String PRODUCT_REF_NO = "3009003" + public static final LocalDate ANY_DATE = LocalDate.now() + public static final int PRODUCT_STOCK_LEVEL = 100 + + @Autowired TestRestTemplate restTemplate + + def 'adjustment that exceeds current stock level should result in shortage'() { + given: + productDescriptionIsSuccessfullyCreated(PRODUCT_REF_NO) + when: + callOffDocumentIsSuccessfullyRequested(PRODUCT_REF_NO, ANY_DATE, PRODUCT_STOCK_LEVEL) + and: + adjustmentIsSuccessfullyRequested(ANY_DATE.plusDays(1), 200) + then: + thereIsShortage(PRODUCT_REF_NO, ANY_DATE.plusDays(1), 200) + + } + + 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 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() + } + + void thereIsShortage(String refNo, LocalDate forDate, long expectedShortage) { + ResponseEntity> res = restTemplate + .exchange("/shortages/search/refNos?refNo={refNo}", + HttpMethod.GET, + null, + new ParameterizedTypeReference>() {}, + ["refNo": refNo]) + assert res.statusCode.is2xxSuccessful() + } + + + @Configuration + static class TestConfiguration { + + @Bean + Clock clock() { + return Clock.fixed(from(ANY_DATE.atStartOfDay().atZone(systemDefault())), systemDefault()) + } + + @Bean + WarehouseService warehouseService() { + return { refNo -> new Stock(PRODUCT_STOCK_LEVEL, 0) } + } + } +} \ No newline at end of file diff --git a/shortages-prediction-adapters/src/main/java/pl/com/bottega/factory/shortages/prediction/monitoring/persistence/ShortagesDao.java b/shortages-prediction-adapters/src/main/java/pl/com/bottega/factory/shortages/prediction/monitoring/persistence/ShortagesDao.java index c2dce1f..0b8ce3d 100644 --- a/shortages-prediction-adapters/src/main/java/pl/com/bottega/factory/shortages/prediction/monitoring/persistence/ShortagesDao.java +++ b/shortages-prediction-adapters/src/main/java/pl/com/bottega/factory/shortages/prediction/monitoring/persistence/ShortagesDao.java @@ -1,5 +1,6 @@ package pl.com.bottega.factory.shortages.prediction.monitoring.persistence; +import org.springframework.data.repository.query.Param; import org.springframework.data.rest.core.annotation.RepositoryRestResource; import org.springframework.data.rest.core.annotation.RestResource; import org.springframework.stereotype.Repository; @@ -13,7 +14,7 @@ import java.util.Optional; itemResourceRel = "shortage") public interface ShortagesDao extends ProjectionRepository { @RestResource(path = "refNos", rel = "refNos") - Optional findByRefNo(String refNo); + Optional findByRefNo(@Param("refNo") String refNo); void deleteAllInBatch(); } From 32601d705af7653a44573cf524138efbdd552262 Mon Sep 17 00:00:00 2001 From: Jakub Pilimon Date: Sun, 11 Mar 2018 18:27:32 +0100 Subject: [PATCH 3/3] accidental code reverted --- .../factory/integration/CallOffDocumentIntegrationSpec.groovy | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/CallOffDocumentIntegrationSpec.groovy b/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/CallOffDocumentIntegrationSpec.groovy index a866d8e..ff0a8b9 100644 --- a/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/CallOffDocumentIntegrationSpec.groovy +++ b/app-monolith/src/test/groovy/pl/com/bottega/factory/integration/CallOffDocumentIntegrationSpec.groovy @@ -30,7 +30,7 @@ import static org.springframework.boot.test.context.SpringBootTest.WebEnvironmen class CallOffDocumentIntegrationSpec extends Specification implements ProductTrait { public static final String PRODUCT_REF_NO = "3009000" - public static final LocalDate ANY_DATE = LocalDate.of(2018, 1, 1) + public static final LocalDate ANY_DATE = LocalDate.now() @Autowired TestRestTemplate restTemplate