2222import static org .mockito .ArgumentMatchers .anyBoolean ;
2323import static org .mockito .ArgumentMatchers .anyLong ;
2424import static org .mockito .ArgumentMatchers .eq ;
25+ import static org .mockito .Mockito .times ;
26+ import static org .mockito .Mockito .verify ;
2527import static org .powermock .api .mockito .PowerMockito .doNothing ;
2628import static org .powermock .api .mockito .PowerMockito .mock ;
29+ import static org .powermock .api .mockito .PowerMockito .verifyNew ;
2730import static org .powermock .api .mockito .PowerMockito .when ;
2831import static org .powermock .api .mockito .PowerMockito .whenNew ;
2932
33+ import com .google .common .util .concurrent .UncheckedExecutionException ;
3034import java .util .Iterator ;
3135import java .util .Vector ;
3236import java .util .stream .LongStream ;
4448import org .powermock .core .classloader .annotations .PrepareForTest ;
4549import org .powermock .modules .junit4 .PowerMockRunner ;
4650
47-
4851/**
4952 * Unit test for {@link ConvertToDBStorageCommand}.
5053 */
5154@ RunWith (PowerMockRunner .class )
5255@ PrepareForTest ({ Bookie .class , ConvertToDBStorageCommand .class })
5356public class ConvertToDBStorageCommandTest extends BookieCommandTestBase {
5457
58+ private InterleavedLedgerStorage interleavedLedgerStorage ;
59+ private DbLedgerStorage dbStorage ;
60+ private LedgerCache .LedgerIndexMetadata metadata ;
61+ private LedgerCache .PageEntriesIterable entries ;
62+
5563 public ConvertToDBStorageCommandTest () {
5664 super (3 , 0 );
5765 }
@@ -61,19 +69,19 @@ public void setup() throws Exception {
6169 super .setup ();
6270
6371 whenNew (ServerConfiguration .class ).withNoArguments ().thenReturn (conf );
64- whenNew (ServerConfiguration .class ).withParameterTypes (AbstractConfiguration .class )
65- .withArguments ( conf ). thenReturn (conf );
72+ whenNew (ServerConfiguration .class ).withParameterTypes (AbstractConfiguration .class ). withArguments ( conf )
73+ .thenReturn (conf );
6674
67- InterleavedLedgerStorage interleavedLedgerStorage = mock (InterleavedLedgerStorage .class );
75+ interleavedLedgerStorage = mock (InterleavedLedgerStorage .class );
6876 whenNew (InterleavedLedgerStorage .class ).withNoArguments ().thenReturn (interleavedLedgerStorage );
6977 doNothing ().when (interleavedLedgerStorage ).shutdown ();
7078 when (interleavedLedgerStorage .getActiveLedgersInRange (anyLong (), anyLong ())).thenReturn (this ::getLedgerId );
71- LedgerCache . LedgerIndexMetadata metadata = mock (LedgerCache .LedgerIndexMetadata .class );
79+ metadata = mock (LedgerCache .LedgerIndexMetadata .class );
7280 when (interleavedLedgerStorage .readLedgerIndexMetadata (anyLong ())).thenReturn (metadata );
73- LedgerCache . PageEntriesIterable entries = mock (LedgerCache .PageEntriesIterable .class );
81+ entries = mock (LedgerCache .PageEntriesIterable .class );
7482 when (interleavedLedgerStorage .getIndexEntries (anyLong ())).thenReturn (entries );
7583
76- DbLedgerStorage dbStorage = mock (DbLedgerStorage .class );
84+ dbStorage = mock (DbLedgerStorage .class );
7785 whenNew (DbLedgerStorage .class ).withNoArguments ().thenReturn (dbStorage );
7886 doNothing ().when (dbStorage ).shutdown ();
7987 when (dbStorage .addLedgerToIndex (anyLong (), anyBoolean (), eq (new byte [0 ]),
@@ -95,5 +103,22 @@ private Iterator<Long> getLedgerId() {
95103 public void testCTDB () {
96104 ConvertToDBStorageCommand cmd = new ConvertToDBStorageCommand ();
97105 Assert .assertTrue (cmd .apply (bkFlags , new String [] { "" }));
106+
107+ try {
108+ verifyNew (ServerConfiguration .class , times (1 )).withArguments (conf );
109+ verifyNew (InterleavedLedgerStorage .class , times (1 )).withNoArguments ();
110+ verifyNew (DbLedgerStorage .class , times (1 )).withNoArguments ();
111+
112+ verify (interleavedLedgerStorage , times (10 )).readLedgerIndexMetadata (anyLong ());
113+ verify (interleavedLedgerStorage , times (10 )).getIndexEntries (anyLong ());
114+ verify (dbStorage , times (10 ))
115+ .addLedgerToIndex (anyLong (), anyBoolean (), any (), any (LedgerCache .PageEntriesIterable .class ));
116+ verify (interleavedLedgerStorage , times (10 )).deleteLedger (anyLong ());
117+
118+ verify (dbStorage , times (1 )).shutdown ();
119+ verify (interleavedLedgerStorage , times (1 )).shutdown ();
120+ } catch (Exception e ) {
121+ throw new UncheckedExecutionException (e .getMessage (), e );
122+ }
98123 }
99124}
0 commit comments