Skip to content

Add tests showing commands vs events #67

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 8, 2025
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
@@ -0,0 +1,12 @@
package pl.mperor.lab.java.clean.code.ddd;

interface Command {

interface CommandHandler<C extends Command> {
Status handle(C command);
}

enum Status {
DONE
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package pl.mperor.lab.java.clean.code.ddd;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import java.util.function.Consumer;

interface Event {

record Publisher(Collection<EventConsumer> consumers) {

public Publisher {
consumers = List.copyOf(consumers);
}

Publisher onPublish(EventConsumer consumer) {
var newConsumers = new ArrayList<>(consumers);
newConsumers.add(consumer);
return new Publisher(newConsumers);
}

void publish() {
var event = new Event() {};
consumers.forEach(consumer -> consumer.accept(event));
}

interface EventConsumer extends Consumer<Event> {
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package pl.mperor.lab.java.clean.code.ddd;

import org.junit.jupiter.api.Assertions;
import org.junit.jupiter.api.Test;

import java.util.Collections;
import java.util.UUID;

/**
* ⚡ Command vs Event 🎯
*
* <p><b>Command:</b> An instruction to perform an action, usually leading to a state change.
* - Imperative 🏗️
* - Synchronous or Asynchronous ⏳
* - Expect a specific effect 🔄
*
* <p><b>Event:</b> A fact that something has happened in the past.
* - Declarative 📢
* - Asynchronous-friendly ⏩
* - Captures history 🕰️
*/
class CommandVsEventTest {

private final Command.CommandHandler handler = command -> {
System.out.printf("🫵 Command: %s%n", command);
return Command.Status.DONE;
};

private final Event.Publisher publisher = new Event.Publisher(Collections.emptyList());

@Test
void commandShouldExecuteImmediately() {
Assertions.assertEquals(Command.Status.DONE, handler.handle(new Command() {}));
}

@Test
void commandsCanBeHandledByTheSameHandler() {
record CreateOrderCommand(UUID uuid) implements Command {}
record CreatePaymentCommand(UUID uuid) implements Command {}
Assertions.assertEquals(Command.Status.DONE, handler.handle(new CreateOrderCommand(UUID.randomUUID())));
Assertions.assertEquals(Command.Status.DONE, handler.handle(new CreatePaymentCommand(UUID.randomUUID())));
}

@Test
void eventShouldNotBeConsumeWithoutPublishing() {
publisher.onPublish(_ -> Assertions.fail());
}

@Test
void eventShouldBeConsumeByManyConsumers() {
publisher.onPublish(event -> System.out.printf("❗Event: %s%n", event))
.onPublish(Assertions::assertNotNull)
.publish();
}
}