DefaultSampleService implements SampleService, PaginatedDataSource<Sample> { @Override public void deleteNote(Sample sample, Long noteId) throws IOException { if (noteId == null) { throw new IllegalArgumentException("Cannot delete an unsaved Note"); } Sample managed = sampleStore.get(sample.getId()); Note deleteNote = null; for (Note note : managed.getNotes()) { if (note.getId() == noteId.longValue()) { deleteNote = note; break; } } if (deleteNote == null) { throw new IOException("Note " + noteId + " not found for Sample " + sample.getId()); } authorizationManager.throwIfNonAdminOrMatchingOwner(deleteNote.getOwner()); managed.getNotes().remove(deleteNote); sampleStore.save(managed); }  void setSampleStore(SampleStore sampleStore); void setAuthorizationManager(AuthorizationManager authorizationManager); void setSampleClassService(SampleClassService sampleClassService); void setSampleValidRelationshipService(SampleValidRelationshipService sampleValidRelationshipService); void setProjectStore(ProjectStore projectStore); void setTissueOriginDao(TissueOriginDao tissueOriginDao); void setTissueTypeDao(TissueTypeDao tissueTypeDao); void setDetailedQcStatusService(DetailedQcStatusService detailedQcStatusService); void setSubprojectService(SubprojectService subprojectService); void setSamplePurposeDao(SamplePurposeDao samplePurposeDao); void setTissueMaterialDao(TissueMaterialDao tissueMaterialDao); void setLabService(LabService labService); void setBoxService(BoxService boxService); void setWorksetService(WorksetService worksetService); void setFileAttachmentService(FileAttachmentService fileAttachmentService); void setNamingSchemeHolder(NamingSchemeHolder namingSchemeHolder); @CoverageIgnore Boolean getAutoGenerateIdBarcodes(); @CoverageIgnore void setAutoGenerateIdBarcodes(Boolean autoGenerateIdBarcodes); @CoverageIgnore Boolean isUniqueExternalNameWithinProjectRequired(); @CoverageIgnore void setUniqueExternalNameWithinProjectRequired(Boolean uniqueExternalNameWithinProjectRequired); @Override Sample get(long sampleId); @Override long create(Sample sample); @Override void confirmExternalNameUniqueForProjectIfRequired(String newExternalName, Sample sample); boolean isValidRelationship(Sample parent, Sample child); @Override long update(Sample sample); @Override List<Sample> list(); @Override Long countAll(); @Override Collection<Sample> listByProjectId(long projectId); @Override List<Sample> listByIdList(List<Long> idList); @Override @Transactional(propagation = Propagation.REQUIRED) Collection<SampleIdentity> getIdentitiesByExternalNameOrAliasAndProject(String externalName, Long projectId, boolean exactMatch); @Override List<Sample> getByAlias(String alias); @Override Sample getByBarcode(String barcode); @Override void addNote(Sample sample, Note note); @Override void deleteNote(Sample sample, Long noteId); @Override AuthorizationManager getAuthorizationManager(); @Override DeletionStore getDeletionStore(); void setDeletionStore(DeletionStore deletionStore); @Override void authorizeDeletion(Sample object); @Override ValidationResult validateDeletion(Sample object); @Override void beforeDelete(Sample object); @Override void afterDelete(Sample object); @Override Sample save(Sample sample); @Override long count(Consumer<String> errorHandler, PaginationFilter... filter); @Override List<Sample> list(Consumer<String> errorHandler, int offset, int limit, boolean sortDir, String sortCol,
      PaginationFilter... filter); @Override EntityReference getNextInProject(Sample sample); @Override EntityReference getPreviousInProject(Sample sample); @Override TransactionTemplate getTransactionTemplate();  }
@Test public void testDeleteNote() throws Exception { Sample paramSample = new SampleImpl(); paramSample.setId(1L); paramSample.setAlias("paramSample"); Sample dbSample = new SampleImpl(); dbSample.setId(paramSample.getId()); dbSample.setAlias("persistedSample"); Note note = new Note(); note.setId(3L); User owner = new UserImpl(); owner.setId(5L); note.setOwner(owner); dbSample.addNote(note); Mockito.when(sampleStore.get(paramSample.getId())).thenReturn(dbSample); sut.deleteNote(paramSample, note.getId()); Mockito.verify(authorizationManager).throwIfNonAdminOrMatchingOwner(owner); ArgumentCaptor<Sample> capture = ArgumentCaptor.forClass(Sample.class); Mockito.verify(sampleStore).save(capture.capture()); Sample savedSample = capture.getValue(); assertTrue(savedSample.getNotes().isEmpty()); }