integration test for pushing call of document and requesting current demands

This commit is contained in:
Jakub Pilimon
2018-03-11 13:02:37 +01:00
parent 27f8cbb8fa
commit 179793cc43
3 changed files with 128 additions and 1 deletions

View File

@@ -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<LocalDate, Demand> results = new TreeMap<>()
for (long level : levels) {
results.put(date, Demand.of(level))
date = date.plusDays(1)
}
new Document(created, refNo, results)
}
}

View File

@@ -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<CurrentDemandEntity> 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<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 thereIsDemand(Collection<CurrentDemandEntity> 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())
}
}
}

View File

@@ -1,7 +1,9 @@
package pl.com.bottega.factory.demand.forecasting.projection; 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.RepositoryRestResource;
import org.springframework.data.rest.core.annotation.RestResource; import org.springframework.data.rest.core.annotation.RestResource;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.stereotype.Repository; import org.springframework.stereotype.Repository;
import pl.com.bottega.tools.ProjectionRepository; import pl.com.bottega.tools.ProjectionRepository;
@@ -14,7 +16,7 @@ import java.util.List;
itemResourceRel = "demand-forecast") itemResourceRel = "demand-forecast")
public interface CurrentDemandDao extends ProjectionRepository<CurrentDemandEntity, Long> { public interface CurrentDemandDao extends ProjectionRepository<CurrentDemandEntity, Long> {
@RestResource(path = "refNos", rel = "refNos") @RestResource(path = "refNos", rel = "refNos")
List<CurrentDemandEntity> findByRefNoAndDateGreaterThanEqual(String refNo, LocalDate date); List<CurrentDemandEntity> findByRefNoAndDateGreaterThanEqual(@Param("refNo") String refNo, @Param("date") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date);
@RestResource(exported = false) @RestResource(exported = false)
void deleteByRefNoAndDate(String refNo, LocalDate date); void deleteByRefNoAndDate(String refNo, LocalDate date);