55import com .arangodb .DbName ;
66import com .arangodb .entity .StreamTransactionEntity ;
77import com .arangodb .model .StreamTransactionOptions ;
8+ import com .arangodb .model .TransactionCollectionOptions ;
89import com .arangodb .springframework .core .ArangoOperations ;
910import com .arangodb .springframework .repository .query .QueryTransactionBridge ;
1011import org .junit .Before ;
1516import org .mockito .InjectMocks ;
1617import org .mockito .Mock ;
1718import org .mockito .junit .MockitoJUnitRunner ;
19+ import org .springframework .transaction .IllegalTransactionStateException ;
1820import org .springframework .transaction .InvalidIsolationLevelException ;
1921import org .springframework .transaction .TransactionDefinition ;
2022import org .springframework .transaction .TransactionStatus ;
2628import java .util .function .Function ;
2729
2830import static org .hamcrest .MatcherAssert .assertThat ;
29- import static org .hamcrest .Matchers .is ;
31+ import static org .hamcrest .Matchers .* ;
3032import static org .mockito .ArgumentMatchers .any ;
3133import static org .mockito .Mockito .*;
34+ import static org .springframework .beans .PropertyAccessorFactory .forDirectFieldAccess ;
3235
3336@ RunWith (MockitoJUnitRunner .class )
3437public class ArangoTransactionManagerTest {
@@ -72,7 +75,7 @@ public void getTransactionReturnsNewTransactionWithoutStreamTransaction() {
7275 }
7376
7477 @ Test
75- public void getTransactionReturnsTransactionCreatesStreamTransactionOnBridgeBeginCall () {
78+ public void getTransactionReturnsTransactionCreatesStreamTransactionWithAllCollectionsOnBridgeBeginCall () {
7679 DefaultTransactionAttribute definition = new DefaultTransactionAttribute ();
7780 definition .setLabels (Collections .singleton ("baz" ));
7881 definition .setTimeout (20 );
@@ -83,9 +86,47 @@ public void getTransactionReturnsTransactionCreatesStreamTransactionOnBridgeBegi
8386 .thenReturn (streamTransaction );
8487 verify (bridge ).setCurrentTransaction (beginPassed .capture ());
8588 beginPassed .getValue ().apply (Arrays .asList ("foo" , "bar" ));
89+ assertThat (transaction .isCompleted (), is (false ));
8690 verify (database ).beginStreamTransaction (optionsPassed .capture ());
8791 assertThat (optionsPassed .getValue ().getAllowImplicit (), is (true ));
8892 assertThat (optionsPassed .getValue ().getLockTimeout (), is (20 ));
93+ TransactionCollectionOptions collections = getCollections (optionsPassed .getValue ());
94+ assertThat (collections .getRead (), nullValue ());
95+ assertThat (collections .getExclusive (), nullValue ());
96+ assertThat (collections .getWrite (), hasItems ("baz" , "foo" , "bar" ));
97+ }
98+
99+ @ Test
100+ public void getTransactionWithMultipleBridgeCallsWorksForKnownCollections () {
101+ DefaultTransactionAttribute definition = new DefaultTransactionAttribute ();
102+ definition .setLabels (Collections .singleton ("baz" ));
103+ definition .setTimeout (20 );
104+ underTest .getTransaction (definition );
105+ when (streamTransaction .getId ())
106+ .thenReturn ("123" );
107+ when (database .beginStreamTransaction (any ()))
108+ .thenReturn (streamTransaction );
109+ verify (bridge ).setCurrentTransaction (beginPassed .capture ());
110+ beginPassed .getValue ().apply (Collections .singletonList ("foo" ));
111+ beginPassed .getValue ().apply (Arrays .asList ("foo" , "baz" ));
112+ verify (database ).beginStreamTransaction (optionsPassed .capture ());
113+ TransactionCollectionOptions collections = getCollections (optionsPassed .getValue ());
114+ assertThat (collections .getWrite (), hasItems ("baz" , "foo" ));
115+ }
116+
117+ @ Test (expected = IllegalTransactionStateException .class )
118+ public void getTransactionWithMultipleBridgeCallsFailsForAdditionalCollection () {
119+ DefaultTransactionAttribute definition = new DefaultTransactionAttribute ();
120+ definition .setLabels (Collections .singleton ("baz" ));
121+ definition .setTimeout (20 );
122+ underTest .getTransaction (definition );
123+ when (streamTransaction .getId ())
124+ .thenReturn ("123" );
125+ when (database .beginStreamTransaction (any ()))
126+ .thenReturn (streamTransaction );
127+ verify (bridge ).setCurrentTransaction (beginPassed .capture ());
128+ beginPassed .getValue ().apply (Collections .singletonList ("foo" ));
129+ beginPassed .getValue ().apply (Collections .singletonList ("bar" ));
89130 }
90131
91132 @ Test (expected = InvalidIsolationLevelException .class )
@@ -94,4 +135,8 @@ public void getTransactionThrowsInvalidIsolationLevelExceptionForIsolationSerial
94135 definition .setIsolationLevel (TransactionDefinition .ISOLATION_SERIALIZABLE );
95136 underTest .getTransaction (definition );
96137 }
138+
139+ private TransactionCollectionOptions getCollections (StreamTransactionOptions options ) {
140+ return (TransactionCollectionOptions ) forDirectFieldAccess (options ).getPropertyValue ("collections" );
141+ }
97142}
0 commit comments