Skip to content

Commit

Permalink
test(rent-service): add test for optimistic locking
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzo.corso committed Mar 27, 2024
1 parent b30d361 commit b0459ae
Showing 1 changed file with 17 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package it.vincenzocorso.carsharing.rentservice.adapters.persistence.jpa;

import it.vincenzocorso.carsharing.common.messaging.events.ResultWithEvents;
import it.vincenzocorso.carsharing.rentservice.adapters.persistence.RentWrapper;
import it.vincenzocorso.carsharing.rentservice.domain.models.RandomRent;
import it.vincenzocorso.carsharing.rentservice.domain.models.Rent;
Expand All @@ -23,7 +24,9 @@
import java.util.stream.Stream;

import static it.vincenzocorso.carsharing.rentservice.domain.models.RandomRent.randomRent;
import static it.vincenzocorso.carsharing.rentservice.domain.models.RentState.PENDING;
import static org.assertj.core.api.Assertions.assertThat;
import static org.assertj.core.api.Assertions.assertThatException;
import static org.junit.jupiter.api.Assertions.*;

@SpringBootTest
Expand Down Expand Up @@ -139,9 +142,22 @@ void shouldFindRentsByPagination() {
assertThat(retrievedRentsIds).hasSize(savedRents.size() - 1);
}

@Test
void shouldThrowExceptionWhenOptimisticLockFails() {
Rent rent = this.rentJPARepositoryAdapter.save(randomRent(PENDING));
Rent loadedRent1 = this.rentJPARepositoryAdapter.findById(rent.getId()).orElseThrow();
Rent loadedRent2 = this.rentJPARepositoryAdapter.findById(rent.getId()).orElseThrow();
loadedRent1.accept();
loadedRent2.reject();

Rent savedRent = this.rentJPARepositoryAdapter.save(loadedRent1);
assertThatException().isThrownBy(() -> this.rentJPARepositoryAdapter.save(loadedRent2));
assertThat(((RentWrapper)savedRent).getVersion()).isEqualTo(((RentWrapper)loadedRent1).getVersion() + 1);
}

private List<Rent> initializeRentsTable() {
RentEntityMapper rentMapper = new RentEntityMapper();
return Stream.of(RentState.PENDING, RentState.STARTED, RentState.ENDED)
return Stream.of(PENDING, RentState.STARTED, RentState.ENDED)
.map(RandomRent::randomRent)
.peek(rent -> rent.setId(null))
.map(rentMapper::convertToEntity)
Expand Down

0 comments on commit b0459ae

Please sign in to comment.