integration test for adjusting demand + minor cleanup

This commit is contained in:
Jakub Pilimon
2018-03-11 17:09:11 +01:00
parent 99fce4e4f4
commit 33fffa777c
6 changed files with 118 additions and 5 deletions

View File

@@ -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<LocalDate, Adjustment> adjustments) {
return new DemandAdjustmentEntity(refNo, "agent", new AdjustDemand(refNo, adjustments))
}
}

View File

@@ -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<CurrentDemandEntity> demands =
demandsForProductStartingFromDateAreRequested(PRODUCT_REF_NO, ANY_DATE.minusDays(1))
assert demands.find { it.date == date && it.level == expectedLevel }
}
Collection<CurrentDemandEntity> demandsForProductStartingFromDateAreRequested(String refNo, LocalDate date) {
ResponseEntity<Resources<CurrentDemandEntity>> res = restTemplate
.exchange("/demand-forecasts/search/refNos?refNo={refNo}&date={date}",
HttpMethod.GET,
null,
new ParameterizedTypeReference<Resources<CurrentDemandEntity>>() {},
["refNo": refNo, "date": date])
assert res.statusCode.is2xxSuccessful()
return res.getBody().getContent()
}
void adjustmentIsSuccessfullyRequested(LocalDate date, long levelExpected) {
Map<LocalDate, Adjustment> 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())
}
}
}

View File

@@ -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;

View File

@@ -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;

View File

@@ -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<LocalDate, Adjustment> adjustments;

View File

@@ -3,7 +3,7 @@ package pl.com.bottega.factory.demand.forecasting;
import lombok.Value;
@Value
class Adjustment {
public class Adjustment {
Demand demand;
boolean strong;