1616// under the License.
1717package com .cloud .upgrade .dao ;
1818
19+ import static org .mockito .Mockito .doNothing ;
1920import static org .mockito .Mockito .when ;
2021
2122import java .sql .Connection ;
2223import java .sql .PreparedStatement ;
24+ import java .sql .ResultSet ;
2325import java .sql .SQLException ;
2426
27+ import org .junit .Before ;
2528import org .junit .Test ;
2629import org .junit .runner .RunWith ;
2730import org .mockito .Mock ;
@@ -37,9 +40,24 @@ public class Upgrade42010to42100Test {
3740 @ Spy
3841 Upgrade42010to42100 upgrade ;
3942
43+ @ Mock
44+ private PreparedStatement mockPreparedStatement ;
45+ @ Mock
46+ private ResultSet mockResultSet ;
4047 @ Mock
4148 private Connection conn ;
4249
50+
51+ @ Before
52+ public void setup () throws Exception {
53+ when (conn .prepareStatement ("SELECT value FROM `cloud`.`configuration` where name = 'vm.allocation.algorithm'" ))
54+ .thenReturn (mockPreparedStatement );
55+ when (mockPreparedStatement .executeQuery ()).thenReturn (mockResultSet );
56+ when (mockResultSet .next ()).thenReturn (true );
57+ when (mockResultSet .getString (1 )).thenReturn ("random" );
58+ }
59+
60+
4361 @ Test
4462 public void testPerformDataMigration () throws SQLException {
4563 try (MockedStatic <DbUpgradeUtils > ignored = Mockito .mockStatic (DbUpgradeUtils .class )) {
@@ -70,4 +88,17 @@ public void testPerformDataMigration() throws SQLException {
7088 }
7189 }
7290 }
91+
92+ @ Test
93+ public void testPerformDataMigrationShouldUpdateVolumeAlgorithm () throws SQLException {
94+ when (mockResultSet .getString (1 )).thenReturn ("userdispersing" );
95+
96+ PreparedStatement updateStmt = Mockito .mock (PreparedStatement .class );
97+ when (conn .prepareStatement ("UPDATE `cloud`.`configuration` SET value = ? WHERE name = 'volume.allocation.algorithm'" ))
98+ .thenReturn (updateStmt );
99+ doNothing ().when (upgrade ).migrateConfigurationScopeToBitmask (conn );
100+ upgrade .performDataMigration (conn );
101+ Mockito .verify (updateStmt ).setString (1 , "userdispersing" );
102+ Mockito .verify (updateStmt ).executeUpdate ();
103+ }
73104}
0 commit comments