33import com .dddheroes .heroesofddd .TestcontainersConfiguration ;
44import com .dddheroes .heroesofddd .astrologers .write .AstrologersEvent ;
55import com .dddheroes .heroesofddd .astrologers .write .AstrologersId ;
6- import com .dddheroes .heroesofddd .astrologers .write .proclaimweeksymbol .ProclaimWeekSymbol ;
76import com .dddheroes .heroesofddd .astrologers .write .proclaimweeksymbol .WeekSymbolProclaimed ;
87import com .dddheroes .heroesofddd .creaturerecruitment .write .DwellingEvent ;
98import com .dddheroes .heroesofddd .creaturerecruitment .write .DwellingId ;
2524import org .springframework .context .annotation .Import ;
2625import org .springframework .test .context .bean .override .mockito .MockitoSpyBean ;
2726
28- import java .util .UUID ;
29-
3027import static com .dddheroes .heroesofddd .utils .AwaitilityUtils .awaitUntilAsserted ;
31- import static org .mockito .ArgumentMatchers .any ;
3228import static org .mockito .Mockito .*;
3329
3430@ Import (TestcontainersConfiguration .class )
@@ -44,41 +40,74 @@ class WhenWeekSymbolProclaimedThenIncreaseDwellingAvailableCreaturesTest {
4440 private CommandGateway commandGateway ;
4541
4642 @ Test
47- void test () {
43+ void whenWeekSymbolProclaimed_thenIncreaseDwellingsAvailableCreaturesIfSymbolSameAsSymbol () {
4844 // given
49- var angelDwellingId1 = givenDwellingBuiltEvent ("angel" );
50- var angelDwellingId2 = givenDwellingBuiltEvent ("angel" );
51- var titanDwellingId = givenDwellingBuiltEvent ("titan" );
45+ var angelDwellingId1 = dwellingBuiltEvent ("angel" );
46+ var angelDwellingId2 = dwellingBuiltEvent ("angel" );
47+ var titanDwellingId = dwellingBuiltEvent ("titan" );
5248
5349 // when
5450 var astrologersId = AstrologersId .random ();
55- whenAstrologersEvents (
51+ astrologersEvents (
5652 astrologersId .raw (),
5753 new WeekSymbolProclaimed (astrologersId .raw (), 1 , 1 , "angel" , 3 )
5854 );
5955
6056 // then
6157 var expectedCommand1 = IncreaseAvailableCreatures .command (angelDwellingId1 , "angel" , 3 );
62- awaitUntilAsserted (() -> verify ( commandGateway , times ( 1 )). sendAndWait ( expectedCommand1 , GameMetaData . withId ( GAME_ID )) );
58+ assertCommandExecuted ( expectedCommand1 );
6359
6460 var expectedCommand2 = IncreaseAvailableCreatures .command (angelDwellingId2 , "angel" , 3 );
65- awaitUntilAsserted (() -> verify ( commandGateway , times ( 1 )). sendAndWait ( expectedCommand2 , GameMetaData . withId ( GAME_ID )) );
61+ assertCommandExecuted ( expectedCommand2 );
6662
6763 var notExpectedCommand = IncreaseAvailableCreatures .command (titanDwellingId , "titan" , 3 );
68- awaitUntilAsserted (() -> verify ( commandGateway , never ()). sendAndWait ( notExpectedCommand , GameMetaData . withId ( GAME_ID )) );
64+ assertCommandNotExecuted ( notExpectedCommand );
6965 }
7066
71- private String givenDwellingBuiltEvent (String creatureId ) {
67+ @ Test
68+ void whenWeekSymbolProclaimed_thenIncreaseAllDwellingsBuiltBeforeTheProclamation () {
69+ // given
70+ var astrologersId = AstrologersId .random ();
71+ var angelDwellingId1 = dwellingBuiltEvent ("angel" );
72+ astrologersEvents (
73+ astrologersId .raw (),
74+ new WeekSymbolProclaimed (astrologersId .raw (), 1 , 1 , "angel" , 1 )
75+ );
76+ var angelDwellingId2 = dwellingBuiltEvent ("angel" );
77+
78+ // when
79+ astrologersEvents (
80+ astrologersId .raw (),
81+ new WeekSymbolProclaimed (astrologersId .raw (), 1 , 2 , "angel" , 2 )
82+ );
83+
84+ // then
85+ // week 1 - only 1 dwelling built
86+ var week1ExpectedCommand1 = IncreaseAvailableCreatures .command (angelDwellingId1 , "angel" , 1 );
87+ assertCommandExecuted (week1ExpectedCommand1 );
88+ var week1NotExpectedCommand1 = IncreaseAvailableCreatures .command (angelDwellingId2 , "angel" , 1 );
89+ assertCommandNotExecuted (week1NotExpectedCommand1 );
90+
91+ // week 2 - 2 dwellings built
92+ var week2ExpectedCommand1 = IncreaseAvailableCreatures .command (angelDwellingId1 , "angel" , 2 );
93+ assertCommandExecuted (week2ExpectedCommand1 );
94+ var week2ExpectedCommand2 = IncreaseAvailableCreatures .command (angelDwellingId2 , "angel" , 2 );
95+ assertCommandExecuted (week2ExpectedCommand2 );
96+ }
97+
98+
99+ private String dwellingBuiltEvent (String creatureId ) {
72100 var dwellingId = DwellingId .random ();
73101 var costPerTroop = Cost .resources (ResourceType .GOLD , Amount .of (1000 ));
74102 var event = DwellingBuilt .event (dwellingId , CreatureId .of (creatureId ), costPerTroop );
75103 eventGateway .publish (dwellingDomainEvent (dwellingId .raw (), 0 , event ));
76104 return dwellingId .raw ();
77105 }
78106
79- private void whenAstrologersEvents (String gameId , AstrologersEvent ... events ) {
80- for (int i = 0 ; i < events .length ; i ++) {
81- eventGateway .publish (astrologersDomainEvent (gameId , i , events [i ]));
107+ private void astrologersEvents (String gameId , WeekSymbolProclaimed ... events ) {
108+ for (var event : events ) {
109+ var aggregateSequence = event .week () - 1 ;
110+ eventGateway .publish (astrologersDomainEvent (gameId , aggregateSequence , event ));
82111 }
83112 }
84113
@@ -118,4 +147,13 @@ private static DomainEventMessage<?> givenDomainEvent(
118147 payload
119148 ).andMetaData (GameMetaData .withId (GAME_ID ));
120149 }
150+
151+ private void assertCommandExecuted (IncreaseAvailableCreatures expectedCommand1 ) {
152+ awaitUntilAsserted (() -> verify (commandGateway , times (1 ))
153+ .sendAndWait (expectedCommand1 , GameMetaData .withId (GAME_ID )));
154+ }
155+
156+ private void assertCommandNotExecuted (IncreaseAvailableCreatures notExpectedCommand ) {
157+ verify (commandGateway , never ()).sendAndWait (notExpectedCommand , GameMetaData .withId (GAME_ID ));
158+ }
121159}
0 commit comments