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 @@ -63,6 +63,7 @@ private SDMConstants() {
public static final Integer MAX_CONNECTIONS_PER_ROUTE = 50;
public static final Integer MAX_CONNECTIONS_TOTAL = 50;
public static final String REST_V2_REPOSITORIES = "rest/v2/repositories";
public static final String ANNOTATION_IS_MEDIA_DATA = "_is_media_data";

public static String nameConstraintMessage(
List<String> fileNameWithRestrictedCharacters, String operation) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,17 +4,13 @@
import com.sap.cds.ql.Predicate;
import com.sap.cds.ql.cqn.CqnSelect;
import com.sap.cds.ql.cqn.Modifier;
import com.sap.cds.reflect.CdsAssociationType;
import com.sap.cds.reflect.CdsElement;
import com.sap.cds.sdm.constants.SDMConstants;
import com.sap.cds.services.cds.ApplicationService;
import com.sap.cds.services.cds.CdsReadEventContext;
import com.sap.cds.services.handler.EventHandler;
import com.sap.cds.services.handler.annotations.Before;
import com.sap.cds.services.handler.annotations.HandlerOrder;
import com.sap.cds.services.handler.annotations.ServiceName;
import java.util.ArrayList;
import java.util.List;

@ServiceName(value = "*", type = ApplicationService.class)
public class SDMReadAttachmentsHandler implements EventHandler {
Expand All @@ -25,41 +21,20 @@ public SDMReadAttachmentsHandler() {}
@HandlerOrder(HandlerOrder.DEFAULT)
public void processBefore(CdsReadEventContext context) {
String repositoryId = SDMConstants.REPOSITORY_ID;
List<String> compositions = getEntityCompositions(context);
for (String composition : compositions) {
if (context.getTarget().getQualifiedName().contains(composition)) {
CqnSelect copy =
CQL.copy(
context.getCqn(),
new Modifier() {
@Override
public Predicate where(Predicate where) {
return CQL.and(where, CQL.get("repositoryId").eq(repositoryId));
}
});
context.setCqn(copy);
if (context.getTarget().getAnnotationValue(SDMConstants.ANNOTATION_IS_MEDIA_DATA, false)) {
CqnSelect copy =
CQL.copy(
context.getCqn(),
new Modifier() {
@Override
public Predicate where(Predicate where) {
return CQL.and(where, CQL.get("repositoryId").eq(repositoryId));
}
});
context.setCqn(copy);

} else {
context.setCqn(context.getCqn());
}
} else {
context.setCqn(context.getCqn());
}
}

private List<String> getEntityCompositions(CdsReadEventContext context) {
List<CdsElement> compositions = context.getTarget().compositions().toList();
List<String> attachmentsCompositionList = new ArrayList<>();
for (CdsElement cdsElement : compositions) {
if (cdsElement != null) {
CdsAssociationType cdsAssociationType = cdsElement.getType();
String targetAspect =
cdsAssociationType.getTargetAspect().isPresent()
? cdsAssociationType.getTargetAspect().get().getQualifiedName()
: null;
if (targetAspect != null && targetAspect.equalsIgnoreCase("sap.attachments.Attachments")) {
attachmentsCompositionList.add(cdsElement.getName());
}
}
}
return attachmentsCompositionList;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,11 +10,8 @@
import com.sap.cds.sdm.constants.SDMConstants;
import com.sap.cds.sdm.handler.applicationservice.SDMReadAttachmentsHandler;
import com.sap.cds.services.cds.CdsReadEventContext;
import java.util.Optional;
import java.util.stream.Stream;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.ArgumentCaptor;
import org.mockito.InjectMocks;
import org.mockito.Mock;
import org.mockito.junit.jupiter.MockitoExtension;
Expand All @@ -41,6 +38,8 @@ void testModifyCqnForAttachmentsEntity_Success() {
CqnSelect select =
Select.from(cdsEntity).where(doc -> doc.get("repositoryId").eq(REPOSITORY_ID_KEY));
when(context.getTarget()).thenReturn(cdsEntity);
when(cdsEntity.getAnnotationValue(any(), any())).thenReturn(true);
when(context.getCqn()).thenReturn(select);
// Act
sdmReadAttachmentsHandler.processBefore(context); // Refers to the method you provided

Expand All @@ -61,57 +60,14 @@ void testModifyCqnForNonAttachmentsEntity() {

// Mock target
when(context.getTarget()).thenReturn(cdsEntity);
when(context.getCqn()).thenReturn(select);
when(cdsEntity.getQualifiedName()).thenReturn(targetEntity);
when(cdsEntity.getAnnotationValue(any(), any())).thenReturn(false);

when(context.getCqn()).thenReturn(select);
// Mock composition with Attachments aspect
when(mockComposition.getType()).thenReturn(mockAssociationType);
when(mockComposition.getName()).thenReturn("attachments");
when(mockTargetAspect.getQualifiedName()).thenReturn("sap.attachments.Attachments");
when(mockAssociationType.getTargetAspect()).thenReturn(Optional.of(mockTargetAspect));

// Return a stream with one valid attachment composition
when(cdsEntity.compositions()).thenReturn(Stream.of(mockComposition));

// Act
sdmReadAttachmentsHandler.processBefore(context);

// Assert — since it enters the 'else' clause, it should call setCqn with original select
verify(context).setCqn(select);
}

@Test
void testModifiesCqnWhenEntityMatchesComposition() {
// Arrange
String targetEntity = "attachments"; // Matches the composition name
String expectedRepositoryId = REPOSITORY_ID_KEY;

// Original query with no WHERE clause
CqnSelect originalCqn = Select.from("SomeEntity");

// Mock context and target entity
when(context.getTarget()).thenReturn(cdsEntity);
when(context.getCqn()).thenReturn(originalCqn);
when(cdsEntity.getQualifiedName()).thenReturn(targetEntity);

// Mock composition with matching name
when(mockComposition.getType()).thenReturn(mockAssociationType);
when(mockComposition.getName()).thenReturn("attachments");
when(mockTargetAspect.getQualifiedName()).thenReturn("sap.attachments.Attachments");
when(mockAssociationType.getTargetAspect()).thenReturn(Optional.of(mockTargetAspect));
when(cdsEntity.compositions()).thenReturn(Stream.of(mockComposition));

// Act
sdmReadAttachmentsHandler.processBefore(context);

// Assert — capture the modified CQN and verify it contains the repositoryId condition
ArgumentCaptor<CqnSelect> captor = ArgumentCaptor.forClass(CqnSelect.class);
verify(context).setCqn(captor.capture());
CqnSelect modifiedCqn = captor.getValue();

// Basic assertion: check that repositoryId predicate was added
String cqnAsString = modifiedCqn.toJson(); // or use toString(), depending on your CQN lib
assertTrue(cqnAsString.contains("\"ref\":[\"repositoryId\"]"));
assertTrue(cqnAsString.contains("\"val\":\"" + expectedRepositoryId + "\""));
}
}
Loading