LockRestrictor implements DeleteRestrictor,
                                       RenameRestrictor { @Override public PathOperationRestriction hasRestriction(final Path path) { final LockInfo lockInfo = lockService.retrieveLockInfo(path); if (lockInfo != null && lockInfo.isLocked() && !identity.getIdentifier().equals(lockInfo.lockedBy())) { return new PathOperationRestriction() { @Override public String getMessage(final Path path) { return path.toURI() + " cannot be deleted, moved or renamed. It is locked by: " + lockInfo.lockedBy(); } }; } final List<LockInfo> lockInfos = lockService.retrieveLockInfos(path, true); if (lockInfos != null && !lockInfos.isEmpty()) { return new PathOperationRestriction() { @Override public String getMessage(final Path path) { return path.toURI() + " cannot be deleted, moved or renamed. It contains the following locked files: " + lockInfos; } }; } return null; }  @Override PathOperationRestriction hasRestriction(final Path path);  }
@Test public void lockFromAnotherUserShouldCauseRestriction() { when(lockInfo.isLocked()).thenReturn(true); when(lockService.retrieveLockInfo(any())).thenReturn(lockInfo); when(identity.getIdentifier()).thenReturn("456"); when(lockInfo.lockedBy()).thenReturn("123"); when(lockService.retrieveLockInfos(path, true)).thenReturn(Arrays.asList(lockInfo)); when(lockInfos.isEmpty()).thenReturn(true); PathOperationRestriction result = lockRestrictor.hasRestriction(path); assertNotNull(result); assertTrue(result instanceof PathOperationRestriction); }
@Test public void lockedFilesShouldCauseRestriction() { when(lockInfo.isLocked()).thenReturn(false); when(lockService.retrieveLockInfo(any())).thenReturn(lockInfo); when(identity.getIdentifier()).thenReturn("123"); when(lockInfo.lockedBy()).thenReturn("123"); when(lockInfos.size()).thenReturn(1); when(lockService.retrieveLockInfos(path, true)).thenReturn(lockInfos); when(lockInfos.isEmpty()).thenReturn(false); PathOperationRestriction result = lockRestrictor.hasRestriction(path); assertNotNull(result); assertTrue(result instanceof PathOperationRestriction); }
@Test public void noLockShouldNotCauseRestriction() { when(lockInfo.isLocked()).thenReturn(false); when(lockService.retrieveLockInfo(any())).thenReturn(lockInfo); when(identity.getIdentifier()).thenReturn("456"); when(lockInfo.lockedBy()).thenReturn("123"); when(lockService.retrieveLockInfos(path, true)).thenReturn(lockInfos); when(lockInfos.isEmpty()).thenReturn(true); PathOperationRestriction result = lockRestrictor.hasRestriction(path); assertNull(result); }
@Test public void lockBySameUserShouldNotCauseRestriction() { when(lockInfo.isLocked()).thenReturn(true); when(lockService.retrieveLockInfo(any())).thenReturn(lockInfo); when(identity.getIdentifier()).thenReturn("123"); when(lockInfo.lockedBy()).thenReturn("123"); when(lockService.retrieveLockInfos(path, true)).thenReturn(lockInfos); when(lockInfos.isEmpty()).thenReturn(true); PathOperationRestriction result = lockRestrictor.hasRestriction(path); assertNull(result); }
@Test public void emptyRestrictionListShouldNotCauseRestriction() { when(lockInfo.isLocked()).thenReturn(false); when(lockService.retrieveLockInfo(any())).thenReturn(lockInfo); when(identity.getIdentifier()).thenReturn("123"); when(lockInfo.lockedBy()).thenReturn("456"); when(lockInfos.isEmpty()).thenReturn(true); when(lockService.retrieveLockInfos(path, true)).thenReturn(lockInfos); PathOperationRestriction result = lockRestrictor.hasRestriction(path); assertNull(result); }