From 5a1ac28c51098dd1e0ffd01fa9ad32c2497b7f25 Mon Sep 17 00:00:00 2001 From: Ralf Ueberfuhr Date: Thu, 20 Jun 2024 14:15:48 +0200 Subject: [PATCH] Prepare Domain for Persistence Layer. --- .../quarkus/domain/CustomersService.java | 25 ++++----- .../domain/CustomersServiceInitializer.java | 12 +++-- .../sample/quarkus/domain/CustomersSink.java | 36 +++++++++++++ .../domain/CustomersSinkInMemoryImpl.java | 52 +++++++++++++++++++ .../quarkus/domain/CustomersServiceTests.java | 11 ++-- 5 files changed, 111 insertions(+), 25 deletions(-) create mode 100644 customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersSink.java create mode 100644 customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersSinkInMemoryImpl.java diff --git a/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersService.java b/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersService.java index 3355393..578a97e 100644 --- a/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersService.java +++ b/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersService.java @@ -1,16 +1,14 @@ package de.schulung.sample.quarkus.domain; import de.schulung.sample.quarkus.domain.events.CustomerCreatedEvent; -import de.schulung.sample.quarkus.shared.interceptors.LogPerformance; import de.schulung.sample.quarkus.shared.interceptors.FireEvent; +import de.schulung.sample.quarkus.shared.interceptors.LogPerformance; import jakarta.enterprise.context.ApplicationScoped; import jakarta.validation.Valid; import jakarta.validation.constraints.NotNull; import lombok.RequiredArgsConstructor; import org.jboss.logging.Logger; -import java.util.HashMap; -import java.util.Map; import java.util.Optional; import java.util.UUID; import java.util.stream.Stream; @@ -19,36 +17,35 @@ @RequiredArgsConstructor public class CustomersService { - private final Map customers = new HashMap<>(); + private final CustomersSink sink; public Stream getAll() { - return this.customers - .values() - .stream(); + return this.sink.findAll(); } public Stream getByState(@NotNull Customer.CustomerState state) { - return this.getAll() - .filter(c -> c.getState() == state); + return this.sink.findByState(state); } @LogPerformance(Logger.Level.DEBUG) @FireEvent(CustomerCreatedEvent.class) public void createCustomer(@Valid Customer customer) { - customer.setUuid(UUID.randomUUID()); - customers.put(customer.getUuid(), customer); + this.sink.save(customer); } public Optional getByUuid(@NotNull UUID uuid) { - return Optional.ofNullable(customers.get(uuid)); + return this.sink.findByUuid(uuid); } public boolean exists(@NotNull UUID uuid) { - return customers.containsKey(uuid); + return this.sink.exists(uuid); } public boolean delete(@NotNull UUID uuid) { - return customers.remove(uuid) != null; + return this.sink.delete(uuid); } + public long count() { + return this.sink.count(); + } } diff --git a/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersServiceInitializer.java b/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersServiceInitializer.java index 539ef75..9eca37a 100644 --- a/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersServiceInitializer.java +++ b/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersServiceInitializer.java @@ -61,11 +61,13 @@ public static class InitialCustomerConfigProvider { @Startup public void initializeData() { - var customer = Customer.builder() - .name(customerConfig.getName()) - .birthdate(LocalDate.parse(customerConfig.getBirthdate())) - .build(); - service.createCustomer(customer); + if(service.count() < 1) { + var customer = Customer.builder() + .name(customerConfig.getName()) + .birthdate(LocalDate.parse(customerConfig.getBirthdate())) + .build(); + service.createCustomer(customer); + } } } diff --git a/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersSink.java b/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersSink.java new file mode 100644 index 0000000..afd0bc5 --- /dev/null +++ b/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersSink.java @@ -0,0 +1,36 @@ +package de.schulung.sample.quarkus.domain; + +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Stream; + +public interface CustomersSink { + + Stream findAll(); + + default Stream findByState(Customer.CustomerState state) { + return this + .findAll() + .filter(c -> c.getState() == state); + } + + default Optional findByUuid(UUID uuid) { + return this + .findAll() + .filter(c -> c.getUuid().equals(uuid)) + .findFirst(); + } + + void save(Customer customer); + + boolean delete(UUID uuid); + + default boolean exists(UUID uuid) { + return this + .findByUuid(uuid) + .isPresent(); + } + + long count(); + +} diff --git a/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersSinkInMemoryImpl.java b/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersSinkInMemoryImpl.java new file mode 100644 index 0000000..840eca5 --- /dev/null +++ b/customer-api-provider/src/main/java/de/schulung/sample/quarkus/domain/CustomersSinkInMemoryImpl.java @@ -0,0 +1,52 @@ +package de.schulung.sample.quarkus.domain; + +import io.quarkus.arc.DefaultBean; +import jakarta.enterprise.context.ApplicationScoped; +import jakarta.enterprise.inject.Typed; + +import java.util.HashMap; +import java.util.Map; +import java.util.Optional; +import java.util.UUID; +import java.util.stream.Stream; + +@ApplicationScoped +@Typed(CustomersSink.class) +@DefaultBean +public class CustomersSinkInMemoryImpl implements CustomersSink { + + private final Map customers = new HashMap<>(); + + @Override + public Stream findAll() { + return this.customers + .values() + .stream(); + } + + @Override + public Optional findByUuid(UUID uuid) { + return Optional.ofNullable(customers.get(uuid)); + } + + @Override + public void save(Customer customer) { + customer.setUuid(UUID.randomUUID()); + customers.put(customer.getUuid(), customer); + } + + @Override + public boolean delete(UUID uuid) { + return customers.remove(uuid) != null; + } + + @Override + public boolean exists(UUID uuid) { + return customers.containsKey(uuid); + } + + @Override + public long count() { + return customers.size(); + } +} diff --git a/customer-api-provider/src/test/java/de/schulung/sample/quarkus/domain/CustomersServiceTests.java b/customer-api-provider/src/test/java/de/schulung/sample/quarkus/domain/CustomersServiceTests.java index 0609e88..115869d 100644 --- a/customer-api-provider/src/test/java/de/schulung/sample/quarkus/domain/CustomersServiceTests.java +++ b/customer-api-provider/src/test/java/de/schulung/sample/quarkus/domain/CustomersServiceTests.java @@ -1,23 +1,22 @@ package de.schulung.sample.quarkus.domain; import org.junit.jupiter.api.Test; -import org.junit.jupiter.api.extension.ExtendWith; -import org.mockito.InjectMocks; -import org.mockito.junit.jupiter.MockitoExtension; import java.time.LocalDate; import java.time.Month; import static org.assertj.core.api.Assertions.assertThat; -@ExtendWith(MockitoExtension.class) class CustomersServiceTests { + // TODO ist das noch sinnvoll so? + // nicht mehr notwendig wegen @FireEvent interceptor //@Mock //Event event; - @InjectMocks - CustomersService service; + private final CustomersService service = new CustomersService( + new CustomersSinkInMemoryImpl() + ); @Test void shouldAddUuidToNewCustomer() {