Skip to content
This repository has been archived by the owner on Jul 26, 2024. It is now read-only.

Commit

Permalink
Prepare Domain for Persistence Layer.
Browse files Browse the repository at this point in the history
  • Loading branch information
Ralf Ueberfuhr committed Jun 20, 2024
1 parent c112f4b commit 5a1ac28
Show file tree
Hide file tree
Showing 5 changed files with 111 additions and 25 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -19,36 +17,35 @@
@RequiredArgsConstructor
public class CustomersService {

private final Map<UUID, Customer> customers = new HashMap<>();
private final CustomersSink sink;

public Stream<Customer> getAll() {
return this.customers
.values()
.stream();
return this.sink.findAll();
}

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

}
Original file line number Diff line number Diff line change
@@ -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<Customer> findAll();

default Stream<Customer> findByState(Customer.CustomerState state) {
return this
.findAll()
.filter(c -> c.getState() == state);
}

default Optional<Customer> 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();

}
Original file line number Diff line number Diff line change
@@ -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<UUID, Customer> customers = new HashMap<>();

@Override
public Stream<Customer> findAll() {
return this.customers
.values()
.stream();
}

@Override
public Optional<Customer> 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();
}
}
Original file line number Diff line number Diff line change
@@ -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<CustomerCreatedEvent> event;
@InjectMocks
CustomersService service;
private final CustomersService service = new CustomersService(
new CustomersSinkInMemoryImpl()
);

@Test
void shouldAddUuidToNewCustomer() {
Expand Down

0 comments on commit 5a1ac28

Please sign in to comment.