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

Separate Initialization Logic #11

Merged
merged 2 commits into from
Jun 19, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,6 @@
import jakarta.validation.Valid;
import jakarta.validation.constraints.NotNull;

import java.time.LocalDate;
import java.time.Month;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
Expand All @@ -17,15 +15,6 @@ public class CustomersService {

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

{ // TODO replace this?
var customer = Customer.builder()
.uuid(UUID.randomUUID())
.name("Tom")
.birthdate(LocalDate.of(2000, Month.DECEMBER, 6))
.build();
customers.put(customer.getUuid(), customer);
}

public Stream<Customer> getAll() {
return this.customers
.values()
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
package de.schulung.sample.quarkus.domain;

import io.quarkus.runtime.Startup;
import jakarta.enterprise.context.Dependent;
import lombok.RequiredArgsConstructor;

import java.time.LocalDate;
import java.time.Month;

@Dependent // destroy object immediately
@RequiredArgsConstructor
public class CustomersServiceInitializer {

private final CustomersService service;

@Startup
public void initializeData() {
var customer = Customer.builder()
.name("Tom")
.birthdate(LocalDate.of(2000, Month.DECEMBER, 6))
.build();
service.createCustomer(customer);
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package de.schulung.sample.quarkus.domain;

import io.quarkus.test.junit.QuarkusTest;
import jakarta.inject.Inject;
import org.junit.jupiter.api.Test;

import static org.assertj.core.api.Assertions.assertThat;

@QuarkusTest
public class CustomersServiceInitializerTests {

@Inject
CustomersService service;

@Test
void shouldHaveAtLeastOneCustomer() {
assertThat(service.getAll())
.hasAtLeastOneElementOfType(Customer.class);
}


}
Loading