Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -73,32 +73,51 @@ public void scanAttachment(CdsEntity attachmentEntity, String contentId) {
contentId,
attachmentEntity.getQualifiedName());

List<SelectionResult> selectionResult = selectData(attachmentEntity, contentId);

selectionResult.forEach(
result -> {
long rowCount = result.result().rowCount();
if (rowCount <= 0) {
logger.debug(
"No attachments {} found in entity {}, nothing to scan.",
contentId,
result.entity.getQualifiedName());
return;
}

if (rowCount > 1) {
logger.warn(
"More than one attachment {} found in entity {}.",
contentId,
result.entity.getQualifiedName());
throw new IllegalStateException(
"More than one attachment with contentId %s.".formatted(contentId));
}

Attachments attachment = result.result().single(Attachments.class);
MalwareScanResultStatus status = scanDocument(attachment);
updateData(result.entity, contentId, status);
});
List<SelectionResult> selectionResults = selectData(attachmentEntity, contentId);

MalwareScanResultStatus status = findAndScanAttachment(selectionResults, contentId);
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can you rename to findAndScanAttachments?


if (status == null) {
logger.debug("Attachment {} not found in any entity, skipping update.", contentId);
return;
}

// Update ALL candidate entities. This ensures the scan result is persisted
// even if the attachment moved between draft and active tables during the scan.
for (SelectionResult result : selectionResults) {
updateData(result.entity, contentId, status);
}
}

private MalwareScanResultStatus findAndScanAttachment(
List<SelectionResult> selectionResults, String contentId) {
return selectionResults.stream()
.filter(result -> validateAndFilter(result, contentId))
.findFirst()
.map(result -> scanDocument(result.result().single(Attachments.class)))
.orElse(null);
}

private boolean validateAndFilter(SelectionResult result, String contentId) {
long rowCount = result.result().rowCount();
if (rowCount <= 0) {
logger.debug(
"No attachments {} found in entity {}, nothing to scan.",
contentId,
result.entity.getQualifiedName());
return false;
}

if (rowCount > 1) {
logger.warn(
"More than one attachment {} found in entity {}.",
contentId,
result.entity.getQualifiedName());
throw new IllegalStateException(
"More than one attachment with contentId %s.".formatted(contentId));
}

return true;
}

private List<SelectionResult> selectData(CdsEntity attachmentEntity, String contentId) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ void contentTakenFromTheDatabaseSelect() {

cut.scanAttachment(entity.orElseThrow(), "");

verify(malwareScanClient, times(2)).scanContent(content);
verify(malwareScanClient, times(1)).scanContent(content);
verifyNoInteractions(attachmentService);
}

Expand All @@ -197,8 +197,8 @@ void contentTakenFromTheAttachmentService() {

cut.scanAttachment(entity.orElseThrow(), "");

verify(attachmentService, times(2)).readAttachment(contentId);
verify(malwareScanClient, times(2)).scanContent(content);
verify(attachmentService, times(1)).readAttachment(contentId);
verify(malwareScanClient, times(1)).scanContent(content);
}

@Test
Expand All @@ -218,7 +218,7 @@ void contentTakenFromTheAttachmentServiceForNonDraft() {
}

@Test
void noDataReturnedForUpdateNothingDoneForNonDraftEntity() {
void updateAttemptedForAllEntitiesEvenWhenActiveHasNoData() {
var entity = runtime.getCdsModel().findEntity(getTestServiceAttachmentName());
when(persistenceService.run(any(CqnSelect.class))).thenReturn(result);
when(result.rowCount()).thenReturn(1L).thenReturn(0L);
Expand All @@ -232,9 +232,10 @@ void noDataReturnedForUpdateNothingDoneForNonDraftEntity() {

cut.scanAttachment(entity.orElseThrow(), "ID");

verify(persistenceService).run(updateCaptor.capture());
var update = updateCaptor.getValue();
assertThat(update.ref().toString()).contains(entity.get().getQualifiedName());
verify(persistenceService, times(2)).run(updateCaptor.capture());
var updateList = updateCaptor.getAllValues();
assertThat(updateList).hasSize(2);
assertThat(updateList.get(0).ref().toString()).contains(entity.get().getQualifiedName());
}

@Test
Expand All @@ -251,19 +252,66 @@ void clientNotCalledIfNoInstanceBound() {
cut.scanAttachment(entity.orElseThrow(), "ID");

verifyNoInteractions(malwareScanClient);
verify(persistenceService).run(updateCaptor.capture());
verify(persistenceService, times(2)).run(updateCaptor.capture());
var updateList = updateCaptor.getAllValues();
assertThat(updateList)
.hasSize(1)
.first()
.satisfies(
.hasSize(2)
.allSatisfy(
update -> {
assertThat(update.entries()).hasSize(1);
assertThat(update.entries().get(0))
.containsEntry(Attachments.STATUS, StatusCode.CLEAN);
});
}

@Test
void scanResultWrittenToAllEntitiesEvenIfDraftDeletedDuringScanning() {
var entity = runtime.getCdsModel().findEntity(getTestServiceAttachmentName());
var content = mock(InputStream.class);
var draftData = Attachments.create();
draftData.setContent(content);
// Phase 1: draft has the row, active does not
when(persistenceService.run(any(CqnSelect.class))).thenReturn(result);
when(result.rowCount()).thenReturn(1L).thenReturn(0L);
when(result.single(Attachments.class)).thenReturn(draftData);
when(malwareScanClient.scanContent(any())).thenReturn(MalwareScanResultStatus.CLEAN);
// Phase 2: simulate draft deleted (0 rows updated), active now has the row (1 row updated)
var draftUpdateResult = mock(Result.class);
when(draftUpdateResult.rowCount()).thenReturn(0L);
var activeUpdateResult = mock(Result.class);
when(activeUpdateResult.rowCount()).thenReturn(1L);
when(persistenceService.run(any(CqnUpdate.class)))
.thenReturn(draftUpdateResult)
.thenReturn(activeUpdateResult);

cut.scanAttachment(entity.orElseThrow(), "ID");

// Scan should happen once from draft content
verify(malwareScanClient).scanContent(content);
// Updates should be attempted on both entities
verify(persistenceService, times(2)).run(updateCaptor.capture());
var updates = updateCaptor.getAllValues();
assertThat(updates).hasSize(2);
updates.forEach(
update -> {
assertThat(update.entries()).hasSize(1);
assertThat(update.entries().get(0)).containsEntry(Attachments.STATUS, StatusCode.CLEAN);
});
}

@Test
void noScanOrUpdateWhenAttachmentNotFoundInAnyEntity() {
var entity = runtime.getCdsModel().findEntity(getTestServiceAttachmentName());
var emptyResult = mock(Result.class);
when(emptyResult.rowCount()).thenReturn(0L);
when(persistenceService.run(any(CqnSelect.class))).thenReturn(emptyResult);

cut.scanAttachment(entity.orElseThrow(), "ID");

verifyNoInteractions(malwareScanClient);
verify(persistenceService, times(0)).run(any(CqnUpdate.class));
}

@Test
void mapStatus() {
assertEquals(
Expand Down
Loading