### Question:
SetAsPrimaryObjectService { public void setAsPrimaryObject(AgentPrincipals agent, PID fileObjPid) { try (Timer.Context context = timer.time()) { aclService.assertHasAccess("Insufficient privileges to set " + fileObjPid.getUUID() + " as primary object", fileObjPid, agent.getPrincipals(), editResourceType); RepositoryObject repoObj = repoObjLoader.getRepositoryObject(fileObjPid); if (!(repoObj instanceof FileObject)) { throw new InvalidOperationForObjectType("Cannot set " + fileObjPid.getUUID() + " as primary object, since objects of type " + repoObj.getClass().getName() + " are not eligible."); } RepositoryObject parent = repoObj.getParent(); if (!(parent instanceof WorkObject)) { throw new InvalidOperationForObjectType("Object of type " + parent.getClass().getName() + " cannot have a primary object."); } WorkObject work = (WorkObject) parent; work.setPrimaryObject(fileObjPid); operationsMessageSender.sendSetAsPrimaryObjectOperation(agent.getUsername(), Arrays.asList(parent.getPid(), fileObjPid)); } }  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 setFileObjectAsPrimaryTest() { when(fileObj.getParent()).thenReturn(workObj); service.setAsPrimaryObject(agent, fileObjPid); verify(workObj).setPrimaryObject(fileObjPid); 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(expected = AccessRestrictionException.class) public void insufficientAccessTest() { doThrow(new AccessRestrictionException()).when(aclService) .assertHasAccess(anyString(), eq(fileObjPid), any(AccessGroupSet.class), eq(editResourceType)); service.setAsPrimaryObject(agent, fileObjPid); }
@Test(expected = InvalidOperationForObjectType.class) public void setNonFileAsPrimaryTest() { when(repoObjLoader.getRepositoryObject(eq(folderObjPid))).thenReturn(folderObj); service.setAsPrimaryObject(agent, folderObjPid); }
@Test(expected = InvalidOperationForObjectType.class) public void setPrimaryObjectOnNonWork() { when(fileObj.getParent()).thenReturn(folderObj); service.setAsPrimaryObject(agent, fileObjPid); }