37
37
import io .spine .core .Ack ;
38
38
import io .spine .core .Command ;
39
39
import io .spine .core .Event ;
40
+ import io .spine .core .EventContext ;
40
41
import io .spine .core .MessageId ;
41
42
import io .spine .core .TenantId ;
42
43
import io .spine .server .BoundedContext ;
50
51
import io .spine .server .aggregate .given .aggregate .TaskAggregateRepository ;
51
52
import io .spine .server .aggregate .given .aggregate .TestAggregate ;
52
53
import io .spine .server .aggregate .given .aggregate .TestAggregateRepository ;
54
+ import io .spine .server .aggregate .given .thermometer .SafeThermometer ;
55
+ import io .spine .server .aggregate .given .thermometer .SafeThermometerRepo ;
56
+ import io .spine .server .aggregate .given .thermometer .Thermometer ;
57
+ import io .spine .server .aggregate .given .thermometer .ThermometerId ;
58
+ import io .spine .server .aggregate .given .thermometer .event .TemperatureChanged ;
53
59
import io .spine .server .commandbus .CommandBus ;
54
60
import io .spine .server .delivery .MessageEndpoint ;
55
61
import io .spine .server .dispatch .BatchDispatchOutcome ;
81
87
import io .spine .test .aggregate .rejection .Rejections .AggCannotReassignUnassignedTask ;
82
88
import io .spine .testing .logging .MuteLogging ;
83
89
import io .spine .testing .server .EventSubject ;
84
- import io .spine .testing .server .blackbox .BlackBoxContext ;
90
+ import io .spine .testing .server .blackbox .ContextAwareTest ;
85
91
import io .spine .testing .server .model .ModelTests ;
86
92
import io .spine .time .testing .TimeTests ;
87
93
import org .junit .jupiter .api .AfterEach ;
@@ -317,12 +323,13 @@ void writeVersionIntoEventContext() {
317
323
dispatchCommand (aggregate , command (createProject ));
318
324
319
325
// Get the first event since the command handler produces only one event message.
320
- Aggregate <?, ?, ?> agg = this .aggregate ;
321
- List <Event > uncommittedEvents = agg .getUncommittedEvents ().list ();
326
+ Aggregate <?, ?, ?> agg = aggregate ;
327
+ List <Event > uncommittedEvents = agg .getUncommittedEvents ()
328
+ .list ();
322
329
Event event = uncommittedEvents .get (0 );
323
-
324
- assertEquals ( this . aggregate .version (), event . context ( )
325
- .getVersion ());
330
+ EventContext context = event . context ();
331
+ assertThat ( aggregate .version ())
332
+ . isEqualTo ( context .getVersion ());
326
333
}
327
334
328
335
@ Test
@@ -719,7 +726,8 @@ void throughNewestEventsFirst() {
719
726
720
727
private ProtoSubject assertNextCommandId () {
721
728
Event event = history .next ();
722
- return assertThat (event .rootMessage ().asCommandId ());
729
+ return assertThat (event .rootMessage ()
730
+ .asCommandId ());
723
731
}
724
732
725
733
@ Test
@@ -818,27 +826,20 @@ void checkEventsUponHistory() {
818
826
private static void dispatch (TenantId tenant ,
819
827
Supplier <MessageEndpoint <ProjectId , ?>> endpoint ) {
820
828
with (tenant ).run (
821
- () -> endpoint .get ().dispatchTo (ID )
829
+ () -> endpoint .get ()
830
+ .dispatchTo (ID )
822
831
);
823
832
}
824
833
825
834
@ Nested
826
835
@ DisplayName ("create a single event when emitting a pair without second value" )
827
- class CreateSingleEventForPair {
828
-
829
- private BlackBoxContext context ;
830
-
831
- @ BeforeEach
832
- void prepareContext () {
833
- context = BlackBoxContext .from (
834
- BoundedContextBuilder .assumingTests ()
835
- .add (new TaskAggregateRepository ())
836
- );
837
- }
836
+ class CreateSingleEventForPair extends ContextAwareTest {
838
837
839
- @ AfterEach
840
- void closeContext () {
841
- context .close ();
838
+ @ Override
839
+ protected BoundedContextBuilder contextBuilder () {
840
+ return BoundedContextBuilder
841
+ .assumingTests ()
842
+ .add (new TaskAggregateRepository ());
842
843
}
843
844
844
845
/**
@@ -852,10 +853,10 @@ void closeContext() {
852
853
@ Test
853
854
@ DisplayName ("when dispatching a command" )
854
855
void fromCommandDispatch () {
855
- context .receivesCommand (createTask ())
856
- .assertEvents ()
857
- .withType (AggTaskCreated .class )
858
- .isNotEmpty ();
856
+ context () .receivesCommand (createTask ())
857
+ .assertEvents ()
858
+ .withType (AggTaskCreated .class )
859
+ .isNotEmpty ();
859
860
}
860
861
861
862
/**
@@ -870,8 +871,8 @@ void fromCommandDispatch() {
870
871
@ Test
871
872
@ DisplayName ("when reacting on an event" )
872
873
void fromEventReact () {
873
- EventSubject assertEvents = context .receivesCommand (assignTask ())
874
- .assertEvents ();
874
+ EventSubject assertEvents = context () .receivesCommand (assignTask ())
875
+ .assertEvents ();
875
876
assertEvents .hasSize (2 );
876
877
assertEvents .withType (AggTaskAssigned .class )
877
878
.hasSize (1 );
@@ -891,13 +892,55 @@ void fromEventReact() {
891
892
@ Test
892
893
@ DisplayName ("when reacting on a rejection" )
893
894
void fromRejectionReact () {
894
- EventSubject assertEvents = context .receivesCommand (reassignTask ())
895
- .assertEvents ();
895
+ EventSubject assertEvents = context () .receivesCommand (reassignTask ())
896
+ .assertEvents ();
896
897
assertEvents .hasSize (2 );
897
898
assertEvents .withType (AggCannotReassignUnassignedTask .class )
898
899
.hasSize (1 );
899
900
assertEvents .withType (AggUserNotified .class )
900
901
.hasSize (1 );
901
902
}
902
903
}
904
+
905
+ @ Nested
906
+ @ DisplayName ("allow having validation on the aggregate state and" )
907
+ class AllowValidatedAggregates extends ContextAwareTest {
908
+
909
+ private final ThermometerId thermometer = ThermometerId .generate ();
910
+
911
+ @ Override
912
+ protected BoundedContextBuilder contextBuilder () {
913
+ return BoundedContextBuilder
914
+ .assumingTests ()
915
+ .add (new SafeThermometerRepo (thermometer ));
916
+ }
917
+
918
+ @ Test
919
+ @ DisplayName ("not change the Aggregate state when there is no reaction on the event" )
920
+ void notChangeStateIfNoReaction () {
921
+ TemperatureChanged booksOnFire =
922
+ TemperatureChanged .newBuilder ()
923
+ .setFahrenheit (451 )
924
+ .vBuild ();
925
+ context ().receivesExternalEvent (booksOnFire )
926
+ .assertEntity (thermometer , SafeThermometer .class )
927
+ .doesNotExist ();
928
+ }
929
+
930
+ @ Test
931
+ @ DisplayName ("save valid aggregate state on change" )
932
+ void safelySaveValidState () {
933
+ TemperatureChanged gettingWarmer =
934
+ TemperatureChanged .newBuilder ()
935
+ .setFahrenheit (72 )
936
+ .vBuild ();
937
+ context ().receivesExternalEvent (gettingWarmer );
938
+ Thermometer expected = Thermometer
939
+ .newBuilder ()
940
+ .setId (thermometer )
941
+ .setFahrenheit (72 )
942
+ .vBuild ();
943
+ context ().assertState (thermometer , expected );
944
+ }
945
+ }
903
946
}
0 commit comments