AttachmentAwareDatabaseHandler { public Set<Attachment> getAllAttachmentsToKeep(Set<Attachment> originalAttachments, Set<Attachment> changedAttachments) { Set <Attachment> attachmentsToKeep = new HashSet<>(); attachmentsToKeep.addAll(nullToEmptySet(changedAttachments)); Set<String> alreadyPresentIdsInAttachmentsToKeep = nullToEmptyCollection(attachmentsToKeep).stream().map(Attachment::getAttachmentContentId).collect(Collectors.toSet()); Set<Attachment> acceptedAttachmentsNotYetToKeep = nullToEmptySet(originalAttachments).stream().filter(a -> (a.getCheckStatus() == CheckStatus.ACCEPTED && !alreadyPresentIdsInAttachmentsToKeep.contains(a.getAttachmentContentId()))).collect(Collectors.toSet()); attachmentsToKeep.addAll(acceptedAttachmentsNotYetToKeep); return attachmentsToKeep; } AttachmentAwareDatabaseHandler(); Set<Attachment> getAllAttachmentsToKeep(Set<Attachment> originalAttachments, Set<Attachment> changedAttachments);  }
@Test public void testGetAllAttachmentsToKeep() throws Exception { Attachment attachmentAccepted = new Attachment().setAttachmentContentId("acceptedAtt").setFilename("att1.file").setCheckStatus(CheckStatus.ACCEPTED); Attachment attachmentRejected = new Attachment().setAttachmentContentId("rejectedAtt").setFilename("att2.file").setCheckStatus(CheckStatus.REJECTED); Set<Attachment> attachmentsBefore = new HashSet<>(); attachmentsBefore.add(attachmentAccepted); attachmentsBefore.add(attachmentRejected); Set<Attachment> attachmentsAfter = new HashSet<>(); Set<Attachment> attachmentsToKeep = handler.getAllAttachmentsToKeep(attachmentsBefore, attachmentsAfter); assertEquals(1, attachmentsToKeep.size()); assertTrue(attachmentsToKeep.contains(attachmentAccepted)); assertFalse(attachmentsToKeep.contains(attachmentRejected)); Attachment originalAttachment = new Attachment().setAttachmentContentId("att").setFilename("att.file").setCheckStatus(CheckStatus.ACCEPTED); attachmentsBefore = new HashSet<>(); attachmentsBefore.add(originalAttachment); Attachment changedAttachment = originalAttachment.deepCopy().setCheckStatus(CheckStatus.REJECTED); attachmentsAfter = new HashSet<>(); attachmentsAfter.add(changedAttachment); attachmentsToKeep = handler.getAllAttachmentsToKeep(attachmentsBefore, attachmentsAfter); assertEquals(1, attachmentsToKeep.size()); assertTrue(attachmentsToKeep.contains(changedAttachment)); }
@Test public void testGetAllAttachmentsToKeepCanHandleNull() throws Exception { Attachment attachmentAccepted1 = new Attachment().setAttachmentContentId("acceptedAtt1").setFilename("att1.file").setCheckStatus(CheckStatus.ACCEPTED); Attachment attachmentRejected1 = new Attachment().setAttachmentContentId("rejectedAtt1").setFilename("att2.file").setCheckStatus(CheckStatus.REJECTED); Attachment attachmentRejected2 = new Attachment().setAttachmentContentId("rejectedAtt2").setFilename("att3.file").setCheckStatus(CheckStatus.NOTCHECKED); Set<Attachment> attachments = new HashSet<>(); attachments.add(attachmentAccepted1); attachments.add(attachmentRejected1); attachments.add(attachmentRejected2); Set<Attachment> attachmentsToKeep = handler.getAllAttachmentsToKeep(attachments, null); assertEquals(1, attachmentsToKeep.size()); assertTrue(attachmentsToKeep.contains(attachmentAccepted1)); assertFalse(attachmentsToKeep.contains(attachmentRejected1)); assertFalse(attachmentsToKeep.contains(attachmentRejected2)); attachmentsToKeep = handler.getAllAttachmentsToKeep(null, attachments); assertEquals(3, attachmentsToKeep.size()); assertTrue(attachmentsToKeep.contains(attachmentAccepted1)); assertTrue(attachmentsToKeep.contains(attachmentRejected1)); assertTrue(attachmentsToKeep.contains(attachmentRejected2)); }