Skip to content

Commit

Permalink
refactor(rent-service): remove hard coded test data
Browse files Browse the repository at this point in the history
  • Loading branch information
vincenzo.corso committed Mar 26, 2024
1 parent 691e55d commit b30d361
Show file tree
Hide file tree
Showing 12 changed files with 418 additions and 277 deletions.
2 changes: 2 additions & 0 deletions rent-service/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ dependencies {
implementation 'org.postgresql:postgresql:42.2.23'
annotationProcessor 'org.hibernate:hibernate-jpamodelgen:6.1.6.Final'

testImplementation 'io.github.benas:random-beans:3.9.0'

intTestImplementation 'org.testcontainers:testcontainers:1.16.0'
intTestImplementation 'org.testcontainers:junit-jupiter:1.16.0'
intTestImplementation 'org.testcontainers:postgresql:1.16.0'
Expand Down
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package it.vincenzocorso.carsharing.rentservice.adapters.persistence.jpa;

import it.vincenzocorso.carsharing.rentservice.adapters.persistence.RentWrapper;
import it.vincenzocorso.carsharing.rentservice.domain.FakeRent;
import it.vincenzocorso.carsharing.rentservice.domain.models.RandomRent;
import it.vincenzocorso.carsharing.rentservice.domain.models.Rent;
import it.vincenzocorso.carsharing.rentservice.domain.models.RentState;
import it.vincenzocorso.carsharing.rentservice.domain.models.SearchRentCriteria;
Expand All @@ -15,13 +15,14 @@
import org.testcontainers.junit.jupiter.Container;
import org.testcontainers.junit.jupiter.Testcontainers;

import java.util.Collections;
import java.util.List;
import java.util.Optional;
import java.util.UUID;
import java.util.stream.Collectors;
import java.util.stream.Stream;

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

Expand Down Expand Up @@ -54,18 +55,23 @@ void cleanUp() {

@Test
void shouldSave() {
Rent rent = rentInState(RentState.STARTED);
Rent rent = randomRent();
rent.setId(null);

Rent savedRent = this.rentJPARepositoryAdapter.save(rent);
rent.setId(savedRent.getId());

assertThat(this.rentRepository.count()).isEqualTo(1);
assertThat(savedRent).isInstanceOf(RentWrapper.class);
assertEqualsWithRent(rent, savedRent);
assertThat(((RentWrapper)savedRent).getVersion()).isZero();
assertThat(savedRent)
.usingRecursiveComparison()
.ignoringFields("id", "version")
.isEqualTo(rent);
}

@Test
void shouldGenerateUUID() {
Rent newRent = rentInState(RentState.PENDING);
Rent newRent = randomRent();
newRent.setId(null);

Rent savedRent = this.rentJPARepositoryAdapter.save(newRent);
Expand All @@ -86,14 +92,16 @@ void shouldFindAllRents() {

@Test
void shouldFindRentById() {
Rent savedRent = this.rentJPARepositoryAdapter.save(rentInState(RentState.STARTED));
Rent savedRent = this.rentJPARepositoryAdapter.save(randomRent());

Optional<Rent> optionalRent = this.rentJPARepositoryAdapter.findById(savedRent.getId());

assertTrue(optionalRent.isPresent());
Rent retrievedRent = optionalRent.get();
assertThat(retrievedRent).isInstanceOf(RentWrapper.class);
assertEqualsWithRent(savedRent, retrievedRent);
assertThat(retrievedRent)
.usingRecursiveComparison()
.isEqualTo(savedRent);
}

@Test
Expand All @@ -111,8 +119,10 @@ void shouldFindRentsByStateCriteria() {
@Test
void shouldFindRentsByCustomerIdCriteria() {
List<Rent> savedRents = this.initializeRentsTable();
List<String> expectedRentsIds = savedRents.stream().map(Rent::getId).collect(Collectors.toList());
SearchRentCriteria searchCriteria = SearchRentCriteria.builder().customerId(CUSTOMER_ID).build();
Collections.shuffle(savedRents);
Rent expectedRent = savedRents.get(0);
List<String> expectedRentsIds = Stream.of(expectedRent).map(Rent::getId).toList();
SearchRentCriteria searchCriteria = SearchRentCriteria.builder().customerId(expectedRent.getDetails().customerId()).build();

List<String> retrievedRentsIds = this.rentJPARepositoryAdapter.findByCriteria(searchCriteria).stream().map(Rent::getId).collect(Collectors.toList());

Expand All @@ -132,7 +142,7 @@ void shouldFindRentsByPagination() {
private List<Rent> initializeRentsTable() {
RentEntityMapper rentMapper = new RentEntityMapper();
return Stream.of(RentState.PENDING, RentState.STARTED, RentState.ENDED)
.map(FakeRent::rentInState)
.map(RandomRent::randomRent)
.peek(rent -> rent.setId(null))
.map(rentMapper::convertToEntity)
.map(this.rentRepository::save)
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,147 @@
package it.vincenzocorso.carsharing.rentservice.adapters.persistence.jpa;

import io.github.benas.randombeans.EnhancedRandomBuilder;
import io.github.benas.randombeans.api.EnhancedRandom;
import it.vincenzocorso.carsharing.rentservice.adapters.persistence.RentWrapper;
import it.vincenzocorso.carsharing.rentservice.domain.models.Rent;
import it.vincenzocorso.carsharing.rentservice.domain.models.RentState;
import it.vincenzocorso.carsharing.rentservice.domain.models.RentStateTransition;

import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneOffset;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

import static it.vincenzocorso.carsharing.rentservice.domain.models.RandomRent.randomRent;
import static it.vincenzocorso.carsharing.rentservice.domain.models.RentState.*;
import static it.vincenzocorso.carsharing.rentservice.domain.models.RentState.STARTED;
import static java.time.temporal.ChronoUnit.MINUTES;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class RandomRentEntity {
private static final EnhancedRandom enhancedRandom = EnhancedRandomBuilder.aNewEnhancedRandom();

public static RentWrapper randomRentWrapper() {
Rent rent = randomRent();
Long version = enhancedRandom.nextObject(Long.class);
return new RentWrapper(rent.getId(), rent.getDetails(), rent.getStateTransitions(), version);
}

public static RentEntity randomRentEntity() {
RentState finalState = enhancedRandom.nextObject(RentState.class);

RentEntity rentEntity = new RentEntity();
rentEntity.setId(UUID.randomUUID());
rentEntity.setCustomerId(enhancedRandom.nextObject(String.class));
rentEntity.setVehicleId(enhancedRandom.nextObject(String.class));
rentEntity.setCurrentState(finalState.toString());
rentEntity.setVersion(enhancedRandom.nextObject(Long.class));
rentEntity.setStateTransitions(randomRentStateTransitionEntityList(rentEntity, finalState));
return rentEntity;
}

public static List<RentStateTransitionEntity> randomRentStateTransitionEntityList(RentEntity rentEntity, RentState finalState) {
List<RentStateTransitionEntity> transitions = new ArrayList<>();
int sequenceNumber = 1;

RentStateTransitionEntity t = new RentStateTransitionEntity();
t.setId(new RentStateTransitionId(rentEntity, sequenceNumber));
t.setState(PENDING.toString());
t.setTimestamp(getFutureInstant(sequenceNumber++));
transitions.add(t);
if(PENDING.equals(finalState)) {
return transitions;
}

if(ACCEPTED.equals(finalState) || REJECTED.equals(finalState)) {
t = new RentStateTransitionEntity();
t.setId(new RentStateTransitionId(rentEntity, sequenceNumber));
t.setState(finalState.toString());
t.setTimestamp(getFutureInstant(sequenceNumber));
transitions.add(t);
return transitions;
}
t = new RentStateTransitionEntity();
t.setId(new RentStateTransitionId(rentEntity, sequenceNumber));
t.setState(ACCEPTED.toString());
t.setTimestamp(getFutureInstant(sequenceNumber++));
transitions.add(t);

if(CANCELLED.equals(finalState)) {
t = new RentStateTransitionEntity();
t.setId(new RentStateTransitionId(rentEntity, sequenceNumber));
t.setState(finalState.toString());
t.setTimestamp(getFutureInstant(sequenceNumber));
transitions.add(t);
return transitions;
}

t = new RentStateTransitionEntity();
t.setId(new RentStateTransitionId(rentEntity, sequenceNumber));
t.setState(STARTED.toString());
t.setTimestamp(getFutureInstant(sequenceNumber++));
transitions.add(t);
if(STARTED.equals(finalState)) {
return transitions;
}

t = new RentStateTransitionEntity();
t.setId(new RentStateTransitionId(rentEntity, sequenceNumber));
t.setState(ENDED.toString());
t.setTimestamp(getFutureInstant(sequenceNumber));
transitions.add(t);

return transitions;
}

private static LocalDateTime getFutureInstant(int sequenceNumber) {
return LocalDateTime.ofInstant(Instant.now()
.minus(10 + (long) (Math.random() * 3000), MINUTES)
.plus(sequenceNumber, MINUTES), ZoneOffset.UTC);
}

public static void assertEqualsWithRent(Rent expected, RentEntity actual) {
assertEquals(expected.getId(), actual.getId().toString());
assertEquals(expected.getDetails().customerId(), actual.getCustomerId());
assertEquals(expected.getDetails().vehicleId(), actual.getVehicleId());
assertEqualsWithRentStateTransitionList(expected.getStateTransitions(), actual.getStateTransitions());
if (expected instanceof RentWrapper r) {
assertEquals(r.getVersion(), actual.getVersion());
}
}

public static void assertEqualsWithRentStateTransitionList(List<RentStateTransition> expected, List<RentStateTransitionEntity> actual) {
assertEquals(expected.size(), actual.size());
for(int i = 0; i < expected.size(); i++) {
RentStateTransition stateTransition = expected.get(i);
RentStateTransitionEntity stateTransitionEntity = actual.get(i);

assertEquals(stateTransition.getTimestamp(), stateTransitionEntity.getTimestamp().toInstant(ZoneOffset.UTC));
assertEquals(stateTransition.getState().toString(), stateTransitionEntity.getState());
assertEquals(stateTransition.getSequenceNumber(), stateTransitionEntity.getId().getSequenceNumber());
}
}

public static void assertEqualsWithRentEntity(RentEntity expected, Rent actual) {
assertEquals(expected.getId().toString(), actual.getId());
assertEquals(expected.getCustomerId(), actual.getDetails().customerId());
assertEquals(expected.getVehicleId(), actual.getDetails().vehicleId());
assertEquals(expected.getCurrentState(), actual.getStateTransitions().get(actual.getStateTransitions().size() - 1).getState().toString());
assertEquals(expected.getVersion(), actual instanceof RentWrapper r ? r.getVersion() : null);
assertEqualsWithRentStateTransitionEntityList(expected.getStateTransitions(), actual.getStateTransitions());
}

public static void assertEqualsWithRentStateTransitionEntityList(List<RentStateTransitionEntity> expected, List<RentStateTransition> actual) {
assertEquals(expected.size(), actual.size());
for(int i = 0; i < expected.size(); i++) {
RentStateTransitionEntity stateTransitionEntity = expected.get(i);
RentStateTransition stateTransition = actual.get(i);

assertEquals(stateTransitionEntity.getId().getSequenceNumber(), stateTransition.getSequenceNumber());
assertEquals(stateTransitionEntity.getTimestamp(), LocalDateTime.ofInstant(stateTransition.getTimestamp(), ZoneOffset.UTC));
assertEquals(stateTransitionEntity.getState(), stateTransition.getState().toString());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
import it.vincenzocorso.carsharing.rentservice.domain.models.Rent;
import org.junit.jupiter.api.Test;

import static it.vincenzocorso.carsharing.rentservice.adapters.persistence.jpa.FakeRentEntity.*;
import static it.vincenzocorso.carsharing.rentservice.domain.FakeRent.*;
import static it.vincenzocorso.carsharing.rentservice.adapters.persistence.jpa.RandomRentEntity.*;
import static it.vincenzocorso.carsharing.rentservice.domain.models.RandomRent.randomRent;
import static org.assertj.core.api.Assertions.*;
import static org.junit.jupiter.api.Assertions.*;

Expand All @@ -14,25 +14,29 @@ class RentEntityMapperTest {

@Test
void shouldConvertToEntity() {
RentEntity rentEntity = this.rentEntityMapper.convertToEntity(RENT);
Rent rent = randomRent();

FakeRentEntity.assertEqualsWithRent(rentEntity);
assertNull(rentEntity.getVersion());
RentEntity rentEntity = this.rentEntityMapper.convertToEntity(rent);

assertEqualsWithRent(rent, rentEntity);
}

@Test
void shouldSetEntityVersion() {
RentEntity rentEntity = this.rentEntityMapper.convertToEntity(RENT_WRAPPER);
RentWrapper rentWrapper = randomRentWrapper();

RentEntity rentEntity = this.rentEntityMapper.convertToEntity(rentWrapper);

assertEquals(RENT_VERSION, rentEntity.getVersion());
assertEquals(rentWrapper.getVersion(), rentEntity.getVersion());
}

@Test
void shouldConvertFromEntity() {
Rent rent = this.rentEntityMapper.convertFromEntity(RENT_ENTITY);
RentEntity rentEntity = randomRentEntity();

Rent rent = this.rentEntityMapper.convertFromEntity(rentEntity);

assertThat(rent).isInstanceOf(RentWrapper.class);
assertEqualsWithRent(rent);
assertEquals(RENT_VERSION, ((RentWrapper)rent).getVersion());
assertEqualsWithRentEntity(rentEntity, rent);
}
}
Loading

0 comments on commit b30d361

Please sign in to comment.