From 53b0a2a84455a1d724eb1bc46d9887d20c877a30 Mon Sep 17 00:00:00 2001 From: niels cappelle Date: Mon, 22 Jul 2024 09:12:53 +0200 Subject: [PATCH 1/2] use Long as type for Version property --- .../data/rest/etag/OptimisticLockingTest.java | 4 ++-- .../invoicing/InvoicingApplicationTests.java | 24 +++++++++++++++++++ .../test/matchers/ETagHeaderMatcher.java | 2 +- .../fixture/invoicing/model/Customer.java | 2 +- .../test/fixture/invoicing/model/Invoice.java | 2 +- .../test/fixture/invoicing/model/Order.java | 4 ++++ 6 files changed, 33 insertions(+), 5 deletions(-) diff --git a/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/data/rest/etag/OptimisticLockingTest.java b/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/data/rest/etag/OptimisticLockingTest.java index 817e0a83..a482710d 100644 --- a/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/data/rest/etag/OptimisticLockingTest.java +++ b/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/data/rest/etag/OptimisticLockingTest.java @@ -57,8 +57,8 @@ public class OptimisticLockingTest { static UUID INVOICE_1_ID; static UUID XENIT_ID; - static int INVOICE_1_VERSION; - static int XENIT_VERSION; + static Long INVOICE_1_VERSION; + static Long XENIT_VERSION; static final ETag INVALID_VERSION = ETag.from("INVALID"); private static final String EXT_ASCII_TEXT = "L'éducation doit être gratuite."; diff --git a/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/test/fixture/invoicing/InvoicingApplicationTests.java b/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/test/fixture/invoicing/InvoicingApplicationTests.java index fc100c41..5eda45c7 100644 --- a/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/test/fixture/invoicing/InvoicingApplicationTests.java +++ b/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/test/fixture/invoicing/InvoicingApplicationTests.java @@ -363,6 +363,30 @@ void createOrder_withPromoCodes_shouldReturn_http201_created() throws Exception }); }); } + + @Test + void createOrder_withMultipartFormData_noContentProperty_http201_created() throws Exception { + var customerId = customers.findByVat(ORG_XENIT_VAT).orElseThrow().getId(); + + var result = mockMvc.perform(multipart(HttpMethod.POST, "/orders") + .param("customer", "/customers/" + customerId) + .contentType(MediaType.MULTIPART_FORM_DATA)) + .andExpect(status().isCreated()) + .andExpect(headers().location().path("/orders/{id}")) + .andReturn(); + + var orderId = Optional.ofNullable(result.getResponse().getHeader(HttpHeaders.LOCATION)) + .map(location -> new UriTemplate("{scheme}://{host}/orders/{id}").match(location)) + .map(matches -> matches.get("id")) + .map(UUID::fromString) + .orElseThrow(); + + doInTransaction(() -> { + assertThat(orders.findById(orderId)).hasValueSatisfying(order -> { + assertThat(order.getCustomer().getId()).isEqualTo(customerId); + }); + }); + } } } diff --git a/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/test/matchers/ETagHeaderMatcher.java b/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/test/matchers/ETagHeaderMatcher.java index 6d726b15..07857565 100644 --- a/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/test/matchers/ETagHeaderMatcher.java +++ b/contentgrid-spring-data-rest/src/test/java/com/contentgrid/spring/test/matchers/ETagHeaderMatcher.java @@ -12,7 +12,7 @@ @RequiredArgsConstructor(access = AccessLevel.PACKAGE) public class ETagHeaderMatcher { - public static ETag toETag(int version) { + public static ETag toETag(long version) { return ETag.from(String.valueOf(version)); } diff --git a/contentgrid-spring-data-rest/src/testFixtures/java/com/contentgrid/spring/test/fixture/invoicing/model/Customer.java b/contentgrid-spring-data-rest/src/testFixtures/java/com/contentgrid/spring/test/fixture/invoicing/model/Customer.java index 5b3871f3..540d7582 100644 --- a/contentgrid-spring-data-rest/src/testFixtures/java/com/contentgrid/spring/test/fixture/invoicing/model/Customer.java +++ b/contentgrid-spring-data-rest/src/testFixtures/java/com/contentgrid/spring/test/fixture/invoicing/model/Customer.java @@ -47,7 +47,7 @@ public class Customer { private UUID id; @Version - private int version; + private Long version = 0L; @JsonProperty(value = "audit_metadata", access = Access.READ_ONLY) @Embedded diff --git a/contentgrid-spring-data-rest/src/testFixtures/java/com/contentgrid/spring/test/fixture/invoicing/model/Invoice.java b/contentgrid-spring-data-rest/src/testFixtures/java/com/contentgrid/spring/test/fixture/invoicing/model/Invoice.java index f9b8f043..51798b63 100644 --- a/contentgrid-spring-data-rest/src/testFixtures/java/com/contentgrid/spring/test/fixture/invoicing/model/Invoice.java +++ b/contentgrid-spring-data-rest/src/testFixtures/java/com/contentgrid/spring/test/fixture/invoicing/model/Invoice.java @@ -51,7 +51,7 @@ public class Invoice { private UUID id; @Version - private int version; + private Long version = 0L; @JsonProperty(value = "audit_metadata", access = Access.READ_ONLY) @Embedded diff --git a/contentgrid-spring-data-rest/src/testFixtures/java/com/contentgrid/spring/test/fixture/invoicing/model/Order.java b/contentgrid-spring-data-rest/src/testFixtures/java/com/contentgrid/spring/test/fixture/invoicing/model/Order.java index 37f892eb..34d5c0d8 100644 --- a/contentgrid-spring-data-rest/src/testFixtures/java/com/contentgrid/spring/test/fixture/invoicing/model/Order.java +++ b/contentgrid-spring-data-rest/src/testFixtures/java/com/contentgrid/spring/test/fixture/invoicing/model/Order.java @@ -6,6 +6,7 @@ import com.fasterxml.jackson.annotation.JsonProperty; import com.fasterxml.jackson.annotation.JsonProperty.Access; import jakarta.persistence.Column; +import jakarta.persistence.Version; import java.util.HashSet; import java.util.Set; import java.util.UUID; @@ -34,6 +35,9 @@ public class Order { @JsonProperty(access = Access.READ_ONLY) private UUID id; + @Version + private Long version = 0L; + @ManyToOne @JoinColumn(name = "customer") @CollectionFilterParam From c33aace2ed95ffb247f87a8ade7d6261f228297f Mon Sep 17 00:00:00 2001 From: niels cappelle Date: Mon, 22 Jul 2024 10:02:18 +0200 Subject: [PATCH 2/2] fix order tests --- .../spring/audit/AuditObservationHandlerTest.java | 8 ++++---- .../events/EntityChangeHibernateEventListenerTest.java | 3 ++- 2 files changed, 6 insertions(+), 5 deletions(-) diff --git a/contentgrid-spring-audit-logging/src/test/java/com/contentgrid/spring/audit/AuditObservationHandlerTest.java b/contentgrid-spring-audit-logging/src/test/java/com/contentgrid/spring/audit/AuditObservationHandlerTest.java index 5f2d745d..b0842e0b 100644 --- a/contentgrid-spring-audit-logging/src/test/java/com/contentgrid/spring/audit/AuditObservationHandlerTest.java +++ b/contentgrid-spring-audit-logging/src/test/java/com/contentgrid/spring/audit/AuditObservationHandlerTest.java @@ -329,11 +329,11 @@ void relationDelete() throws Exception { void relationGetItem(MediaType mediaType) throws Exception { var order = new Order(); order.setCustomer(customerRepository.getReferenceById(CUSTOMER_ID)); - orderRepository.save(order); + var savedOrder = orderRepository.save(order); var invoice = invoiceRepository.findById(INVOICE_ID).orElseThrow(); - invoice.setOrders(Set.of(order)); + invoice.setOrders(Set.of(savedOrder)); invoiceRepository.save(invoice); - mockMvc.perform(MockMvcRequestBuilders.get("/invoices/{id}/orders/{id}", INVOICE_ID, order.getId()) + mockMvc.perform(MockMvcRequestBuilders.get("/invoices/{id}/orders/{id}", INVOICE_ID, savedOrder.getId()) .accept(mediaType) ) .andExpect(MockMvcResultMatchers.status().is3xxRedirection()); @@ -344,7 +344,7 @@ void relationGetItem(MediaType mediaType) throws Exception { assertThat(event.getOperation()).isEqualTo(EntityRelationItemAuditEvent.Operation.READ); assertThat(event.getId()).isEqualTo(INVOICE_ID.toString()); assertThat(event.getRelationName()).isEqualTo("orders"); - assertThat(event.getRelationId()).isEqualTo(order.getId().toString()); + assertThat(event.getRelationId()).isEqualTo(savedOrder.getId().toString()); }); } diff --git a/contentgrid-spring-integration-events/src/test/java/com/contentgrid/spring/integration/events/EntityChangeHibernateEventListenerTest.java b/contentgrid-spring-integration-events/src/test/java/com/contentgrid/spring/integration/events/EntityChangeHibernateEventListenerTest.java index bf973919..8322a2fa 100644 --- a/contentgrid-spring-integration-events/src/test/java/com/contentgrid/spring/integration/events/EntityChangeHibernateEventListenerTest.java +++ b/contentgrid-spring-integration-events/src/test/java/com/contentgrid/spring/integration/events/EntityChangeHibernateEventListenerTest.java @@ -171,7 +171,8 @@ void whenPromoIsAddedToOrderPromos_manyToMany_postUpdateCollectionShouldBeCalled assertThat(testMessageHandler.messages()).satisfiesExactly( checkEvent("create", Order.class), - checkEvent("create", PromotionCampaign.class) + checkEvent("create", PromotionCampaign.class), + checkEvent("update", Order.class) ); }