KeyLockChecker { public void checkKeyLocks() { try { log.debug("checking locks for {}", InetAddress.getLocalHost().toString()); List<ChangeKeyLock> lockList = facade.getAllChangeKeyLocksByInstance(true); if (hsmServiceHolder.isServiceSet()) { for ( ChangeKeyLock lock : lockList ) { if (!hsmServiceHolder.isWorkingOnKey(lock.getKeyName())) { facade.releaseChangeKeyLock(lock); log.debug("lock for key {} released", lock.getKeyName()); } } } List<ChangeKeyLock> foreignLockList = facade.getAllChangeKeyLocksByInstance(false); for ( ChangeKeyLock lock : foreignLockList ) { if (facade.obtainChangeKeyLock(lock.getKeyName(), lock.getType()) != null) { if (lock.getType() == ChangeKeyLock.TYPE_DELETE) { hsmServiceHolder.deleteKey(lock.getKeyName()); log.debug("lock for key {} stolen, deleting key", lock.getKeyName()); } else { hsmServiceHolder.distributeKey(lock.getKeyName()); log.debug("lock for key {} stolen, distributing key", lock.getKeyName()); } } } } catch (HSMException | IOException e) { log.error("Problem during key lock check", e); } } KeyLockChecker(TerminalPermissionAO facade, HSMServiceHolder hsmServiceHolder); void checkKeyLocks();  }
@Test void testCheckKeyLocks() throws IOException, HSMException { String firstKey = "firstKey"; String secondKey = "secondKey"; String thirdKey = "thirdKey"; String fourthKey = "fourthKey"; String fifthKey = "fifthKey"; 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)); 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)); Mockito.doReturn(new ArrayList<>(ownKeyLockList.values())) .when(facade) .getAllChangeKeyLocksByInstance(true); Mockito.doReturn(new ArrayList<>(otherKeyLockList.values())) .when(facade) .getAllChangeKeyLocksByInstance(false); 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); Mockito.when(hsmServiceHolder.isServiceSet()).thenReturn(true); Mockito.doReturn(false).when(hsmServiceHolder).isWorkingOnKey(firstKey); Mockito.doReturn(true).when(hsmServiceHolder).isWorkingOnKey(secondKey); KeyLockChecker keyLockChecker = new KeyLockChecker(facade, hsmServiceHolder); keyLockChecker.checkKeyLocks(); Mockito.verify(facade).releaseChangeKeyLock(ownKeyLockList.get(firstKey)); Mockito.verify(facade, Mockito.never()).releaseChangeKeyLock(ownKeyLockList.get(secondKey)); Mockito.verify(hsmServiceHolder).deleteKey(thirdKey); Mockito.verify(hsmServiceHolder).distributeKey(fourthKey); Mockito.verify(hsmServiceHolder, Mockito.never()).deleteKey(fifthKey); Mockito.verify(hsmServiceHolder, Mockito.never()).distributeKey(fifthKey); }