1212
1313import org .junit .jupiter .api .DisplayName ;
1414import org .junit .jupiter .api .Test ;
15+ import org .mockito .ArgumentCaptor ;
16+ import org .mockito .Captor ;
1517import org .mockito .InjectMocks ;
1618import org .mockito .Mock ;
1719import org .mockito .Mockito ;
20+ import org .springframework .context .ApplicationEvent ;
21+ import org .springframework .context .ApplicationEventPublisher ;
1822
1923import net .laffyco .javamatchingengine .engine .Order ;
2024import net .laffyco .javamatchingengine .engine .OrderBook ;
2125import net .laffyco .javamatchingengine .engine .Side ;
2226import net .laffyco .javamatchingengine .engine .Trade ;
27+ import net .laffyco .javamatchingengine .events .OrderAddedEvent ;
28+ import net .laffyco .javamatchingengine .events .OrderMatchedEvent ;
2329import test .utils .AbstractTest ;
2430
2531/**
@@ -36,13 +42,25 @@ public class OrderControllerTests extends AbstractTest {
3642 @ Mock
3743 private OrderBook orderBook ;
3844
45+ /**
46+ * Event publisher.
47+ */
48+ @ Mock
49+ private ApplicationEventPublisher applicationEventPublisher ;
50+
3951 /**
4052 * Controller under test.
4153 */
4254 @ InjectMocks
4355 @ Resource
4456 private OrderController controller ;
4557
58+ /**
59+ * Key captor.
60+ */
61+ @ Captor
62+ private ArgumentCaptor <ApplicationEvent > captor ;
63+
4664 /**
4765 * Test data id.
4866 */
@@ -61,6 +79,11 @@ public class OrderControllerTests extends AbstractTest {
6179 */
6280 private final double amt = 25.0 ;
6381
82+ /**
83+ * Test data price.
84+ */
85+ private final double price = 25 ;
86+
6487 @ Override
6588 public final void init () {
6689 Mockito .when (this .orderBook .findOrder (this .id , this .side ))
@@ -100,19 +123,41 @@ public void getOrderById() {
100123 @ Test
101124 @ DisplayName ("Add an order" )
102125 public void addOrder () {
103- final double price = 25 ;
104-
105126 final List <Trade > trades = new ArrayList <Trade >();
106127 Mockito .when (this .orderBook .process (Mockito .any ())).thenReturn (trades );
107128
108129 final Map <String , Object > result = this .controller .addOrder (this .side ,
109- this .amt , price );
130+ this .amt , this . price );
110131
111132 assertTrue (result .containsKey (this .id ));
112133 assertTrue (result .containsKey ("trades" ));
113134 assertEquals (result .get ("trades" ), trades );
114135 }
115136
137+ /**
138+ * Checking that the correct events are published.
139+ */
140+ @ Test
141+ @ DisplayName ("Check that appropriate events are published" )
142+ public void events () {
143+ final List <Trade > trades = new ArrayList <Trade >() {
144+ {
145+ this .add (null );
146+ }
147+ };
148+ Mockito .when (this .orderBook .process (Mockito .any ())).thenReturn (trades );
149+
150+ this .controller .addOrder (this .side , this .amt , this .price );
151+
152+ Mockito .verify (this .applicationEventPublisher , Mockito .times (2 ))
153+ .publishEvent (this .captor .capture ());
154+
155+ assertTrue (
156+ this .captor .getAllValues ().get (0 ) instanceof OrderAddedEvent );
157+ assertTrue (
158+ this .captor .getAllValues ().get (1 ) instanceof OrderMatchedEvent );
159+ }
160+
116161 /**
117162 * Delete an order.
118163 */
@@ -137,16 +182,15 @@ public void deleteOrder() {
137182 @ Test
138183 @ DisplayName ("Update an order" )
139184 public void updateOrder () {
140- final double price = 35.0 ;
141185 final Side newSide = Side .SELL ;
142186
143187 final Map <String , Object > result = this .controller .updateOrder (this .id ,
144- this .side , Optional .of (this .amt ), Optional .of (price ),
188+ this .side , Optional .of (this .amt ), Optional .of (this . price ),
145189 Optional .of (newSide ));
146190
147191 assertTrue ((boolean ) result .get ("updated" ));
148192 Mockito .verify (this .mockOrder ).setAmount (this .amt );
149- Mockito .verify (this .mockOrder ).setPrice (price );
193+ Mockito .verify (this .mockOrder ).setPrice (this . price );
150194 Mockito .verify (this .mockOrder ).setSide (newSide );
151195 }
152196
@@ -156,7 +200,6 @@ public void updateOrder() {
156200 @ Test
157201 @ DisplayName ("Update an order with one parameter" )
158202 public void updateOrder1Param () {
159-
160203 final Map <String , Object > result = this .controller .updateOrder (this .id ,
161204 this .side , Optional .of (this .amt ), Optional .empty (),
162205 Optional .empty ());
0 commit comments