Skip to content
Merged
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 @@ -110,8 +110,7 @@ public void processAfter(CdsReadEventContext context, List<CdsData> data) {
private List<String> getAttachmentAssociations(CdsModel model, CdsEntity entity, String associationName,
List<String> processedEntities) {
var associationNames = new ArrayList<String>();
var baseEntity = ApplicationHandlerHelper.getBaseEntity(model, entity);
if (ApplicationHandlerHelper.isMediaEntity(baseEntity)) {
if (ApplicationHandlerHelper.isMediaEntity(entity)) {
associationNames.add(associationName);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,8 @@
import com.sap.cds.feature.attachments.handler.constants.ModelConstants;
import com.sap.cds.reflect.CdsElement;
import com.sap.cds.reflect.CdsEntity;
import com.sap.cds.reflect.CdsModel;
import com.sap.cds.reflect.CdsStructuredType;
import com.sap.cds.services.draft.Drafts;
import com.sap.cds.services.utils.model.CdsModelUtils;

/**
* The class {@link ApplicationHandlerHelper} provides helper methods for the attachment application handlers.
Expand Down Expand Up @@ -96,11 +94,6 @@ public static Map<String, Object> removeDraftKeys(Map<String, Object> keys) {
return keyMap;
}

public static CdsEntity getBaseEntity(CdsModel model, CdsEntity entity) {
var entityResultOptional = entity.query().map(q -> CdsModelUtils.getEntityPath(q, model).rootEntity());
return entityResultOptional.orElseGet(() -> model.findEntity(entity.getQualifiedName()).orElseThrow());
}

private static boolean hasElementAnnotation(CdsElement element, String annotation) {
return element.findAnnotation(annotation).isPresent();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,7 @@ private List<LinkedList<AssociationIdentifier>> getAttachmentAssociationPath(Cds
var localProcessEntities = new ArrayList<String>();
currentList.set(new LinkedList<>());

var baseEntity = ApplicationHandlerHelper.getBaseEntity(model, entity);
var isMediaEntity = ApplicationHandlerHelper.isMediaEntity(baseEntity);
var isMediaEntity = ApplicationHandlerHelper.isMediaEntity(entity);
if (isMediaEntity) {
var identifier = new AssociationIdentifier(associationName, entity.getQualifiedName());
firstList.addLast(identifier);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,16 @@
package com.sap.cds.feature.attachments.handler.common.model;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

/**
* The class {@link NodeTree} is a tree data structure that holds the association identifier and its children.
*/
public class NodeTree {

final AssociationIdentifier identifier;
final List<NodeTree> children = new ArrayList<>();
private final AssociationIdentifier identifier;
private final List<NodeTree> children = new ArrayList<>();

public NodeTree(AssociationIdentifier identifier) {
this.identifier = identifier;
Expand Down Expand Up @@ -52,7 +53,7 @@ public AssociationIdentifier getIdentifier() {
}

public List<NodeTree> getChildren() {
return children;
return Collections.unmodifiableList(children);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ private void startScanning(CdsEntity attachmentEntityToScan, String contentId) {
logger.debug("Transaction completed. Starting to scan attachment asynchronously.");
Supplier<Void> executeAdapterSupplier = () -> {
// run malware scan asynchronously with current request context
runner.run((context) -> {
runner.run(context -> {
logger.info("Starting to scan attachment");
attachmentMalwareScanner.scanAttachment(attachmentEntityToScan, contentId);
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.List;
import java.util.stream.Collectors;

import org.junit.jupiter.api.Test;

Expand All @@ -20,7 +19,7 @@ void registrationFileAvailable() throws IOException {
String path = "src/main/resources/META-INF/services/com.sap.cds.services.runtime.CdsRuntimeConfiguration";

BufferedReader reader = Files.newBufferedReader(Paths.get(path));
List<String> classes = reader.lines().collect(Collectors.toList());
List<String> classes = reader.lines().toList();
reader.close();

String runtimeRegistrationName = Registration.class.getCanonicalName();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -125,9 +125,9 @@ void readonlyDataFilledForDraftActivate() {
verifyNoInteractions(eventFactory, event);
assertThat(attachment.get(DRAFT_READONLY_CONTEXT)).isNotNull();
var readOnlyData = (CdsData) attachment.get(DRAFT_READONLY_CONTEXT);
assertThat(readOnlyData).containsEntry(Attachment.CONTENT_ID, attachment.getContentId());
assertThat(readOnlyData).containsEntry(Attachment.STATUS, attachment.getStatus());
assertThat(readOnlyData).containsEntry(Attachment.SCANNED_AT, attachment.getScannedAt());
assertThat(readOnlyData).containsEntry(Attachment.CONTENT_ID, attachment.getContentId())
.containsEntry(Attachment.STATUS, attachment.getStatus())
.containsEntry(Attachment.SCANNED_AT, attachment.getScannedAt());
}

@Test
Expand All @@ -149,9 +149,8 @@ void readonlyDataClearedIfNotDraftActivate() {

verifyNoInteractions(eventFactory, event);
assertThat(createAttachment.get(DRAFT_READONLY_CONTEXT)).isNull();
assertThat(createAttachment).containsEntry(Attachment.CONTENT_ID, contentId);
assertThat(createAttachment).doesNotContainKey(Attachment.STATUS);
assertThat(createAttachment).doesNotContainKey(Attachment.SCANNED_AT);
assertThat(createAttachment).containsEntry(Attachment.CONTENT_ID, contentId)
.doesNotContainKey(Attachment.STATUS).doesNotContainKey(Attachment.SCANNED_AT);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@

class UpdateAttachmentsHandlerTest {

private static final String UP__ID = "up__ID";
private static final String UP_ID = "up__ID";
private static final String DRAFT_READONLY_CONTEXT = "DRAFT_READONLY_CONTEXT";
private static CdsRuntime runtime;

Expand Down Expand Up @@ -156,9 +156,9 @@ void readonlyDataFilledForDraftActivate() {
verifyNoInteractions(eventFactory, event);
assertThat(updateAttachment.get(DRAFT_READONLY_CONTEXT)).isNotNull();
var readOnlyUpdateData = (CdsData) updateAttachment.get(DRAFT_READONLY_CONTEXT);
assertThat(readOnlyUpdateData).containsEntry(Attachment.CONTENT_ID, updateAttachment.getContentId());
assertThat(readOnlyUpdateData).containsEntry(Attachment.STATUS, updateAttachment.getStatus());
assertThat(readOnlyUpdateData).containsEntry(Attachment.SCANNED_AT, updateAttachment.getScannedAt());
assertThat(readOnlyUpdateData).containsEntry(Attachment.CONTENT_ID, updateAttachment.getContentId())
.containsEntry(Attachment.STATUS, updateAttachment.getStatus())
.containsEntry(Attachment.SCANNED_AT, updateAttachment.getScannedAt());
}

@Test
Expand All @@ -180,9 +180,8 @@ void readonlyDataClearedIfNotDraftActivate() {

verifyNoInteractions(eventFactory, event);
assertThat(updateAttachment.get(DRAFT_READONLY_CONTEXT)).isNull();
assertThat(updateAttachment).containsEntry(Attachment.CONTENT_ID, contentId);
assertThat(updateAttachment).doesNotContainKey(Attachment.STATUS);
assertThat(updateAttachment).doesNotContainKey(Attachment.SCANNED_AT);
assertThat(updateAttachment).containsEntry(Attachment.CONTENT_ID, contentId)
.doesNotContainKey(Attachment.STATUS).doesNotContainKey(Attachment.SCANNED_AT);
}

@Test
Expand Down Expand Up @@ -280,7 +279,7 @@ void noKeysNoException() {
void selectIsUsedWithFilterAndWhere() {
var attachment = Attachments.create();
attachment.setId(UUID.randomUUID().toString());
attachment.put(UP__ID, "test_full");
attachment.put(UP_ID, "test_full");
attachment.setContent(mock(InputStream.class));
var entityWithKeys = CQL.entity(Attachment_.CDS_NAME).matching(getAttachmentKeyMap(attachment));
CqnUpdate update = Update.entity(entityWithKeys).byId("test");
Expand All @@ -293,14 +292,14 @@ void selectIsUsedWithFilterAndWhere() {
var select = selectCaptor.getValue();
assertThat(select.toString()).contains(getRefString("$key", "test"));
assertThat(select.toString()).contains(getRefString(Attachment.ID, attachment.getId()));
assertThat(select.toString()).contains(getRefString(UP__ID, (String) attachment.get(UP__ID)));
assertThat(select.toString()).contains(getRefString(UP_ID, (String) attachment.get(UP_ID)));
}

@Test
void selectIsUsedWithFilter() {
var attachment = Attachments.create();
attachment.setId(UUID.randomUUID().toString());
attachment.put(UP__ID, "test_filter");
attachment.put(UP_ID, "test_filter");
attachment.setContent(mock(InputStream.class));
var entityWithKeys = CQL.entity(Attachment_.CDS_NAME).matching(getAttachmentKeyMap(attachment));
CqnUpdate update = Update.entity(entityWithKeys);
Expand All @@ -312,14 +311,14 @@ void selectIsUsedWithFilter() {
verify(attachmentsReader).readAttachments(eq(runtime.getCdsModel()), eq(serviceEntity), selectCaptor.capture());
var select = selectCaptor.getValue();
assertThat(select.toString()).contains(getRefString(Attachment.ID, attachment.getId()));
assertThat(select.toString()).contains(getRefString(UP__ID, (String) attachment.get(UP__ID)));
assertThat(select.toString()).contains(getRefString(UP_ID, (String) attachment.get(UP_ID)));
}

@Test
void selectIsUsedWithWhere() {
var attachment = Attachments.create();
attachment.setId(UUID.randomUUID().toString());
attachment.put(UP__ID, "test_where");
attachment.put(UP_ID, "test_where");
attachment.setContent(mock(InputStream.class));
CqnUpdate update = Update.entity(Attachment_.CDS_NAME).byId("test");
var serviceEntity = runtime.getCdsModel().findEntity(Attachment_.CDS_NAME).orElseThrow();
Expand All @@ -330,15 +329,15 @@ void selectIsUsedWithWhere() {
verify(attachmentsReader).readAttachments(eq(runtime.getCdsModel()), eq(serviceEntity), selectCaptor.capture());
var select = selectCaptor.getValue();
assertThat(select.toString()).doesNotContain(Attachment.ID);
assertThat(select.toString()).doesNotContain(UP__ID);
assertThat(select.toString()).doesNotContain(UP_ID);
assertThat(select.toString()).contains(getRefString("$key", "test"));
}

@Test
void selectIsUsedWithAttachmentId() {
var attachment = Attachments.create();
attachment.setId(UUID.randomUUID().toString());
attachment.put(UP__ID, "test_up_id");
attachment.put(UP_ID, "test_up_id");
attachment.setContent(mock(InputStream.class));
var serviceEntity = runtime.getCdsModel().findEntity(Attachment_.CDS_NAME).orElseThrow();
CqnUpdate update = Update.entity(Attachment_.class).where(entity -> entity.ID().eq(attachment.getId()));
Expand All @@ -349,18 +348,18 @@ void selectIsUsedWithAttachmentId() {
verify(attachmentsReader).readAttachments(eq(runtime.getCdsModel()), eq(serviceEntity), selectCaptor.capture());
var select = selectCaptor.getValue();
assertThat(select.toString()).contains(getRefString(Attachment.ID, attachment.getId()));
assertThat(select.toString()).doesNotContain(UP__ID);
assertThat(select.toString()).doesNotContain(UP_ID);
}

@Test
void selectIsCorrectForMultipleAttachments() {
var attachment1 = Attachments.create();
attachment1.setId(UUID.randomUUID().toString());
attachment1.put(UP__ID, "test_multiple 2");
attachment1.put(UP_ID, "test_multiple 2");
attachment1.setContent(mock(InputStream.class));
var attachment2 = Attachments.create();
attachment2.setId(UUID.randomUUID().toString());
attachment2.put(UP__ID, "test_multiple 2");
attachment2.put(UP_ID, "test_multiple 2");
attachment2.setContent(mock(InputStream.class));
CqnUpdate update = Update.entity(Attachment_.class).where(
attachment -> attachment.ID().eq(attachment1.getId()).or(attachment.ID().eq(attachment2.getId())));
Expand Down Expand Up @@ -466,7 +465,7 @@ private void mockTargetInUpdateContext(CdsEntity serviceEntity, CqnUpdate update
}

private Map<String, Object> getAttachmentKeyMap(Attachments attachment) {
return Map.of(Attachment.ID, attachment.getId(), "up__ID", attachment.get(UP__ID));
return Map.of(Attachment.ID, attachment.getId(), "up__ID", attachment.get(UP_ID));
}

private String getRefString(String key, String value) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,8 @@ void documentIsExternallyDeleted() {
verify(attachmentService).markAttachmentAsDeleted(deletionInputCaptor.capture());
assertThat(deletionInputCaptor.getValue().contentId()).isEqualTo(contentId);
assertThat(deletionInputCaptor.getValue().userInfo()).isEqualTo(userInfo);
assertThat(currentData).containsEntry(Attachments.CONTENT_ID, null);
assertThat(currentData).containsEntry(Attachments.STATUS, null);
assertThat(currentData).containsEntry(Attachments.SCANNED_AT, null);
assertThat(currentData).containsEntry(Attachments.CONTENT_ID, null).containsEntry(Attachments.STATUS, null)
.containsEntry(Attachments.SCANNED_AT, null);
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ void singlePathEntryWorksCorrectly(String associationName) {
var cut = new NodeTree(identifier);
cut.addPath(List.of(identifier));

assertThat(cut.children).isEmpty();
assertThat(cut.getChildren()).isEmpty();
}

@Test
Expand Down Expand Up @@ -65,26 +65,26 @@ void addComplexPathWithMultipleChildren() {
cut.addPath(List.of(identifier1, identifier4, identifier2, identifier3));
cut.addPath(List.of(identifier1, identifier4, identifier4));

assertThat(cut.children).hasSize(3);
assertThat(cut.children.get(0).identifier).isEqualTo(identifier2);
assertThat(cut.children.get(0).children).hasSize(1);
assertThat(cut.children.get(0).children.get(0).identifier).isEqualTo(identifier3);
assertThat(cut.children.get(0).children.get(0).children).isNotNull().isEmpty();

assertThat(cut.children.get(1).identifier).isEqualTo(identifier3);
assertThat(cut.children.get(1).children).hasSize(1);
assertThat(cut.children.get(1).children.get(0).identifier).isEqualTo(identifier4);
assertThat(cut.children.get(1).children.get(0).children).isNotNull().isEmpty();

assertThat(cut.children.get(2).identifier).isEqualTo(identifier4);
assertThat(cut.children.get(2).children).hasSize(2);
assertThat(cut.children.get(2).children.get(0).identifier).isEqualTo(identifier2);
assertThat(cut.children.get(2).children.get(0).children).hasSize(1);
assertThat(cut.children.get(2).children.get(0).children.get(0).identifier).isEqualTo(identifier3);
assertThat(cut.children.get(2).children.get(0).children.get(0).children).isNotNull().isEmpty();

assertThat(cut.children.get(2).children.get(1).identifier).isEqualTo(identifier4);
assertThat(cut.children.get(2).children.get(1).children).isNotNull().isEmpty();
assertThat(cut.getChildren()).hasSize(3);
assertThat(cut.getChildren().get(0).getIdentifier()).isEqualTo(identifier2);
assertThat(cut.getChildren().get(0).getChildren()).hasSize(1);
assertThat(cut.getChildren().get(0).getChildren().get(0).getIdentifier()).isEqualTo(identifier3);
assertThat(cut.getChildren().get(0).getChildren().get(0).getChildren()).isNotNull().isEmpty();

assertThat(cut.getChildren().get(1).getIdentifier()).isEqualTo(identifier3);
assertThat(cut.getChildren().get(1).getChildren()).hasSize(1);
assertThat(cut.getChildren().get(1).getChildren().get(0).getIdentifier()).isEqualTo(identifier4);
assertThat(cut.getChildren().get(1).getChildren().get(0).getChildren()).isNotNull().isEmpty();

assertThat(cut.getChildren().get(2).getIdentifier()).isEqualTo(identifier4);
assertThat(cut.getChildren().get(2).getChildren()).hasSize(2);
assertThat(cut.getChildren().get(2).getChildren().get(0).getIdentifier()).isEqualTo(identifier2);
assertThat(cut.getChildren().get(2).getChildren().get(0).getChildren()).hasSize(1);
assertThat(cut.getChildren().get(2).getChildren().get(0).getChildren().get(0).getIdentifier()).isEqualTo(identifier3);
assertThat(cut.getChildren().get(2).getChildren().get(0).getChildren().get(0).getChildren()).isNotNull().isEmpty();

assertThat(cut.getChildren().get(2).getChildren().get(1).getIdentifier()).isEqualTo(identifier4);
assertThat(cut.getChildren().get(2).getChildren().get(1).getChildren()).isNotNull().isEmpty();
}

@Test
Expand All @@ -96,16 +96,16 @@ void nodeNotInPathWillDoNothing() {
var cut = new NodeTree(identifier1);
cut.addPath(List.of(identifier2, identifier3));

assertThat(cut.children).isEmpty();
assertThat(cut.getChildren()).isEmpty();
}

private void verifyTwoNodeHierarchies(NodeTree cut, AssociationIdentifier identifier2,
AssociationIdentifier identifier3) {
assertThat(cut.children).hasSize(1);
assertThat(cut.children.get(0).identifier).isEqualTo(identifier2);
assertThat(cut.children.get(0).children).hasSize(1);
assertThat(cut.children.get(0).children.get(0).identifier).isEqualTo(identifier3);
assertThat(cut.children.get(0).children.get(0).children).isEmpty();
assertThat(cut.getChildren()).hasSize(1);
assertThat(cut.getChildren().get(0).getIdentifier()).isEqualTo(identifier2);
assertThat(cut.getChildren().get(0).getChildren()).hasSize(1);
assertThat(cut.getChildren().get(0).getChildren().get(0).getIdentifier()).isEqualTo(identifier3);
assertThat(cut.getChildren().get(0).getChildren().get(0).getChildren()).isEmpty();
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ void tearDown() {
@Test
void notCompletedTransactionDoNothing() {
var executionDone = new AtomicBoolean();
doAnswer((invocation) -> {
doAnswer(invocation -> {
executionDone.set(true);
return null;
}).when(attachmentMalwareScanner).scanAttachment(attachmentEntity, contentId);
Expand All @@ -82,7 +82,7 @@ void notCompletedTransactionDoNothing() {
void completedTransactionScanAttachments() {
var executionDone = new AtomicBoolean();
var usedThread = new AtomicReference<String>();
doAnswer((invocation) -> {
doAnswer(invocation -> {
executionDone.set(true);
usedThread.set(Thread.currentThread().getName());
return null;
Expand All @@ -97,7 +97,7 @@ void completedTransactionScanAttachments() {

@Test
void exceptionDuringScanningLogged() {
doAnswer((invocation) -> {
doAnswer(invocation -> {
throw new RuntimeException("Some exception");
}).when(attachmentMalwareScanner).scanAttachment(attachmentEntity, contentId);
observer.start();
Expand All @@ -111,7 +111,7 @@ void exceptionDuringScanningLogged() {
void directScanCallScanAttachments() {
var executionDone = new AtomicBoolean();
var usedThread = new AtomicReference<String>();
doAnswer((invocation) -> {
doAnswer(invocation -> {
executionDone.set(true);
usedThread.set(Thread.currentThread().getName());
return null;
Expand All @@ -126,7 +126,7 @@ void directScanCallScanAttachments() {

@Test
void exceptionDuringScanningLoggedForDirectScanCall() {
doAnswer((invocation) -> {
doAnswer(invocation -> {
throw new RuntimeException("Some exception");
}).when(attachmentMalwareScanner).scanAttachment(attachmentEntity, contentId);
observer.start();
Expand Down
Loading