Skip to content

Commit ea37338

Browse files
✨ feat: Astrologers | automation: DayStarted (day == 1) -> ProclaimWeekSymbol (#8)
1 parent 581e5f4 commit ea37338

5 files changed

Lines changed: 143 additions & 1 deletion

File tree

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.dddheroes.heroesofddd.astrologers;
2+
3+
import com.dddheroes.heroesofddd.astrologers.automation.whenweekstartedthenproclaimweeksymbol.WeekSymbolCalculator;
4+
import com.dddheroes.heroesofddd.astrologers.write.WeekSymbol;
5+
import com.dddheroes.heroesofddd.shared.CreatureId;
6+
import org.springframework.context.annotation.Bean;
7+
import org.springframework.context.annotation.Configuration;
8+
9+
@Configuration
10+
public class AstrologersConfiguration {
11+
12+
// todo: add more options than angel
13+
@Bean
14+
WeekSymbolCalculator inMemoryWeekSymbolCalculator() {
15+
return __ -> WeekSymbol.of(CreatureId.of("angel"), random(1, 5));
16+
}
17+
18+
private static int random(int min, int max) {
19+
return (int) (Math.random() * (max - min + 1) + min);
20+
}
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package com.dddheroes.heroesofddd.astrologers.automation.whenweekstartedthenproclaimweeksymbol;
2+
3+
import com.dddheroes.heroesofddd.astrologers.write.MonthWeek;
4+
import com.dddheroes.heroesofddd.astrologers.write.WeekSymbol;
5+
6+
import java.util.function.Function;
7+
8+
@FunctionalInterface
9+
public interface WeekSymbolCalculator extends Function<MonthWeek, WeekSymbol> {
10+
11+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
package com.dddheroes.heroesofddd.astrologers.automation.whenweekstartedthenproclaimweeksymbol;
2+
3+
4+
import com.dddheroes.heroesofddd.astrologers.write.MonthWeek;
5+
import com.dddheroes.heroesofddd.astrologers.write.proclaimweeksymbol.ProclaimWeekSymbol;
6+
import com.dddheroes.heroesofddd.calendar.write.startday.DayStarted;
7+
import org.axonframework.commandhandling.gateway.CommandGateway;
8+
import org.axonframework.config.ProcessingGroup;
9+
import org.axonframework.eventhandling.DisallowReplay;
10+
import org.axonframework.eventhandling.EventHandler;
11+
import org.springframework.stereotype.Component;
12+
13+
@ProcessingGroup("Automation_WhenWeekStartedThenProclaimWeekSymbolProcessor_Processor")
14+
@DisallowReplay
15+
@Component
16+
class WhenWeekStartedThenProclaimWeekSymbolProcessor {
17+
18+
private final CommandGateway commandGateway;
19+
private final WeekSymbolCalculator weekSymbolCalculator;
20+
21+
WhenWeekStartedThenProclaimWeekSymbolProcessor(
22+
CommandGateway commandGateway,
23+
WeekSymbolCalculator weekSymbolCalculator
24+
) {
25+
this.commandGateway = commandGateway;
26+
this.weekSymbolCalculator = weekSymbolCalculator;
27+
}
28+
29+
@EventHandler
30+
void react(DayStarted event) {
31+
var isWeekStarted = event.day() == 1;
32+
if (isWeekStarted) {
33+
var weekSymbol = weekSymbolCalculator.apply(MonthWeek.of(event.month(), event.week()));
34+
var command = ProclaimWeekSymbol.command(
35+
event.calendarId(),
36+
event.month(),
37+
event.week(),
38+
weekSymbol.weekOf().raw(),
39+
weekSymbol.growth()
40+
);
41+
commandGateway.sendAndWait(command);
42+
}
43+
}
44+
}

src/main/java/com/dddheroes/heroesofddd/creaturerecruitment/automation/WhenCreatureRecruitedThenAddToArmyProcessor.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,7 @@ class WhenCreatureRecruitedThenAddToArmyProcessor {
2020
}
2121

2222
@EventHandler
23-
void on(CreatureRecruited event) {
23+
void react(CreatureRecruited event) {
2424
var command = AddCreatureToArmy.command(
2525
event.toArmy(),
2626
event.creatureId(),
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,66 @@
1+
package com.dddheroes.heroesofddd.astrologers.automation.whenweekstartedthenproclaimweeksymbol;
2+
3+
import com.dddheroes.heroesofddd.TestcontainersConfiguration;
4+
import com.dddheroes.heroesofddd.astrologers.write.proclaimweeksymbol.ProclaimWeekSymbol;
5+
import com.dddheroes.heroesofddd.calendar.write.CalendarEvent;
6+
import com.dddheroes.heroesofddd.calendar.write.CalendarId;
7+
import com.dddheroes.heroesofddd.calendar.write.startday.DayStarted;
8+
import org.axonframework.commandhandling.gateway.CommandGateway;
9+
import org.axonframework.eventhandling.DomainEventMessage;
10+
import org.axonframework.eventhandling.GenericDomainEventMessage;
11+
import org.axonframework.eventhandling.gateway.EventGateway;
12+
import org.junit.jupiter.api.*;
13+
import org.springframework.beans.factory.annotation.Autowired;
14+
import org.springframework.boot.test.context.SpringBootTest;
15+
import org.springframework.context.annotation.Import;
16+
import org.springframework.test.context.bean.override.mockito.MockitoSpyBean;
17+
18+
import java.util.UUID;
19+
20+
import static com.dddheroes.heroesofddd.utils.AwaitilityUtils.awaitUntilAsserted;
21+
import static org.mockito.Mockito.*;
22+
23+
@Import(TestcontainersConfiguration.class)
24+
@SpringBootTest
25+
class WhenWeekStartedThenProclaimWeekSymbolTest {
26+
27+
@Autowired
28+
private EventGateway eventGateway;
29+
30+
@MockitoSpyBean
31+
private CommandGateway commandGateway;
32+
33+
@Test
34+
void whenDayStartedForFirstDayOfTheWeek_ThenProclaimWeekSymbol() {
35+
// given
36+
var gameId = UUID.randomUUID().toString();
37+
var calendarId = CalendarId.of(gameId);
38+
givenCalendarEvents(
39+
gameId,
40+
new DayStarted(calendarId.raw(), 1, 1, 1)
41+
);
42+
43+
// when
44+
// processed by the automation
45+
46+
// then
47+
awaitUntilAsserted(() -> verify(commandGateway, times(1))
48+
.sendAndWait(ProclaimWeekSymbol.command(gameId, 1, 1, "angel", any()))
49+
);
50+
}
51+
52+
private void givenCalendarEvents(String gameId, CalendarEvent... events) {
53+
for (int i = 0; i < events.length; i++) {
54+
eventGateway.publish(calendarDomainEvent(gameId, i, events[i]));
55+
}
56+
}
57+
58+
private DomainEventMessage<?> calendarDomainEvent(String identifier, int sequenceNumber, CalendarEvent payload) {
59+
return new GenericDomainEventMessage<>(
60+
"Calendar",
61+
identifier,
62+
sequenceNumber,
63+
payload
64+
);
65+
}
66+
}

0 commit comments

Comments
 (0)