Skip to content

Commit

Permalink
Merge pull request #254 from xenit-eu/optimistic-locking
Browse files Browse the repository at this point in the history
Use Long as type for @Version properties [ACC-1496]
  • Loading branch information
NielsCW authored Jul 22, 2024
2 parents d1a527d + c33aace commit 01c43e6
Show file tree
Hide file tree
Showing 8 changed files with 39 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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());
Expand All @@ -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());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
});
}
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
);
}

Expand Down

0 comments on commit 01c43e6

Please sign in to comment.