2929import org .mockito .junit .jupiter .MockitoExtension ;
3030import org .neo4j .driver .Result ;
3131import org .neo4j .driver .Session ;
32+ import org .neo4j .driver .SessionConfig ;
33+ import org .neo4j .driver .Transaction ;
34+ import org .neo4j .driver .TransactionWork ;
3235import org .neo4j .driver .Values ;
3336import org .neo4j .driver .exceptions .ServiceUnavailableException ;
3437import org .neo4j .driver .exceptions .SessionExpiredException ;
35- import org .neo4j .driver .SessionConfig ;
3638import org .springframework .boot .actuate .health .Health ;
3739import org .springframework .boot .actuate .health .Status ;
3840
@@ -48,6 +50,9 @@ class Neo4jHealthIndicatorTest extends Neo4jHealthIndicatorTestBase {
4850 @ Mock
4951 private Result statementResult ;
5052
53+ @ Mock
54+ private Transaction transaction ;
55+
5156 @ Test
5257 void shouldWorkWithoutDatabaseName () {
5358 when (this .serverInfo .version ()).thenReturn ("4711" );
@@ -60,7 +65,11 @@ void shouldWorkWithoutDatabaseName() {
6065 when (record .get ("edition" )).thenReturn (Values .value ("some edition" ));
6166 when (this .statementResult .single ()).thenReturn (this .record );
6267 when (this .statementResult .consume ()).thenReturn (this .resultSummary );
63- when (this .session .run (anyString ())).thenReturn (this .statementResult );
68+ when (this .transaction .run (anyString ())).thenReturn (this .statementResult );
69+ when (this .session .writeTransaction (any (TransactionWork .class ))).then (invocationOnMock -> {
70+ TransactionWork <ResultSummaryWithEdition > tw = invocationOnMock .getArgument (0 );
71+ return tw .execute (transaction );
72+ });
6473
6574 when (this .driver .session (any (SessionConfig .class ))).thenReturn (this .session );
6675
@@ -84,7 +93,11 @@ void shouldWorkWithEmptyDatabaseName() {
8493 when (record .get ("edition" )).thenReturn (Values .value ("some edition" ));
8594 when (this .statementResult .single ()).thenReturn (this .record );
8695 when (this .statementResult .consume ()).thenReturn (this .resultSummary );
87- when (this .session .run (anyString ())).thenReturn (this .statementResult );
96+ when (this .transaction .run (anyString ())).thenReturn (this .statementResult );
97+ when (this .session .writeTransaction (any (TransactionWork .class ))).then (invocationOnMock -> {
98+ TransactionWork <ResultSummaryWithEdition > tw = invocationOnMock .getArgument (0 );
99+ return tw .execute (transaction );
100+ });
88101
89102 when (driver .session (any (SessionConfig .class ))).thenReturn (this .session );
90103
@@ -103,8 +116,11 @@ void neo4jIsUp() {
103116
104117 when (this .statementResult .single ()).thenReturn (this .record );
105118 when (this .statementResult .consume ()).thenReturn (this .resultSummary );
106- when (this .session .run (anyString ())).thenReturn (this .statementResult );
107-
119+ when (this .transaction .run (anyString ())).thenReturn (this .statementResult );
120+ when (this .session .writeTransaction (any (TransactionWork .class ))).then (invocationOnMock -> {
121+ TransactionWork <ResultSummaryWithEdition > tw = invocationOnMock .getArgument (0 );
122+ return tw .execute (transaction );
123+ });
108124 when (this .driver .session (any (SessionConfig .class ))).thenReturn (this .session );
109125
110126 Neo4jHealthIndicator healthIndicator = new Neo4jHealthIndicator (this .driver );
@@ -115,7 +131,8 @@ void neo4jIsUp() {
115131 assertThat (health .getDetails ()).containsEntry ("edition" , "ultimate collectors edition" );
116132
117133 verify (session ).close ();
118- verifyNoMoreInteractions (this .driver , this .session , this .statementResult , this .resultSummary , this .serverInfo , this .databaseInfo );
134+ verifyNoMoreInteractions (this .driver , this .session , this .statementResult , this .resultSummary , this .serverInfo ,
135+ this .databaseInfo , this .transaction );
119136 }
120137
121138 @ Test
@@ -126,12 +143,16 @@ void neo4jSessionIsExpiredOnce() {
126143 prepareSharedMocks ();
127144 when (this .statementResult .single ()).thenReturn (this .record );
128145 when (this .statementResult .consume ()).thenReturn (this .resultSummary );
129- when (this .session .run (anyString ())).thenAnswer (invocation -> {
146+ when (this .transaction .run (anyString ())).thenAnswer (invocation -> {
130147 if (cnt .compareAndSet (0 , 1 )) {
131148 throw new SessionExpiredException ("Session expired" );
132149 }
133150 return Neo4jHealthIndicatorTest .this .statementResult ;
134151 });
152+ when (this .session .writeTransaction (any (TransactionWork .class ))).then (invocationOnMock -> {
153+ TransactionWork <ResultSummaryWithEdition > tw = invocationOnMock .getArgument (0 );
154+ return tw .execute (transaction );
155+ });
135156 when (driver .session (any (SessionConfig .class ))).thenReturn (this .session );
136157
137158 Neo4jHealthIndicator healthIndicator = new Neo4jHealthIndicator (this .driver );
@@ -141,7 +162,8 @@ void neo4jSessionIsExpiredOnce() {
141162 assertThat (health .getDetails ()).containsEntry ("server" , "4711@Zu Hause" );
142163
143164 verify (this .session , times (2 )).close ();
144- verifyNoMoreInteractions (this .driver , this .session , this .statementResult , this .resultSummary , this .serverInfo , this .databaseInfo );
165+ verifyNoMoreInteractions (this .driver , this .session , this .statementResult , this .resultSummary , this .serverInfo ,
166+ this .databaseInfo , this .transaction );
145167 }
146168
147169 @ Test
@@ -155,6 +177,7 @@ void neo4jSessionIsDown() {
155177 assertThat (health .getStatus ()).isEqualTo (Status .DOWN );
156178 assertThat (health .getDetails ()).containsKeys ("error" );
157179
158- verifyNoMoreInteractions (this .driver , this .session , this .statementResult , this .resultSummary , this .serverInfo , this .databaseInfo );
180+ verifyNoMoreInteractions (this .driver , this .session , this .statementResult , this .resultSummary , this .serverInfo ,
181+ this .databaseInfo , this .transaction );
159182 }
160183}
0 commit comments