### Question:
SetAsPrimaryObjectService { public void clearPrimaryObject(AgentPrincipals agent, PID objPid) { try (Timer.Context context = timer.time()) { RepositoryObject repoObj = repoObjLoader.getRepositoryObject(objPid); RepositoryObject parent; if (repoObj instanceof FileObject) { parent = repoObj.getParent(); } else { parent = repoObj; } aclService.assertHasAccess("Insufficient privileges to clear primary object for " + parent.getPid().getId(), parent.getPid(), agent.getPrincipals(), editResourceType); if (!(parent instanceof WorkObject)) { throw new InvalidOperationForObjectType("Object of type " + parent.getClass().getName() + " cannot have a primary object."); } WorkObject work = (WorkObject) parent; List<PID> updated = new ArrayList<>(); updated.add(parent.getPid()); FileObject previousPrimary = work.getPrimaryObject(); if (previousPrimary != null) { updated.add(previousPrimary.getPid()); work.clearPrimaryObject(); } operationsMessageSender.sendSetAsPrimaryObjectOperation(agent.getUsername(), updated); } }  void setAsPrimaryObject(AgentPrincipals agent, PID fileObjPid); void clearPrimaryObject(AgentPrincipals agent, PID objPid); void setAclService(AccessControlService aclService); void setRepositoryObjectLoader(RepositoryObjectLoader repoObjLoader); void setOperationsMessageSender(OperationsMessageSender operationsMessageSender);  }### Answer:
@Test public void testClearPrimaryObject() { when(fileObj.getParent()).thenReturn(workObj); when(workObj.getPrimaryObject()).thenReturn(fileObj); service.clearPrimaryObject(agent, fileObjPid); verify(workObj).clearPrimaryObject(); verify(messageSender).sendSetAsPrimaryObjectOperation(anyString(), pidsCaptor.capture()); Collection<PID> collections = pidsCaptor.getValue(); assertEquals(collections.size(), 2); assertTrue(collections.contains(fileObjPid)); assertTrue(collections.contains(fileObj.getParent().getPid())); }
@Test public void testClearPrimaryObjectViaWork() { when(fileObj.getParent()).thenReturn(workObj); when(workObj.getPrimaryObject()).thenReturn(fileObj); service.clearPrimaryObject(agent, workObjPid); verify(workObj).clearPrimaryObject(); verify(messageSender).sendSetAsPrimaryObjectOperation(anyString(), pidsCaptor.capture()); Collection<PID> collections = pidsCaptor.getValue(); assertEquals(collections.size(), 2); assertTrue(collections.contains(fileObjPid)); assertTrue(collections.contains(workObjPid)); }
@Test public void testClearPrimaryObjectNoPrimarySet() { when(fileObj.getParent()).thenReturn(workObj); service.clearPrimaryObject(agent, workObjPid); verify(messageSender).sendSetAsPrimaryObjectOperation(anyString(), pidsCaptor.capture()); Collection<PID> collections = pidsCaptor.getValue(); assertEquals(collections.size(), 1); assertTrue(collections.contains(workObjPid)); }
@Test(expected = AccessRestrictionException.class) public void testClearPrimaryObjectAccessRestriction() { doThrow(new AccessRestrictionException()).when(aclService) .assertHasAccess(anyString(), eq(workObjPid), any(AccessGroupSet.class), eq(editResourceType)); when(fileObj.getParent()).thenReturn(workObj); service.clearPrimaryObject(agent, fileObjPid); }