Skip to content

Commit 223b6ef

Browse files
authored
Add tests showing commands vs events (#67)
1 parent 5b9963f commit 223b6ef

File tree

3 files changed

+97
-0
lines changed

3 files changed

+97
-0
lines changed
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
package pl.mperor.lab.java.clean.code.ddd;
2+
3+
interface Command {
4+
5+
interface CommandHandler<C extends Command> {
6+
Status handle(C command);
7+
}
8+
9+
enum Status {
10+
DONE
11+
}
12+
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package pl.mperor.lab.java.clean.code.ddd;
2+
3+
import java.util.ArrayList;
4+
import java.util.Collection;
5+
import java.util.List;
6+
import java.util.function.Consumer;
7+
8+
interface Event {
9+
10+
record Publisher(Collection<EventConsumer> consumers) {
11+
12+
public Publisher {
13+
consumers = List.copyOf(consumers);
14+
}
15+
16+
Publisher onPublish(EventConsumer consumer) {
17+
var newConsumers = new ArrayList<>(consumers);
18+
newConsumers.add(consumer);
19+
return new Publisher(newConsumers);
20+
}
21+
22+
void publish() {
23+
var event = new Event() {};
24+
consumers.forEach(consumer -> consumer.accept(event));
25+
}
26+
27+
interface EventConsumer extends Consumer<Event> {
28+
}
29+
}
30+
}
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
package pl.mperor.lab.java.clean.code.ddd;
2+
3+
import org.junit.jupiter.api.Assertions;
4+
import org.junit.jupiter.api.Test;
5+
6+
import java.util.Collections;
7+
import java.util.UUID;
8+
9+
/**
10+
* ⚡ Command vs Event 🎯
11+
*
12+
* <p><b>Command:</b> An instruction to perform an action, usually leading to a state change.
13+
* - Imperative 🏗️
14+
* - Synchronous or Asynchronous ⏳
15+
* - Expect a specific effect 🔄
16+
*
17+
* <p><b>Event:</b> A fact that something has happened in the past.
18+
* - Declarative 📢
19+
* - Asynchronous-friendly ⏩
20+
* - Captures history 🕰️
21+
*/
22+
class CommandVsEventTest {
23+
24+
private final Command.CommandHandler handler = command -> {
25+
System.out.printf("🫵 Command: %s%n", command);
26+
return Command.Status.DONE;
27+
};
28+
29+
private final Event.Publisher publisher = new Event.Publisher(Collections.emptyList());
30+
31+
@Test
32+
void commandShouldExecuteImmediately() {
33+
Assertions.assertEquals(Command.Status.DONE, handler.handle(new Command() {}));
34+
}
35+
36+
@Test
37+
void commandsCanBeHandledByTheSameHandler() {
38+
record CreateOrderCommand(UUID uuid) implements Command {}
39+
record CreatePaymentCommand(UUID uuid) implements Command {}
40+
Assertions.assertEquals(Command.Status.DONE, handler.handle(new CreateOrderCommand(UUID.randomUUID())));
41+
Assertions.assertEquals(Command.Status.DONE, handler.handle(new CreatePaymentCommand(UUID.randomUUID())));
42+
}
43+
44+
@Test
45+
void eventShouldNotBeConsumeWithoutPublishing() {
46+
publisher.onPublish(_ -> Assertions.fail());
47+
}
48+
49+
@Test
50+
void eventShouldBeConsumeByManyConsumers() {
51+
publisher.onPublish(event -> System.out.printf("❗Event: %s%n", event))
52+
.onPublish(Assertions::assertNotNull)
53+
.publish();
54+
}
55+
}

0 commit comments

Comments
 (0)