5151import java .util .Optional ;
5252import org .junit .jupiter .api .BeforeEach ;
5353import org .junit .jupiter .api .Test ;
54+ import org .junit .jupiter .params .ParameterizedTest ;
55+ import org .junit .jupiter .params .provider .ValueSource ;
5456import org .mockito .ArgumentCaptor ;
5557import org .mockito .Mock ;
5658import org .mockito .MockitoAnnotations ;
@@ -466,6 +468,7 @@ public void commit_EmptySnapshotGiven_ShouldDoNothing()
466468 CrudException {
467469 // Arrange
468470 when (config .isDirectAssetAccessEnabled ()).thenReturn (false );
471+ when (config .isTxStateManagementEnabled ()).thenReturn (false );
469472
470473 // Act
471474 ledger .commit ();
@@ -482,6 +485,7 @@ public void commit_EmptySnapshotGivenAndDirectAssetAccessEnabled_ShouldDoNothing
482485 CrudException {
483486 // Arrange
484487 when (config .isDirectAssetAccessEnabled ()).thenReturn (true );
488+ when (config .isTxStateManagementEnabled ()).thenReturn (false );
485489
486490 // Act
487491 ledger .commit ();
@@ -741,12 +745,14 @@ public void commit_CommitExceptionThrownInCommit_ShouldThrowDatabaseException()
741745 assertThat (proofs ).containsOnly (proof );
742746 }
743747
744- @ Test
745- public void commit_TxStateManagementEnabled_ShouldPutWithStateManager ()
748+ @ ParameterizedTest
749+ @ ValueSource (booleans = {true , false })
750+ public void commit_WriteTransactionGiven_ShouldPutWithStateManagerAccordingToConfig (
751+ boolean txStateManagementEnabled )
746752 throws CrudException , CommitException ,
747753 com .scalar .db .exception .transaction .UnknownTransactionStatusException {
748754 // Arrange
749- when (config .isTxStateManagementEnabled ()).thenReturn (true );
755+ when (config .isTxStateManagementEnabled ()).thenReturn (txStateManagementEnabled );
750756 snapshot .put (ANY_ID , asset );
751757 snapshot .put (ANY_ID , ANY_DATA );
752758 doNothing ().when (transaction ).put (any (List .class ));
@@ -758,32 +764,40 @@ public void commit_TxStateManagementEnabled_ShouldPutWithStateManager()
758764 ledger .commit ();
759765
760766 // Assert
761- verify (stateManager ).putCommit (transaction , ANY_NONCE );
767+ if (txStateManagementEnabled ) {
768+ verify (stateManager ).putCommit (transaction , ANY_NONCE );
769+ } else {
770+ verify (stateManager , never ()).putCommit (any (DistributedTransaction .class ), anyString ());
771+ }
762772 verify (transaction ).put (any (List .class ));
763773 verify (transaction ).put (any (Put .class ));
764774 verify (transaction ).commit ();
765775 }
766776
767- @ Test
768- public void commit_TxStateManagementDisabled_ShouldNotPutWithStateManager ()
777+ @ ParameterizedTest
778+ @ ValueSource (booleans = {true , false })
779+ public void commit_ReadOnlyTransactionGiven_ShouldPutWithStateManagerAccordingToConfig (
780+ boolean txStateManagementEnabled )
769781 throws CrudException , CommitException ,
770782 com .scalar .db .exception .transaction .UnknownTransactionStatusException {
771783 // Arrange
772- when (config .isTxStateManagementEnabled ()).thenReturn (false );
773- snapshot .put (ANY_ID , asset );
774- snapshot .put (ANY_ID , ANY_DATA );
775- doNothing ().when (transaction ).put (any (List .class ));
776- doNothing ().when (transaction ).put (any (Put .class ));
784+ when (config .isTxStateManagementEnabled ()).thenReturn (txStateManagementEnabled );
785+ when (config .isDirectAssetAccessEnabled ()).thenReturn (false );
777786 when (transaction .getId ()).thenReturn (ANY_NONCE );
778787 doNothing ().when (stateManager ).putCommit (any (DistributedTransaction .class ), anyString ());
788+ snapshot .put (ANY_ID , asset );
779789
780790 // Act
781791 ledger .commit ();
782792
783793 // Assert
784- verify (stateManager , never ()).putCommit (transaction , ANY_NONCE );
785- verify (transaction ).put (any (List .class ));
786- verify (transaction ).put (any (Put .class ));
794+ if (txStateManagementEnabled ) {
795+ verify (stateManager ).putCommit (transaction , ANY_NONCE );
796+ } else {
797+ verify (stateManager , never ()).putCommit (any (DistributedTransaction .class ), anyString ());
798+ }
799+ verify (transaction , never ()).put (any (List .class ));
800+ verify (transaction , never ()).put (any (Put .class ));
787801 verify (transaction ).commit ();
788802 }
789803
0 commit comments