### Question:
EditFilenameService { public void editLabel(AgentPrincipals agent, PID pid, String label) { FedoraTransaction tx = txManager.startTransaction(); try (Timer.Context context = timer.time()) { aclService.assertHasAccess( "User does not have permissions to edit filenames", pid, agent.getPrincipals(), Permission.editDescription); RepositoryObject obj = repoObjLoader.getRepositoryObject(pid); if (!(obj instanceof FileObject)) { throw new IllegalArgumentException("Failed to edit filename for " + obj.getPid()); } BinaryObject binaryObj = ((FileObject) obj).getOriginalFile(); String oldLabel = getOldLabel(binaryObj.getFilename()); repoObjFactory.createExclusiveRelationship(binaryObj, Ebucore.filename, label); repoObjFactory.createExclusiveRelationship(obj, DcElements.title, label); obj.getPremisLog() .buildEvent(Premis.FilenameChange) .addImplementorAgent(AgentPids.forPerson(agent)) .addEventDetail("Object renamed from " + oldLabel + " to " + label) .writeAndClose(); } catch (Exception e) { tx.cancel(e); } finally { tx.close(); } operationsMessageSender.sendUpdateDescriptionOperation( agent.getUsername(), Arrays.asList(pid)); } EditFilenameService(); void editLabel(AgentPrincipals agent, PID pid, String label); void setAclService(AccessControlService aclService); void setRepositoryObjectFactory(RepositoryObjectFactory repoObjFactory); void setRepositoryObjectLoader(RepositoryObjectLoader repoObjLoader); void setTransactionManager(TransactionManager txManager); void setOperationsMessageSender(OperationsMessageSender operationsMessageSender);  }### Answer:
@Test public void editFilenameTest() { when(binaryObj.getFilename()).thenReturn("Old file name"); String label = "a brand-new title!"; service.editLabel(agent, pid, label); verify(repoObjFactory).createExclusiveRelationship(eq(binaryObj), eq(Ebucore.filename), eq(label)); verify(repoObjFactory).createExclusiveRelationship(eq(repoObj), eq(DcElements.title), eq(label)); verify(premisLogger).buildEvent(eq(Premis.FilenameChange)); verify(eventBuilder).addEventDetail(labelCaptor.capture()); assertEquals(labelCaptor.getValue(), "Object renamed from Old file name to " + label); verify(eventBuilder).writeAndClose(); verify(messageSender).sendUpdateDescriptionOperation(anyString(), pidCaptor.capture()); assertEquals(pid, pidCaptor.getValue().get(0)); }
@Test public void editNoFilenameTest() { String label = "a brand-new title too!"; service.editLabel(agent, pid, label); verify(repoObjFactory).createExclusiveRelationship(eq(binaryObj), eq(Ebucore.filename), eq(label)); verify(repoObjFactory).createExclusiveRelationship(eq(repoObj), eq(DcElements.title), eq(label)); verify(premisLogger).buildEvent(eq(Premis.FilenameChange)); verify(eventBuilder).addEventDetail(labelCaptor.capture()); assertEquals(labelCaptor.getValue(), "Object renamed from no ebucore:filename to " + label); verify(eventBuilder).writeAndClose(); verify(messageSender).sendUpdateDescriptionOperation(anyString(), pidCaptor.capture()); assertEquals(pid, pidCaptor.getValue().get(0)); }
@Test public void editFilenamelNonFileObjTest() { when(repoObjLoader.getRepositoryObject(any(PID.class))).thenReturn(workObj); try { service.editLabel(agent, pid, "label"); } catch (Exception e) { assertEquals(e.getCause().getClass(), IllegalArgumentException.class); } }
@Test(expected = TransactionCancelledException.class) public void insufficientAccessTest() { doThrow(new AccessRestrictionException()).when(aclService) .assertHasAccess(anyString(), eq(pid), any(AccessGroupSet.class), eq(Permission.editDescription)); service.editLabel(agent, pid, "label"); }