### Question:
WorkObject extends ContentContainerObject { public void setPrimaryObject(PID primaryPid) { RepositoryObject primaryObj = driver.getRepositoryObject(primaryPid); if (!(primaryObj instanceof FileObject)) { throw new InvalidOperationForObjectType("Cannot set " + primaryPid.getUUID() + " as primary object, since objects of type " + primaryObj.getClass().getName() + " are not eligible."); } RepositoryObject parent = primaryObj.getParent(); if (!parent.getPid().equals(getPid())) { throw new InvalidRelationshipException("Primary object must be a member of the Work"); } repoObjFactory.createExclusiveRelationship(this, Cdr.primaryObject, primaryObj.getResource()); } protected  WorkObject(PID pid, RepositoryObjectDriver driver, RepositoryObjectFactory repoObjFactory); @Override WorkObject validateType(); void clearPrimaryObject(); void setPrimaryObject(PID primaryPid); FileObject getPrimaryObject(); @Override ContentContainerObject addMember(ContentObject member); FileObject addDataFile(URI storageUri, String filename, String mimetype,
            String sha1Checksum, String md5Checksum); FileObject addDataFile(URI storageUri, String filename,
            String mimetype, String sha1Checksum, String md5Checksum, Model model); FileObject addDataFile(PID filePid, URI storageUri, String filename,
            String mimetype, String sha1Checksum, String md5Checksum, Model model);  }### Answer:
@Test public void setPrimaryObjectTest() { PID primaryPid = makePid(); Resource primaryResc = createResource(primaryPid.getURI()); when(fileObj.getResource()).thenReturn(primaryResc); work.setPrimaryObject(primaryPid); verify(repoObjFactory).createExclusiveRelationship(any(RepositoryObject.class), eq(Cdr.primaryObject), eq(primaryResc)); }
@Test(expected = InvalidRelationshipException.class) public void setPrimaryObjectToNonMemberTest() { PID anotherPid = makePid(); WorkObject anotherWork = mock(WorkObject.class); when(anotherWork.getPid()).thenReturn(anotherPid); when(fileObj.getParent()).thenReturn(anotherWork); PID primaryPid = makePid(); try { work.setPrimaryObject(primaryPid); } finally { verify(repoObjFactory, never()).createExclusiveRelationship(any(RepositoryObject.class), eq(Cdr.primaryObject), any(Resource.class)); } }
@Test(expected = InvalidOperationForObjectType.class) public void setPrimaryObjectToInvalidTypeTest() { PID anotherPid = makePid(); WorkObject anotherWork = mock(WorkObject.class); when(anotherWork.getPid()).thenReturn(anotherPid); when(driver.getRepositoryObject(eq(anotherPid))).thenReturn(anotherWork); try { work.setPrimaryObject(anotherPid); } finally { verify(repoObjFactory, never()).createExclusiveRelationship(any(RepositoryObject.class), eq(Cdr.primaryObject), any(Resource.class)); } }