Navigation Menu

Skip to content

Commit

Permalink
Introduce example value object implemented as record.
Browse files Browse the repository at this point in the history
  • Loading branch information
odrotbohm committed Feb 3, 2022
1 parent 43d116a commit 4e60942
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 5 deletions.
Expand Up @@ -24,6 +24,7 @@

import org.jmolecules.ddd.types.AggregateRoot;
import org.jmolecules.ddd.types.Identifier;
import org.jmolecules.ddd.types.ValueObject;
import org.jmolecules.examples.jpa.customer.Customer.CustomerId;
import org.springframework.util.Assert;

Expand All @@ -34,7 +35,7 @@
public class Customer implements AggregateRoot<Customer, CustomerId> {

private final CustomerId id;
private String firstname, lastname;
private Name name;
private List<Address> addresses;

public Customer(String firstname, String lastname, Address address) {
Expand All @@ -43,13 +44,13 @@ public Customer(String firstname, String lastname, Address address) {

this.id = CustomerId.of(UUID.randomUUID().toString());

this.firstname = firstname;
this.lastname = lastname;

this.name = new Name(firstname, lastname);
this.addresses = new ArrayList<>();
this.addresses.add(address);
}

public record Name(String firstname, String lastname) implements ValueObject {}

@Value(staticConstructor = "of")
public static class CustomerId implements Identifier {
private final String id;
Expand Down
Expand Up @@ -17,6 +17,7 @@

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

import jakarta.persistence.EntityManager;
import lombok.RequiredArgsConstructor;

import org.jmolecules.examples.jpa.customer.Address;
Expand All @@ -29,15 +30,20 @@
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.boot.test.context.assertj.AssertableApplicationContext;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.test.context.TestConstructor;
import org.springframework.test.context.TestConstructor.AutowireMode;
import org.springframework.transaction.annotation.Transactional;

/**
* @author Oliver Drotbohm
*/
@SpringBootTest
@TestConstructor(autowireMode = AutowireMode.ALL)
@RequiredArgsConstructor
class ApplicationIntegrationTests {

private final ConfigurableApplicationContext context;
private final EntityManager em;

@Test
void bootstrapsContainer() {
Expand All @@ -55,6 +61,7 @@ void bootstrapsContainer() {
}

@Test // #24
@Transactional
void exposesPersistenceComponents() {

var address = new Address("41 Greystreet", "Dreaming Tree", "2731");
Expand All @@ -65,6 +72,12 @@ void exposesPersistenceComponents() {
var orders = context.getBean(Orders.class);
var order = orders.save(new Order(customer));

customers.resolveRequired(order.getCustomer());
em.flush();
em.clear();

var resolved = customers.resolveRequired(order.getCustomer());

assertThat(resolved.getName().firstname()).isEqualTo("Dave");
assertThat(resolved.getName().lastname()).isEqualTo("Matthews");
}
}

0 comments on commit 4e60942

Please sign in to comment.