@Test
void testCheckKeyLocks() throws IOException, HSMException
{

  // Create commonly used key names
  String firstKey = "firstKey";
  String secondKey = "secondKey";
  String thirdKey = "thirdKey";
  String fourthKey = "fourthKey";
  String fifthKey = "fifthKey";
  // Prepare ChangeKeyLocks for so called own instance
  Map<String, ChangeKeyLock> ownKeyLockList = new HashMap<>();
  ownKeyLockList.put(firstKey, new ChangeKeyLock(firstKey, "localhost", System.currentTimeMillis(), 0));
  ownKeyLockList.put(secondKey, new ChangeKeyLock(secondKey, "localhost", System.currentTimeMillis(), 1));
  // Prepare ChangeKeyLocks for so called other instances
  Map<String, ChangeKeyLock> otherKeyLockList = new HashMap<>();
  otherKeyLockList.put(thirdKey, new ChangeKeyLock(thirdKey, "otherhost", System.currentTimeMillis(), 0));
  otherKeyLockList.put(fourthKey, new ChangeKeyLock(fourthKey, "otherhost", System.currentTimeMillis(), 1));
  otherKeyLockList.put(fifthKey, new ChangeKeyLock(fifthKey, "otherhost", System.currentTimeMillis(), 1));
  // Make mock return different lists of ChangeKeyLocks depending on the parameter
  Mockito.doReturn(new ArrayList<>(ownKeyLockList.values()))
         .when(facade)
         .getAllChangeKeyLocksByInstance(true);
  Mockito.doReturn(new ArrayList<>(otherKeyLockList.values()))
         .when(facade)
         .getAllChangeKeyLocksByInstance(false);
  // Make mock return correct ChangeKeyLocks when obtainChangeKeyLock is called with different parameters
  Mockito.doReturn(otherKeyLockList.get(thirdKey)).when(facade).obtainChangeKeyLock(thirdKey, 0);
  Mockito.doReturn(otherKeyLockList.get(fourthKey)).when(facade).obtainChangeKeyLock(fourthKey, 1);
  Mockito.doReturn(null).when(facade).obtainChangeKeyLock(fifthKey, 1);
  // Make mock return different values depending on the parameter
  Mockito.when(hsmServiceHolder.isServiceSet()).thenReturn(true);
  Mockito.doReturn(false).when(hsmServiceHolder).isWorkingOnKey(firstKey);
  Mockito.doReturn(true).when(hsmServiceHolder).isWorkingOnKey(secondKey);
  // Create the KeyLockChecker and execute the method
  KeyLockChecker keyLockChecker = new KeyLockChecker(facade, hsmServiceHolder);
  keyLockChecker.checkKeyLocks();
  // Verify that the correct ChangeKeyLocks are released
  Mockito.verify(facade).releaseChangeKeyLock(ownKeyLockList.get(firstKey));
  Mockito.verify(facade, Mockito.never()).releaseChangeKeyLock(ownKeyLockList.get(secondKey));
  // Verify that the correct ChangeKeyLocks are deleted or distributed
  Mockito.verify(hsmServiceHolder).deleteKey(thirdKey);
  Mockito.verify(hsmServiceHolder).distributeKey(fourthKey);
  Mockito.verify(hsmServiceHolder, Mockito.never()).deleteKey(fifthKey);
  Mockito.verify(hsmServiceHolder, Mockito.never()).distributeKey(fifthKey);
}