From 179793cc434a240d7be5da772bd8d29641c39842 Mon Sep 17 00:00:00 2001 From: Jakub Pilimon Date: Sun, 11 Mar 2018 13:02:37 +0100 Subject: [PATCH] integration test for pushing call of document and requesting current demands --- .../com/bottega/factory/ProductTrait.groovy | 36 ++++++++ .../CallOffDocumentIntegrationSpec.groovy | 89 +++++++++++++++++++ .../projection/CurrentDemandDao.java | 4 +- 3 files changed, 128 insertions(+), 1 deletion(-) create mode 100644 app-monolith/src/test/pl/com/bottega/factory/ProductTrait.groovy create mode 100644 app-monolith/src/test/pl/com/bottega/factory/integration/CallOffDocumentIntegrationSpec.groovy diff --git a/app-monolith/src/test/pl/com/bottega/factory/ProductTrait.groovy b/app-monolith/src/test/pl/com/bottega/factory/ProductTrait.groovy new file mode 100644 index 0000000..98c67ee --- /dev/null +++ b/app-monolith/src/test/pl/com/bottega/factory/ProductTrait.groovy @@ -0,0 +1,36 @@ +package src.test.pl.com.bottega.factory + +import pl.com.bottega.factory.demand.forecasting.Demand +import pl.com.bottega.factory.demand.forecasting.Document +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 + +import java.time.Instant +import java.time.LocalDate +import java.time.OffsetTime +import java.time.ZoneOffset + +trait ProductTrait { + + + DocumentEntity documentFor(String refNo, LocalDate date, long ... levels) { + Document document = document(refNo, date, levels) + return new DocumentEntity("uri", "storedUri", document) + } + + ProductDescriptionEntity productDescription(String refNo) { + ProductDescription desc = new ProductDescription("461952398951", ["PROWAD.POJ.NA JARZ.ESSENT"]) + return new ProductDescriptionEntity(refNo, desc) + } + + Document document(String refNo, LocalDate date, long ... levels) { + Instant created = date.atTime(OffsetTime.of(8, 0, 0, 0, ZoneOffset.UTC)).toInstant() + SortedMap results = new TreeMap<>() + for (long level : levels) { + results.put(date, Demand.of(level)) + date = date.plusDays(1) + } + new Document(created, refNo, results) + } +} diff --git a/app-monolith/src/test/pl/com/bottega/factory/integration/CallOffDocumentIntegrationSpec.groovy b/app-monolith/src/test/pl/com/bottega/factory/integration/CallOffDocumentIntegrationSpec.groovy new file mode 100644 index 0000000..a5a065c --- /dev/null +++ b/app-monolith/src/test/pl/com/bottega/factory/integration/CallOffDocumentIntegrationSpec.groovy @@ -0,0 +1,89 @@ +package src.test.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.demand.forecasting.persistence.DocumentEntity +import pl.com.bottega.factory.demand.forecasting.projection.CurrentDemandEntity +import pl.com.bottega.factory.product.management.ProductDescriptionEntity +import spock.lang.Specification +import src.test.pl.com.bottega.factory.ProductTrait + +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 + +@SpringBootTest(webEnvironment = RANDOM_PORT, classes = AppConfiguration) +class CallOffDocumentIntegrationSpec extends Specification implements ProductTrait { + + public static final String PRODUCT_REF_NO = "refNum" + public static final LocalDate ANY_DATE = LocalDate.of(2019, 1, 1) + + @Autowired TestRestTemplate restTemplate + + def 'receiving call off document should create new product demands for subsequent days'() { + given: + productDescriptionIsSuccessfullyCreated(PRODUCT_REF_NO) + when: + callOffDocumentIsSuccessfullyRequested(PRODUCT_REF_NO, ANY_DATE, 100, 200, 300) + and: + Collection demands = + demandsForProductStartingFromDateAreRequested(PRODUCT_REF_NO, ANY_DATE.minusDays(1)) + then: + demands.size() == 3 + thereIsDemand(demands, ANY_DATE, 100) + thereIsDemand(demands, ANY_DATE.plusDays(1), 200) + thereIsDemand(demands, ANY_DATE.plusDays(2), 300) + + } + + 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() + } + + 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 thereIsDemand(Collection demands, LocalDate date, long expectedLevel) { + assert demands.find { it.date == date && it.level == expectedLevel } + } + + @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/projection/CurrentDemandDao.java b/demand-forecasting-adapters/src/main/java/pl/com/bottega/factory/demand/forecasting/projection/CurrentDemandDao.java index cd1a426..85d4fed 100644 --- a/demand-forecasting-adapters/src/main/java/pl/com/bottega/factory/demand/forecasting/projection/CurrentDemandDao.java +++ b/demand-forecasting-adapters/src/main/java/pl/com/bottega/factory/demand/forecasting/projection/CurrentDemandDao.java @@ -1,7 +1,9 @@ package pl.com.bottega.factory.demand.forecasting.projection; +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.format.annotation.DateTimeFormat; import org.springframework.stereotype.Repository; import pl.com.bottega.tools.ProjectionRepository; @@ -14,7 +16,7 @@ import java.util.List; itemResourceRel = "demand-forecast") public interface CurrentDemandDao extends ProjectionRepository { @RestResource(path = "refNos", rel = "refNos") - List findByRefNoAndDateGreaterThanEqual(String refNo, LocalDate date); + List findByRefNoAndDateGreaterThanEqual(@Param("refNo") String refNo, @Param("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date); @RestResource(exported = false) void deleteByRefNoAndDate(String refNo, LocalDate date);