1+ package com .dddheroes .heroesofddd .astrologers .automation .whenweeksymbolproclaimedthenincreasedwellingavailablecreatures ;
2+
3+ import com .dddheroes .heroesofddd .TestcontainersConfiguration ;
4+ import com .dddheroes .heroesofddd .astrologers .write .AstrologersEvent ;
5+ import com .dddheroes .heroesofddd .astrologers .write .AstrologersId ;
6+ import com .dddheroes .heroesofddd .astrologers .write .proclaimweeksymbol .ProclaimWeekSymbol ;
7+ import com .dddheroes .heroesofddd .astrologers .write .proclaimweeksymbol .WeekSymbolProclaimed ;
8+ import com .dddheroes .heroesofddd .creaturerecruitment .write .DwellingEvent ;
9+ import com .dddheroes .heroesofddd .creaturerecruitment .write .DwellingId ;
10+ import com .dddheroes .heroesofddd .creaturerecruitment .write .builddwelling .DwellingBuilt ;
11+ import com .dddheroes .heroesofddd .creaturerecruitment .write .changeavailablecreatures .IncreaseAvailableCreatures ;
12+ import com .dddheroes .heroesofddd .shared .Amount ;
13+ import com .dddheroes .heroesofddd .shared .Cost ;
14+ import com .dddheroes .heroesofddd .shared .CreatureId ;
15+ import com .dddheroes .heroesofddd .shared .ResourceType ;
16+ import org .axonframework .commandhandling .gateway .CommandGateway ;
17+ import org .axonframework .eventhandling .DomainEventMessage ;
18+ import org .axonframework .eventhandling .GenericDomainEventMessage ;
19+ import org .axonframework .eventhandling .gateway .EventGateway ;
20+ import org .junit .jupiter .api .*;
21+ import org .springframework .beans .factory .annotation .Autowired ;
22+ import org .springframework .boot .test .context .SpringBootTest ;
23+ import org .springframework .context .annotation .Import ;
24+ import org .springframework .test .context .bean .override .mockito .MockitoSpyBean ;
25+
26+ import java .util .UUID ;
27+
28+ import static com .dddheroes .heroesofddd .utils .AwaitilityUtils .awaitUntilAsserted ;
29+ import static org .mockito .ArgumentMatchers .any ;
30+ import static org .mockito .Mockito .*;
31+
32+ @ Import (TestcontainersConfiguration .class )
33+ @ SpringBootTest
34+ class WhenWeekSymbolProclaimedThenIncreaseDwellingAvailableCreaturesTest {
35+
36+ @ Autowired
37+ private EventGateway eventGateway ;
38+
39+ @ MockitoSpyBean
40+ private CommandGateway commandGateway ;
41+
42+ @ Test
43+ void test () {
44+ // given
45+ var angelDwellingId1 = givenDwellingBuiltEvent ("angel" );
46+ var angelDwellingId2 = givenDwellingBuiltEvent ("angel" );
47+ var titanDwellingId = givenDwellingBuiltEvent ("titan" );
48+
49+ // when
50+ var astrologersId = AstrologersId .random ();
51+ whenAstrologersEvents (
52+ astrologersId .raw (),
53+ new WeekSymbolProclaimed (astrologersId .raw (), 1 , 1 , "angel" , 3 )
54+ );
55+
56+ // then
57+ var expectedCommand1 = IncreaseAvailableCreatures .command (angelDwellingId1 , "angel" , 3 );
58+ awaitUntilAsserted (() -> verify (commandGateway , times (1 )).sendAndWait (expectedCommand1 ));
59+
60+ var expectedCommand2 = IncreaseAvailableCreatures .command (angelDwellingId2 , "angel" , 3 );
61+ awaitUntilAsserted (() -> verify (commandGateway , times (1 )).sendAndWait (expectedCommand2 ));
62+
63+ var notExpectedCommand = IncreaseAvailableCreatures .command (titanDwellingId , "titan" , 3 );
64+ awaitUntilAsserted (() -> verify (commandGateway , never ()).sendAndWait (notExpectedCommand ));
65+ }
66+
67+ private String givenDwellingBuiltEvent (String creatureId ) {
68+ var dwellingId = DwellingId .random ();
69+ var costPerTroop = Cost .resources (ResourceType .GOLD , Amount .of (1000 ));
70+ var event = DwellingBuilt .event (dwellingId , CreatureId .of (creatureId ), costPerTroop );
71+ eventGateway .publish (dwellingDomainEvent (dwellingId .raw (), 0 , event ));
72+ return dwellingId .raw ();
73+ }
74+
75+ private void whenAstrologersEvents (String gameId , AstrologersEvent ... events ) {
76+ for (int i = 0 ; i < events .length ; i ++) {
77+ eventGateway .publish (astrologersDomainEvent (gameId , i , events [i ]));
78+ }
79+ }
80+
81+ private static DomainEventMessage <?> astrologersDomainEvent (String identifier , int sequenceNumber ,
82+ AstrologersEvent payload ) {
83+ return givenDomainEvent (
84+ "Astrologers" ,
85+ identifier ,
86+ sequenceNumber ,
87+ payload
88+ );
89+ }
90+
91+ private static DomainEventMessage <?> dwellingDomainEvent (
92+ String identifier ,
93+ int sequenceNumber ,
94+ DwellingEvent payload
95+ ) {
96+ return givenDomainEvent (
97+ "Dwelling" ,
98+ identifier ,
99+ sequenceNumber ,
100+ payload
101+ );
102+ }
103+
104+ private static DomainEventMessage <?> givenDomainEvent (
105+ String aggregateType ,
106+ String identifier ,
107+ int sequenceNumber ,
108+ Object payload
109+ ) {
110+ return new GenericDomainEventMessage <>(
111+ aggregateType ,
112+ identifier ,
113+ sequenceNumber ,
114+ payload
115+ );
116+ }
117+ }
0 commit comments