Skip to content
Merged
4 changes: 2 additions & 2 deletions samples/spring-eventsourced-customer-registry/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
<maven.build.timestamp.format>yyyyMMddHHmmss</maven.build.timestamp.format>
<mainClass>customer.Main</mainClass>

<jdk.target>11</jdk.target>
<jdk.target>17</jdk.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<kalix-sdk.version>1.0.8</kalix-sdk.version>
Expand Down Expand Up @@ -123,7 +123,7 @@
<name>${dockerImage}:%l</name>
<build>
<!-- Base Docker image which contains jre-->
<from>docker.io/library/adoptopenjdk:${jdk.target}-jre-hotspot</from>
<from>docker.io/library/eclipse-temurin:${jdk.target}-alpine</from>
<createImageOptions>
<platform>linux/amd64</platform>
</createImageOptions>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ public class CustomerIntegrationTest extends KalixIntegrationTestKitSupport {
private Duration timeout = Duration.of(5, SECONDS);

@Test
public void create() throws InterruptedException {
public void create() {
String id = UUID.randomUUID().toString();
Customer customer = new Customer(id, "[email protected]", "Johanna", null);

Expand All @@ -47,11 +47,11 @@ public void create() throws InterruptedException {
.block(timeout);

Assertions.assertEquals(HttpStatus.OK, response.getStatusCode());
Assertions.assertEquals("Johanna", getCustomerById(id).name);
Assertions.assertEquals("Johanna", getCustomerById(id).name());
}

@Test
public void changeName() throws InterruptedException {
public void changeName() {
String id = UUID.randomUUID().toString();
Customer customer = new Customer(id, "[email protected]", "Johanna", null);

Expand All @@ -74,11 +74,11 @@ public void changeName() throws InterruptedException {


Assertions.assertEquals(HttpStatus.OK, resUpdate.getStatusCode());
Assertions.assertEquals("Katarina", getCustomerById(id).name);
Assertions.assertEquals("Katarina", getCustomerById(id).name());
}

@Test
public void changeAddress() throws InterruptedException {
public void changeAddress() {
String id = UUID.randomUUID().toString();
Customer customer = new Customer(id, "[email protected]", "Johanna", null);

Expand All @@ -103,12 +103,12 @@ public void changeAddress() throws InterruptedException {


Assertions.assertEquals(HttpStatus.OK, resUpdate.getStatusCode());
Assertions.assertEquals("Elm st. 5", getCustomerById(id).address.street);
Assertions.assertEquals("Elm st. 5", getCustomerById(id).address().street());
}


@Test
public void findByName() throws Exception {
public void findByName() {
String id = UUID.randomUUID().toString();
Customer customer = new Customer(id, "[email protected]", "Foo", null);
ResponseEntity<String> response =
Expand All @@ -131,13 +131,13 @@ public void findByName() throws Exception {
.retrieve()
.bodyToMono(CustomerView.class)
.block(timeout)
.name,
.name(),
new IsEqual("Foo")
);
}

@Test
public void findByEmail() throws Exception {
public void findByEmail() {
String id = UUID.randomUUID().toString();
Customer customer = new Customer(id, "[email protected]", "Bar", null);
ResponseEntity<String> response =
Expand All @@ -160,7 +160,7 @@ public void findByEmail() throws Exception {
.retrieve()
.bodyToMono(CustomerView.class)
.block(timeout)
.name,
.name(),
new IsEqual("Bar")
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,4 @@
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
*/

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Address {
public String street;
public String city;

@JsonCreator
public Address(@JsonProperty("street") String street, @JsonProperty("city") String city) {
this.street = street;
this.city = city;
}
}
public record Address(String street, String city) {}

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -3,30 +3,7 @@
* Copyright (C) 2009-2022 Lightbend Inc. <https://www.lightbend.com>
*/

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;

public class Customer {
public String customerId;
public String email;
public String name;
public Address address;

// TODO: remove JsonCreator and JsonProperty
// this should not be needed and it's not when running the application
// however, the integration tests seems to need it.
// Probably related to how the compiler is configured for the tests?
@JsonCreator
public Customer(@JsonProperty("customerId") String customerId,
@JsonProperty("email") String email,
@JsonProperty("name") String name,
@JsonProperty("address") Address address) {
this.customerId = customerId;
this.email = email;
this.name = name;
this.address = address;
}

public record Customer(String customerId, String email, String name, Address address) {

public Customer withName(String newName) {
return new Customer(customerId, email, newName, address);
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,12 @@

import kalix.javasdk.eventsourcedentity.EventSourcedEntity;
import kalix.javasdk.eventsourcedentity.EventSourcedEntityContext;
import kalix.javasdk.valueentity.ValueEntity;
import kalix.springsdk.annotations.Entity;
import kalix.springsdk.annotations.EventHandler;
import org.springframework.web.bind.annotation.*;

import static customer.api.CustomerEvent.*;

@Entity(entityKey = "id", entityType = "customer")
@RequestMapping("/customer/{id}")
public class CustomerEntity extends EventSourcedEntity<Customer> {
Expand All @@ -41,13 +42,13 @@ public Effect<Customer> getCustomer() {
@PostMapping("/create")
public Effect<String> create(@RequestBody Customer customer) {
return effects()
.emitEvent(new CustomerCreated(customer.email, customer.name, customer.address))
.emitEvent(new CustomerCreated(customer.email(), customer.name(), customer.address()))
.thenReply(__ -> "OK");
}

@EventHandler
public Customer onEvent(CustomerCreated created) {
return new Customer(entityId, created.email, created.name, created.address);
return new Customer(entityId, created.email(), created.name(), created.address());
}


Expand All @@ -61,7 +62,7 @@ public Effect<String> changeName(@PathVariable String newName) {

@EventHandler
public Customer onEvent(NameChanged nameChanged) {
return currentState().withName(nameChanged.newName);
return currentState().withName(nameChanged.newName());
}


Expand All @@ -73,7 +74,7 @@ public Effect<String> changeAddress(@RequestBody Address newAddress) {
}

@EventHandler
public Customer onEvents(AddressChanged addressChanged){
return currentState().withAddress(addressChanged.address);
public Customer onEvents(AddressChanged addressChanged) {
return currentState().withAddress(addressChanged.address());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,23 @@
import com.fasterxml.jackson.annotation.JsonSubTypes;
import com.fasterxml.jackson.annotation.JsonTypeInfo;

import static customer.api.CustomerEvent.*;

@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
@JsonSubTypes(
{
@JsonSubTypes.Type(value = CustomerCreated.class, name = "customer-created"),
@JsonSubTypes.Type(value = NameChanged.class, name = "name-changed"),
@JsonSubTypes.Type(value = AddressChanged.class, name = "address-changed")
})
public interface CustomerEvent {
public sealed interface CustomerEvent {

record CustomerCreated(String email, String name, Address address) implements CustomerEvent {
}

record NameChanged(String newName) implements CustomerEvent {
}

record AddressChanged(Address address) implements CustomerEvent {
}
}

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,30 +1,20 @@
package customer.view;

import com.fasterxml.jackson.annotation.JsonCreator;
import com.fasterxml.jackson.annotation.JsonProperty;
import customer.api.*;
import customer.api.Address;
import customer.api.CustomerEvent;

import java.util.Optional;

public class CustomerView {
final public String email;
final public String name;
final public Address address;

@JsonCreator
public CustomerView(@JsonProperty("email") String email, @JsonProperty("name") String name, @JsonProperty("address") Address address) {
this.email = email;
this.name = name;
this.address = address;
}
import static customer.api.CustomerEvent.*;

public record CustomerView(String email, String name, Address address) {

public CustomerView withName(String newName) {
return new CustomerView( email, newName, address);
return new CustomerView(email, newName, address);
}

public CustomerView withAddress(Address newAddress) {
return new CustomerView( email, name, newAddress);
return new CustomerView(email, name, newAddress);
}

/**
Expand All @@ -35,25 +25,20 @@ public static Optional<CustomerView> onEvent(CustomerView state, CustomerEvent e

if (state == null) {
// the only event we can receive when state is null is the CustomerCreated
if (event instanceof CustomerCreated) {
CustomerCreated created = (CustomerCreated) event;
return Optional.of(new CustomerView(created.email, created.name, created.address));
if (event instanceof CustomerCreated created) {
return Optional.of(new CustomerView(created.email(), created.name(), created.address()));
}
} else {
// when not null, we can receive the other events
if (event instanceof NameChanged) {
NameChanged nameChanged = (NameChanged) event;
return Optional.of(state.withName(nameChanged.newName));
} else if (event instanceof AddressChanged) {
AddressChanged addressChanged = (AddressChanged) event;
return Optional.of(state.withAddress(addressChanged.address));
if (event instanceof NameChanged nameChanged) {
return Optional.of(state.withName(nameChanged.newName()));
} else if (event instanceof AddressChanged addressChanged) {
return Optional.of(state.withAddress(addressChanged.address()));
}
}

// If state is null, and we receive anything different from CustomerCreated we will end-up here.
// That case won't happen as the delivery ordering is guaranteed, but we need to keep the compiler happy
return Optional.empty();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
import kalix.springsdk.testkit.EventSourcedTestKit;
import org.junit.jupiter.api.Test;

import static customer.api.CustomerEvent.*;
import static org.junit.jupiter.api.Assertions.assertEquals;

public class CustomerEntityTest {
Expand All @@ -25,7 +26,7 @@ public void testCustomerNameChange() {
{
EventSourcedResult<String> result = testKit.call(e -> e.changeName("FooBar"));
assertEquals("OK", result.getReply());
assertEquals("FooBar", testKit.getState().name);
assertEquals("FooBar", testKit.getState().name());
result.getNextEventOfType(NameChanged.class);
}

Expand All @@ -45,8 +46,8 @@ public void testCustomerAddressChange() {
Address newAddress = new Address("Sesame Street", "Sesame City");
EventSourcedResult<String> result = testKit.call(e -> e.changeAddress(newAddress));
assertEquals("OK", result.getReply());
assertEquals("Sesame Street", testKit.getState().address.street);
assertEquals("Sesame City", testKit.getState().address.city);
assertEquals("Sesame Street", testKit.getState().address().street());
assertEquals("Sesame City", testKit.getState().address().city());
result.getNextEventOfType(AddressChanged.class);
}

Expand Down