Skip to content

Commit

Permalink
Fix DBUtilTest cases
Browse files Browse the repository at this point in the history
Signed-off-by: Rohit Yadav <[email protected]>
  • Loading branch information
rohityadavcloud committed Apr 24, 2024
1 parent 360c64c commit 2eb8b02
Showing 1 changed file with 8 additions and 15 deletions.
23 changes: 8 additions & 15 deletions framework/db/src/test/java/com/cloud/utils/DbUtilTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -151,28 +151,24 @@ public void cleanup() throws SecurityException, NoSuchFieldException, IllegalArg
public void getGlobalLock() throws SQLException {
Mockito.when(dataSource.getConnection()).thenReturn(connection);
Mockito.when(connection.prepareStatement(Matchers.anyString())).thenReturn(preparedStatement);
Mockito.when(preparedStatement.executeQuery()).thenReturn(resultSet);
Mockito.when(resultSet.first()).thenReturn(true);
Mockito.when(resultSet.getInt(1)).thenReturn(1);
Mockito.when(preparedStatement.executeUpdate()).thenReturn(1);
Assert.assertTrue(DbUtil.getGlobalLock("TEST", 600));

Mockito.verify(connection).prepareStatement(Matchers.anyString());
Mockito.verify(preparedStatement).close();
Mockito.verify(resultSet).close();
}

@Test
public void getGlobalLockTimeout() throws SQLException {
Mockito.when(dataSource.getConnection()).thenReturn(connection);
Mockito.when(connection.prepareStatement(Matchers.anyString())).thenReturn(preparedStatement);
Mockito.when(preparedStatement.executeQuery()).thenReturn(resultSet);
Mockito.when(resultSet.first()).thenReturn(true);
Mockito.when(resultSet.getInt(1)).thenReturn(0);
Assert.assertFalse(DbUtil.getGlobalLock("TEST", 600));
Mockito.when(preparedStatement.executeUpdate()).thenReturn(0);

Mockito.verify(connection).prepareStatement(Matchers.anyString());
Mockito.verify(preparedStatement).close();
Mockito.verify(resultSet).close();
final int tries = 2;
Assert.assertFalse(DbUtil.getGlobalLock("TEST", tries));

Mockito.verify(connection, Mockito.times(tries)).prepareStatement(Matchers.anyString());
Mockito.verify(preparedStatement, Mockito.times(tries)).close();
Mockito.verify(connection).close();

// if any error happens, the connection map must be cleared
Expand Down Expand Up @@ -238,13 +234,10 @@ void releaseGlobalLockNotexisting() throws SQLException {
@Test
public void releaseGlobalLock() throws SQLException {
Mockito.when(connection.prepareStatement(Matchers.anyString())).thenReturn(preparedStatement);
Mockito.when(preparedStatement.executeQuery()).thenReturn(resultSet);
Mockito.when(resultSet.first()).thenReturn(true);
Mockito.when(resultSet.getInt(1)).thenReturn(1);
Mockito.when(preparedStatement.executeUpdate()).thenReturn(1);
connectionMap.put("testLock", connection);
Assert.assertTrue(DbUtil.releaseGlobalLock("testLock"));

Mockito.verify(resultSet).close();
Mockito.verify(preparedStatement).close();
Mockito.verify(connection).close();
Assert.assertFalse(connectionMap.containsKey("testLock"));
Expand Down

0 comments on commit 2eb8b02

Please sign in to comment.