From bf0227f04e74e0c878f0a0e39934db3332b85cb5 Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+rishikunnath2747@users.noreply.github.com> Date: Thu, 27 Nov 2025 14:43:04 +0530 Subject: [PATCH 1/9] potential fix --- .../com/sap/cds/sdm/service/SDMAttachmentsService.java | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/SDMAttachmentsService.java b/sdm/src/main/java/com/sap/cds/sdm/service/SDMAttachmentsService.java index c99fc4dc3..2e7db521c 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/SDMAttachmentsService.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/SDMAttachmentsService.java @@ -39,13 +39,14 @@ public void copyAttachments(CopyAttachmentInput input, boolean isSystemUser) { // Parse facet to extract parent entity and composition name String[] facetParts = input.facet().split("\\."); - if (facetParts.length < 3) { + if (facetParts.length < 2) { throw new IllegalArgumentException( String.format(SDMConstants.INVALID_FACET_FORMAT_ERROR, input.facet())); } - String parentEntity = facetParts[0] + "." + facetParts[1]; // Service.Entity - String compositionName = facetParts[2]; // composition name + // The last part is the composition name, everything else is the parent entity + String compositionName = facetParts[facetParts.length - 1]; + String parentEntity = input.facet().substring(0, input.facet().lastIndexOf(".")); var copyContext = AttachmentCopyEventContext.create(); copyContext.setUpId(input.upId()); From 19648c361dd23fec859e6432446f6ced956a6bb4 Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+rishikunnath2747@users.noreply.github.com> Date: Fri, 28 Nov 2025 08:36:25 +0530 Subject: [PATCH 2/9] adding logging statements --- .../sdm/service/SDMAttachmentsService.java | 1 + .../sap/cds/sdm/service/SDMServiceImpl.java | 1 + .../handler/SDMCustomServiceHandler.java | 28 +++++++++++++++++++ 3 files changed, 30 insertions(+) diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/SDMAttachmentsService.java b/sdm/src/main/java/com/sap/cds/sdm/service/SDMAttachmentsService.java index 2e7db521c..f8ecefa35 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/SDMAttachmentsService.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/SDMAttachmentsService.java @@ -47,6 +47,7 @@ public void copyAttachments(CopyAttachmentInput input, boolean isSystemUser) { // The last part is the composition name, everything else is the parent entity String compositionName = facetParts[facetParts.length - 1]; String parentEntity = input.facet().substring(0, input.facet().lastIndexOf(".")); + logger.info("Composition Name: {}, Parent Entity: {}", compositionName, parentEntity); var copyContext = AttachmentCopyEventContext.create(); copyContext.setUpId(input.upId()); diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java b/sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java index f1c765022..9654dcc11 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java @@ -697,6 +697,7 @@ public List copyAttachment( JSONObject errorJson = new JSONObject(responseBody); String exceptionType = errorJson.optString("exception"); String errorMessage = errorJson.optString("message"); + logger.error("Error copying attachment: {} : {}", exceptionType, errorMessage); throw new ServiceException(exceptionType + " : " + errorMessage); } catch (IOException e) { throw new ServiceException(SDMConstants.FAILED_TO_COPY_ATTACHMENT, e); diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java index fd8df12aa..843debce1 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java @@ -13,6 +13,7 @@ import com.sap.cds.sdm.model.SDMCredentials; import com.sap.cds.sdm.persistence.DBQuery; import com.sap.cds.sdm.service.RegisterService; +import com.sap.cds.sdm.service.SDMAttachmentsService; import com.sap.cds.sdm.service.SDMService; import com.sap.cds.services.ServiceException; import com.sap.cds.services.draft.DraftService; @@ -26,6 +27,8 @@ import java.util.Map; import java.util.Optional; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; @ServiceName(value = "*", type = RegisterService.class) public class SDMCustomServiceHandler { @@ -34,6 +37,7 @@ public class SDMCustomServiceHandler { private final List draftService; private final TokenHandler tokenHandler; private final PersistenceService persistenceService; + private static final Logger logger = LoggerFactory.getLogger(SDMAttachmentsService.class); // Result class for copyAttachmentsToSDM method private static class CopyAttachmentsResult { @@ -76,15 +80,24 @@ public void copyAttachments(AttachmentCopyEventContext context) throws IOExcepti String folderName = upID + "__" + compositionName; String repositoryId = SDMConstants.REPOSITORY_ID; Boolean isSystemUser = context.getSystemUser(); + logger.info( + "Copying attachments in SDMCustomServiceHandler for upId: {}, facet: {}, objectIds: {}, isSystemUser: {}", + upID, + parentEntity + "." + compositionName, + context.getObjectIds(), + isSystemUser); SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials(); // Check if folder exists before trying to create it boolean folderExists = sdmService.getFolderIdByPath(folderName, repositoryId, sdmCredentials, isSystemUser) != null; + logger.info("Folder '{}' exists: {}", folderName, folderExists); String folderId = ensureFolderExists(folderName, repositoryId, sdmCredentials, isSystemUser); + logger.info("Using folder ID: {}", folderId); List objectIds = context.getObjectIds(); + logger.info("Object IDs to copy: {}", objectIds); CopyAttachmentsRequest request = CopyAttachmentsRequest.builder() @@ -96,13 +109,20 @@ public void copyAttachments(AttachmentCopyEventContext context) throws IOExcepti .isSystemUser(isSystemUser) .folderExists(folderExists) .build(); + logger.info( + "CopyAttachmentsRequest prepared with folderId: {}, repositoryId: {}, isSystemUser: {}", + folderId, + repositoryId, + isSystemUser); CopyAttachmentsResult copyResult = copyAttachmentsToSDM(request); + logger.info("Attachments copied successfully to SDM : " + copyResult.getAttachmentsMetadata()); List> attachmentsMetadata = copyResult.getAttachmentsMetadata(); List populatedDocuments = copyResult.getPopulatedDocuments(); String upIdKey = resolveUpIdKey(context, parentEntity, compositionName); + logger.info("Resolved upIdKey: {}", upIdKey); CreateDraftEntriesRequest draftRequest = CreateDraftEntriesRequest.builder() @@ -159,7 +179,10 @@ private CopyAttachmentsResult copyAttachmentsToSDM(CopyAttachmentsRequest reques attachmentsMetadata.add( sdmService.copyAttachment( cmisDocument, request.getSdmCredentials(), request.getIsSystemUser())); + logger.info("Attachments metadata after copying: {}", attachmentsMetadata); } catch (ServiceException e) { + logger.info("Copy failure : " + e); + logger.info("Attachments metadata after copy failure : " + attachmentsMetadata); handleCopyFailure( request.getContext(), request.getFolderId(), @@ -232,6 +255,11 @@ private void createDraftEntries(CreateDraftEntriesRequest request) { String fileName = attachmentMetadata.get(0); String mimeType = attachmentMetadata.get(1); String newObjectId = attachmentMetadata.get(2); + logger.info( + "Creating draft entry for attachment - fileName: {}, mimeType: {}, newObjectId: {}", + fileName, + mimeType, + newObjectId); updatedFields.put("objectId", newObjectId); updatedFields.put("repositoryId", request.getRepositoryId()); From f276aadc4bd9e817f3e87736f4ea6031394af893 Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+rishikunnath2747@users.noreply.github.com> Date: Fri, 28 Nov 2025 08:38:34 +0530 Subject: [PATCH 3/9] pushing save logs --- .../applicationservice/SDMCreateAttachmentsHandler.java | 1 + .../applicationservice/SDMUpdateAttachmentsHandler.java | 1 + .../helper/AttachmentsHandlerUtils.java | 9 +++++++++ 3 files changed, 11 insertions(+) diff --git a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java index b1f79db03..44767d38f 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java +++ b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java @@ -53,6 +53,7 @@ public SDMCreateAttachmentsHandler( @Before @HandlerOrder(HandlerOrder.EARLY) public void processBefore(CdsCreateEventContext context, List data) throws IOException { + logger.info("Processing CREATE ATTACHMENTS HANDLER"); logger.info("Target Entity : " + context.getTarget().getQualifiedName()); for (CdsData entityData : data) { Map> attachmentCompositionDetails = diff --git a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandler.java b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandler.java index 88ddcadb9..2e7bee284 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandler.java +++ b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandler.java @@ -49,6 +49,7 @@ public SDMUpdateAttachmentsHandler( @HandlerOrder(HandlerOrder.EARLY) public void processBefore(CdsUpdateEventContext context, List data) throws IOException { // Get comprehensive attachment composition details for each entity + logger.info("Processing UPDATE ATTACHMENTS HANDLER"); for (CdsData entityData : data) { Map> attachmentCompositionDetails = AttachmentsHandlerUtils.getAttachmentCompositionDetails( diff --git a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/helper/AttachmentsHandlerUtils.java b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/helper/AttachmentsHandlerUtils.java index a55a3e5f0..9a92327be 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/helper/AttachmentsHandlerUtils.java +++ b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/helper/AttachmentsHandlerUtils.java @@ -557,6 +557,10 @@ public static Boolean validateFileNames( EventContext context, List data, String composition, String contextInfo) { Boolean isError = false; String targetEntity = context.getTarget().getQualifiedName(); + logger.info( + "Validating file names for target entity: {}, composition: {}", + targetEntity, + composition); // Validation for file names Set whitespaceFilenames = @@ -565,6 +569,11 @@ public static Boolean validateFileNames( SDMUtils.FileNameContainsRestrictedCharaters(data, composition, targetEntity); Set duplicateFilenames = SDMUtils.FileNameDuplicateInDrafts(data, composition, targetEntity); + logger.info( + "File name validation results - Whitespace: {}, Restricted: {}, Duplicates: {}", + whitespaceFilenames, + restrictedFileNames, + duplicateFilenames); // Collecting all the errors if (whitespaceFilenames != null && !whitespaceFilenames.isEmpty()) { From 01b78fd572ab92d0a7f1506a9540c74903e04f9f Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+rishikunnath2747@users.noreply.github.com> Date: Fri, 28 Nov 2025 20:37:37 +0530 Subject: [PATCH 4/9] init changes for duplicate issue (uts failing) --- .../SDMCreateAttachmentsHandler.java | 7 ++--- .../SDMUpdateAttachmentsHandler.java | 9 +++--- .../helper/AttachmentsHandlerUtils.java | 21 ++++++------- .../sap/cds/sdm/service/SDMServiceImpl.java | 1 - .../handler/SDMAttachmentsServiceHandler.java | 16 +--------- .../handler/SDMCustomServiceHandler.java | 28 ----------------- .../handler/SDMServiceGenericHandler.java | 20 +++--------- .../com/sap/cds/sdm/utilities/SDMUtils.java | 31 +++++++++++++++++-- .../SDMCreateAttachmentsHandlerTest.java | 26 ++++++++++------ .../SDMUpdateAttachmentsHandlerTest.java | 9 ++++-- .../SDMAttachmentsServiceHandlerTest.java | 8 +++-- .../sap/cds/sdm/utilities/SDMUtilsTest.java | 2 +- 12 files changed, 81 insertions(+), 97 deletions(-) diff --git a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java index 44767d38f..83d0ab3ad 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java +++ b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandler.java @@ -53,7 +53,6 @@ public SDMCreateAttachmentsHandler( @Before @HandlerOrder(HandlerOrder.EARLY) public void processBefore(CdsCreateEventContext context, List data) throws IOException { - logger.info("Processing CREATE ATTACHMENTS HANDLER"); logger.info("Target Entity : " + context.getTarget().getQualifiedName()); for (CdsData entityData : data) { Map> attachmentCompositionDetails = @@ -95,9 +94,11 @@ public void updateName( + "\nPage: " + (parentTitle != null ? parentTitle : "Unknown"); + Optional attachmentEntity = + context.getModel().findEntity(attachmentCompositionDefinition); isError = AttachmentsHandlerUtils.validateFileNames( - context, data, attachmentCompositionName, contextInfo); + context, data, attachmentCompositionName, contextInfo, attachmentEntity); if (!isError) { List fileNameWithRestrictedCharacters = new ArrayList<>(); List duplicateFileNameList = new ArrayList<>(); @@ -116,8 +117,6 @@ public void updateName( targetEntity); continue; } - Optional attachmentEntity = - context.getModel().findEntity(attachmentCompositionDefinition); propertyTitles = SDMUtils.getPropertyTitles(attachmentEntity, attachments.get(0)); secondaryPropertiesWithInvalidDefinitions = SDMUtils.getSecondaryPropertiesWithInvalidDefinition( diff --git a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandler.java b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandler.java index 2e7bee284..53e91dd93 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandler.java +++ b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandler.java @@ -49,7 +49,6 @@ public SDMUpdateAttachmentsHandler( @HandlerOrder(HandlerOrder.EARLY) public void processBefore(CdsUpdateEventContext context, List data) throws IOException { // Get comprehensive attachment composition details for each entity - logger.info("Processing UPDATE ATTACHMENTS HANDLER"); for (CdsData entityData : data) { Map> attachmentCompositionDetails = AttachmentsHandlerUtils.getAttachmentCompositionDetails( @@ -87,12 +86,14 @@ public void updateName( + "\nPage: " + (parentTitle != null ? parentTitle : "Unknown"); + Optional attachmentEntity = Optional.empty(); + if (context.getModel() != null) { + attachmentEntity = context.getModel().findEntity(attachmentCompositionDefinition); + } isError = AttachmentsHandlerUtils.validateFileNames( - context, data, attachmentCompositionName, contextInfo); + context, data, attachmentCompositionName, contextInfo, attachmentEntity); if (!isError) { - Optional attachmentEntity = - context.getModel().findEntity(attachmentCompositionDefinition); renameDocument( attachmentEntity, context, diff --git a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/helper/AttachmentsHandlerUtils.java b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/helper/AttachmentsHandlerUtils.java index 9a92327be..9b0046c3f 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/helper/AttachmentsHandlerUtils.java +++ b/sdm/src/main/java/com/sap/cds/sdm/handler/applicationservice/helper/AttachmentsHandlerUtils.java @@ -554,13 +554,17 @@ private static String extractTitleFromEntity(Object entityObj) { * @return true if any validation errors are found, false otherwise */ public static Boolean validateFileNames( - EventContext context, List data, String composition, String contextInfo) { + EventContext context, + List data, + String composition, + String contextInfo, + Optional attachmentEntity) { Boolean isError = false; String targetEntity = context.getTarget().getQualifiedName(); - logger.info( - "Validating file names for target entity: {}, composition: {}", - targetEntity, - composition); + String upIdKey = ""; + if (attachmentEntity.isPresent()) { + upIdKey = SDMUtils.getUpIdKey(attachmentEntity.get()); + } // Validation for file names Set whitespaceFilenames = @@ -568,12 +572,7 @@ public static Boolean validateFileNames( List restrictedFileNames = SDMUtils.FileNameContainsRestrictedCharaters(data, composition, targetEntity); Set duplicateFilenames = - SDMUtils.FileNameDuplicateInDrafts(data, composition, targetEntity); - logger.info( - "File name validation results - Whitespace: {}, Restricted: {}, Duplicates: {}", - whitespaceFilenames, - restrictedFileNames, - duplicateFilenames); + SDMUtils.FileNameDuplicateInDrafts(data, composition, targetEntity, upIdKey); // Collecting all the errors if (whitespaceFilenames != null && !whitespaceFilenames.isEmpty()) { diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java b/sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java index 9654dcc11..f1c765022 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java @@ -697,7 +697,6 @@ public List copyAttachment( JSONObject errorJson = new JSONObject(responseBody); String exceptionType = errorJson.optString("exception"); String errorMessage = errorJson.optString("message"); - logger.error("Error copying attachment: {} : {}", exceptionType, errorMessage); throw new ServiceException(exceptionType + " : " + errorMessage); } catch (IOException e) { throw new ServiceException(SDMConstants.FAILED_TO_COPY_ATTACHMENT, e); diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandler.java b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandler.java index 86795bdb0..1d85cb24c 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandler.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandler.java @@ -182,7 +182,7 @@ private void processEntities(AttachmentCreateEventContext eventContext) Map attachmentIds = eventContext.getAttachmentIds(); CdsEntity attachmentDraftEntity = getAttachmentDraftEntity(eventContext); - String upIdKey = getUpIdKey(attachmentDraftEntity); + String upIdKey = SDMUtils.getUpIdKey(attachmentDraftEntity); String upID = (String) attachmentIds.get(upIdKey); Result result = @@ -202,20 +202,6 @@ private CdsEntity getAttachmentDraftEntity(AttachmentCreateEventContext eventCon () -> new ServiceException(SDMConstants.DRAFT_NOT_FOUND)); } - private String getUpIdKey(CdsEntity attachmentDraftEntity) { - String upIdKey = ""; - Optional upAssociation = attachmentDraftEntity.findAssociation("up_"); - if (upAssociation.isPresent()) { - CdsElement association = upAssociation.get(); - // get association type - CdsAssociationType associationType = association.getType(); - // get the refs of the association - List fkElements = associationType.refs().map(ref -> "up__" + ref.path()).toList(); - upIdKey = fkElements.get(0); - } - return upIdKey; - } - private void checkAttachmentConstraints( AttachmentCreateEventContext eventContext, CdsEntity attachmentDraftEntity, diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java index 843debce1..fd8df12aa 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMCustomServiceHandler.java @@ -13,7 +13,6 @@ import com.sap.cds.sdm.model.SDMCredentials; import com.sap.cds.sdm.persistence.DBQuery; import com.sap.cds.sdm.service.RegisterService; -import com.sap.cds.sdm.service.SDMAttachmentsService; import com.sap.cds.sdm.service.SDMService; import com.sap.cds.services.ServiceException; import com.sap.cds.services.draft.DraftService; @@ -27,8 +26,6 @@ import java.util.Map; import java.util.Optional; import org.json.JSONObject; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; @ServiceName(value = "*", type = RegisterService.class) public class SDMCustomServiceHandler { @@ -37,7 +34,6 @@ public class SDMCustomServiceHandler { private final List draftService; private final TokenHandler tokenHandler; private final PersistenceService persistenceService; - private static final Logger logger = LoggerFactory.getLogger(SDMAttachmentsService.class); // Result class for copyAttachmentsToSDM method private static class CopyAttachmentsResult { @@ -80,24 +76,15 @@ public void copyAttachments(AttachmentCopyEventContext context) throws IOExcepti String folderName = upID + "__" + compositionName; String repositoryId = SDMConstants.REPOSITORY_ID; Boolean isSystemUser = context.getSystemUser(); - logger.info( - "Copying attachments in SDMCustomServiceHandler for upId: {}, facet: {}, objectIds: {}, isSystemUser: {}", - upID, - parentEntity + "." + compositionName, - context.getObjectIds(), - isSystemUser); SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials(); // Check if folder exists before trying to create it boolean folderExists = sdmService.getFolderIdByPath(folderName, repositoryId, sdmCredentials, isSystemUser) != null; - logger.info("Folder '{}' exists: {}", folderName, folderExists); String folderId = ensureFolderExists(folderName, repositoryId, sdmCredentials, isSystemUser); - logger.info("Using folder ID: {}", folderId); List objectIds = context.getObjectIds(); - logger.info("Object IDs to copy: {}", objectIds); CopyAttachmentsRequest request = CopyAttachmentsRequest.builder() @@ -109,20 +96,13 @@ public void copyAttachments(AttachmentCopyEventContext context) throws IOExcepti .isSystemUser(isSystemUser) .folderExists(folderExists) .build(); - logger.info( - "CopyAttachmentsRequest prepared with folderId: {}, repositoryId: {}, isSystemUser: {}", - folderId, - repositoryId, - isSystemUser); CopyAttachmentsResult copyResult = copyAttachmentsToSDM(request); - logger.info("Attachments copied successfully to SDM : " + copyResult.getAttachmentsMetadata()); List> attachmentsMetadata = copyResult.getAttachmentsMetadata(); List populatedDocuments = copyResult.getPopulatedDocuments(); String upIdKey = resolveUpIdKey(context, parentEntity, compositionName); - logger.info("Resolved upIdKey: {}", upIdKey); CreateDraftEntriesRequest draftRequest = CreateDraftEntriesRequest.builder() @@ -179,10 +159,7 @@ private CopyAttachmentsResult copyAttachmentsToSDM(CopyAttachmentsRequest reques attachmentsMetadata.add( sdmService.copyAttachment( cmisDocument, request.getSdmCredentials(), request.getIsSystemUser())); - logger.info("Attachments metadata after copying: {}", attachmentsMetadata); } catch (ServiceException e) { - logger.info("Copy failure : " + e); - logger.info("Attachments metadata after copy failure : " + attachmentsMetadata); handleCopyFailure( request.getContext(), request.getFolderId(), @@ -255,11 +232,6 @@ private void createDraftEntries(CreateDraftEntriesRequest request) { String fileName = attachmentMetadata.get(0); String mimeType = attachmentMetadata.get(1); String newObjectId = attachmentMetadata.get(2); - logger.info( - "Creating draft entry for attachment - fileName: {}, mimeType: {}, newObjectId: {}", - fileName, - mimeType, - newObjectId); updatedFields.put("objectId", newObjectId); updatedFields.put("repositoryId", request.getRepositoryId()); diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java index 1bec4714c..fe53221c7 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java @@ -196,7 +196,7 @@ private void revertLinksForComposition( CdsEntity draftEntity = model.findEntity(draftEntityName).get(); CdsEntity activeEntity = model.findEntity(attachmentCompositionDefinition).get(); - String upIdKey = getUpIdKey(draftEntity); + final String upIdKey = SDMUtils.getUpIdKey(draftEntity); String parentKeyName = upIdKey.replaceFirst("^up__", ""); Object parentId = parentKeys.get(parentKeyName); @@ -327,7 +327,9 @@ private void createLink(EventContext context) throws IOException { cdsModel.findEntity(context.getTarget().getQualifiedName() + "_drafts"); String upIdKey = - attachmentDraftEntity.isPresent() ? getUpIdKey(attachmentDraftEntity.get()) : "up__ID"; + attachmentDraftEntity.isPresent() + ? SDMUtils.getUpIdKey(attachmentDraftEntity.get()) + : "up__ID"; CqnSelect select = (CqnSelect) context.get("cqn"); String upID = fetchUPIDFromCQN(select); String filenameInRequest = context.get("name").toString(); @@ -415,20 +417,6 @@ private void editLink(EventContext context) throws IOException { context.setCompleted(); } - private String getUpIdKey(CdsEntity attachmentDraftEntity) { - String upIdKey = ""; - Optional upAssociation = attachmentDraftEntity.findAssociation("up_"); - if (upAssociation.isPresent()) { - CdsElement association = upAssociation.get(); - // get association type - CdsAssociationType associationType = association.getType(); - // get the refs of the association - List fkElements = associationType.refs().map(ref -> "up__" + ref.path()).toList(); - upIdKey = fkElements.get(0); - } - return upIdKey; - } - private void checkAttachmentConstraints( EventContext context, CdsEntity attachmentDraftEntity, String upID, String upIdKey) throws ServiceException { diff --git a/sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java b/sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java index 339cc3ffc..c005ef83d 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java +++ b/sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java @@ -2,6 +2,7 @@ import com.sap.cds.CdsData; import com.sap.cds.reflect.CdsAnnotation; +import com.sap.cds.reflect.CdsAssociationType; import com.sap.cds.reflect.CdsElement; import com.sap.cds.reflect.CdsEntity; import com.sap.cds.sdm.caching.CacheConfig; @@ -54,7 +55,7 @@ public static Set FileNameContainsWhitespace( } public static Set FileNameDuplicateInDrafts( - List data, String composition, String targetEntity) { + List data, String composition, String targetEntity, String upIdKey) { Set uniqueFilenames = new HashSet<>(); Set duplicateFilenames = new HashSet<>(); for (Map entity : data) { @@ -67,7 +68,9 @@ public static Set FileNameDuplicateInDrafts( String filenameInRequest = (String) attachment.get("fileName"); if (filenameInRequest != null && !filenameInRequest.isBlank()) { String repositoryInRequest = (String) attachment.get("repositoryId"); - String fileRepositorySpecific = filenameInRequest + "#" + repositoryInRequest; + String upId = (String) attachment.get(upIdKey); + String fileRepositorySpecific = + filenameInRequest + "#" + repositoryInRequest + "#" + upId; if (!uniqueFilenames.add(fileRepositorySpecific)) { duplicateFilenames.add(filenameInRequest); } @@ -368,6 +371,30 @@ public static boolean isRelatedEntity(CdsEntity attachmentEntity, CdsEntity cdsE && !attachmentQualifiedName.equals(cdsEntity.getQualifiedName()); } + public static String getUpIdKey(CdsEntity attachmentDraftEntity) { + String upIdKey = ""; + Optional upAssociation = attachmentDraftEntity.findAssociation("up_"); + if (upAssociation.isPresent()) { + CdsElement association = upAssociation.get(); + // get association type + CdsAssociationType associationType = association.getType(); + // get the refs of the association + List fkElements = associationType.refs().map(ref -> "up__" + ref.path()).toList(); + if (!fkElements.isEmpty()) { + upIdKey = fkElements.get(0); + } + } + // Fallback: if no association found, try to find element starting with "up__" + if (upIdKey.isEmpty()) { + Optional upElement = + attachmentDraftEntity.elements().filter(e -> e.getName().startsWith("up__")).findFirst(); + if (upElement.isPresent()) { + upIdKey = upElement.get().getName(); + } + } + return upIdKey; + } + private static void processCompositions( CdsEntity cdsEntity, AttachmentInfo attachmentInfo, CdsEntity attachmentEntity) { List compositions = cdsEntity.compositions().toList(); diff --git a/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandlerTest.java b/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandlerTest.java index 6ec0cc9e3..d774879b6 100644 --- a/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandlerTest.java +++ b/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandlerTest.java @@ -145,7 +145,9 @@ public void testUpdateNameWithDuplicateFilenames() throws IOException { SDMUtils.FileNameContainsRestrictedCharaters(anyList(), anyString(), anyString())) .thenReturn(Collections.emptyList()); sdmUtilsMockedStatic - .when(() -> SDMUtils.FileNameDuplicateInDrafts(data, "compositionName", "TestEntity")) + .when( + () -> + SDMUtils.FileNameDuplicateInDrafts(data, "compositionName", "TestEntity", "upId")) .thenReturn(duplicateFilenames); try (MockedStatic attachmentUtilsMockedStatic = mockStatic(AttachmentsHandlerUtils.class)) { @@ -153,7 +155,7 @@ public void testUpdateNameWithDuplicateFilenames() throws IOException { .when( () -> AttachmentsHandlerUtils.validateFileNames( - any(), anyList(), anyString(), anyString())) + any(), anyList(), anyString(), anyString(), any())) .thenCallRealMethod(); // Act @@ -179,7 +181,7 @@ public void testUpdateNameWithEmptyData() throws IOException { // Arrange List data = new ArrayList<>(); sdmUtilsMockedStatic - .when(() -> SDMUtils.FileNameDuplicateInDrafts(data, "compositionName", "entity")) + .when(() -> SDMUtils.FileNameDuplicateInDrafts(data, "compositionName", "entity", "upId")) .thenReturn(Collections.emptySet()); // Act @@ -216,7 +218,7 @@ public void testUpdateNameWithNoAttachments() throws IOException { // Mock utility methods sdmUtilsMockedStatic - .when(() -> SDMUtils.FileNameDuplicateInDrafts(data, "compositionName", "entity")) + .when(() -> SDMUtils.FileNameDuplicateInDrafts(data, "compositionName", "entity", "upId")) .thenReturn(Collections.emptySet()); // Act @@ -476,7 +478,7 @@ public void testUpdateNameWithEmptyFilename() throws IOException { .when( () -> SDMUtils.FileNameDuplicateInDrafts( - data, "compositionName", "some.qualified.Name")) + data, "compositionName", "some.qualified.Name", "upId")) .thenReturn(new HashSet<>()); // Mock AttachmentsHandlerUtils.fetchAttachments to return the attachment with null filename @@ -492,7 +494,7 @@ public void testUpdateNameWithEmptyFilename() throws IOException { .when( () -> AttachmentsHandlerUtils.validateFileNames( - any(), anyList(), anyString(), anyString())) + any(), anyList(), anyString(), anyString(), any())) .thenCallRealMethod(); // Mock attachment entity @@ -555,7 +557,10 @@ public void testUpdateNameWithEmptyFilename() throws IOException { .when(() -> SDMUtils.FileNameContainsWhitespace(anyList(), anyString(), anyString())) .thenCallRealMethod(); sdmUtilsMockedStatic - .when(() -> SDMUtils.FileNameDuplicateInDrafts(anyList(), anyString(), anyString())) + .when( + () -> + SDMUtils.FileNameDuplicateInDrafts( + anyList(), anyString(), anyString(), anyString())) .thenReturn(new HashSet<>()); // Act @@ -614,7 +619,10 @@ public void testUpdateNameWithRestrictedCharacters() throws IOException { .when(() -> SDMUtils.FileNameContainsWhitespace(anyList(), anyString(), anyString())) .thenReturn(Collections.emptySet()); sdmUtilsMockedStatic - .when(() -> SDMUtils.FileNameDuplicateInDrafts(anyList(), anyString(), anyString())) + .when( + () -> + SDMUtils.FileNameDuplicateInDrafts( + anyList(), anyString(), anyString(), anyString())) .thenReturn(Collections.emptySet()); sdmUtilsMockedStatic .when( @@ -635,7 +643,7 @@ public void testUpdateNameWithRestrictedCharacters() throws IOException { .when( () -> AttachmentsHandlerUtils.validateFileNames( - any(), anyList(), anyString(), anyString())) + any(), anyList(), anyString(), anyString(), any())) .thenCallRealMethod(); // Act diff --git a/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandlerTest.java b/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandlerTest.java index 60fe5260c..a59260038 100644 --- a/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandlerTest.java +++ b/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMUpdateAttachmentsHandlerTest.java @@ -166,7 +166,7 @@ public void testRenameWithDuplicateFilenames() throws IOException { .when( () -> AttachmentsHandlerUtils.validateFileNames( - any(), anyList(), anyString(), anyString())) + any(), anyList(), anyString(), anyString(), any())) .thenCallRealMethod(); // Mock SDMUtils helper methods to ensure validation works correctly @@ -183,7 +183,10 @@ public void testRenameWithDuplicateFilenames() throws IOException { Set duplicateFiles = new HashSet<>(); duplicateFiles.add("file1.txt"); sdmUtilsMockedStatic - .when(() -> SDMUtils.FileNameDuplicateInDrafts(anyList(), anyString(), anyString())) + .when( + () -> + SDMUtils.FileNameDuplicateInDrafts( + anyList(), anyString(), anyString(), anyString())) .thenReturn(duplicateFiles); // Call the method under test; validateFileNames will detect duplicates and call @@ -360,7 +363,7 @@ public void testRenameWithNoSDMRoles() throws IOException { .when( () -> SDMUtils.FileNameDuplicateInDrafts( - any(List.class), eq("compositionName"), anyString())) + any(List.class), eq("compositionName"), anyString(), anyString())) .thenReturn(Collections.emptySet()); sdmUtilsMock diff --git a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java index fcad2ff9b..bf8ac63f2 100644 --- a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java +++ b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java @@ -766,7 +766,7 @@ public void testCreateNonVersionedDIUnauthorizedI18n() throws IOException { .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("10__null"); attachmentUtilsMockedStatic - .when(() -> AttachmentsHandlerUtils.validateFileNames(any(), any(), any(), any())) + .when(() -> AttachmentsHandlerUtils.validateFileNames(any(), any(), any(), any(), any())) .thenCallRealMethod(); // Assert that a ServiceException is thrown and verify its message @@ -855,7 +855,8 @@ public void testCreateNonVersionedDIUnauthorized() throws IOException { try (MockedStatic attachmentUtilsMockedStatic = mockStatic(AttachmentsHandlerUtils.class)) { attachmentUtilsMockedStatic - .when(() -> AttachmentsHandlerUtils.validateFileNames(any(), any(), any(), any())) + .when( + () -> AttachmentsHandlerUtils.validateFileNames(any(), any(), any(), any(), any())) .thenCallRealMethod(); // Assert that a ServiceException is thrown and verify its message @@ -946,7 +947,8 @@ public void testCreateNonVersionedDIBlocked() throws IOException { try (MockedStatic attachmentUtilsMockedStatic = mockStatic(AttachmentsHandlerUtils.class)) { attachmentUtilsMockedStatic - .when(() -> AttachmentsHandlerUtils.validateFileNames(any(), any(), any(), any())) + .when( + () -> AttachmentsHandlerUtils.validateFileNames(any(), any(), any(), any(), any())) .thenCallRealMethod(); // Assert that a ServiceException is thrown and verify its message diff --git a/sdm/src/test/java/unit/com/sap/cds/sdm/utilities/SDMUtilsTest.java b/sdm/src/test/java/unit/com/sap/cds/sdm/utilities/SDMUtilsTest.java index 6d8393973..2cd6caf4c 100644 --- a/sdm/src/test/java/unit/com/sap/cds/sdm/utilities/SDMUtilsTest.java +++ b/sdm/src/test/java/unit/com/sap/cds/sdm/utilities/SDMUtilsTest.java @@ -90,7 +90,7 @@ public void testIsFileNameDuplicateInDrafts() { data.add(CdsData.create(entity)); Set duplicateFilenames = - SDMUtils.FileNameDuplicateInDrafts(data, "attachmentCompositionName", "entity"); + SDMUtils.FileNameDuplicateInDrafts(data, "attachmentCompositionName", "entity", "upId"); assertTrue(duplicateFilenames.contains("file1.txt")); } From c152a5dc93bc8ad346d5767c437c1f383eabacbd Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+rishikunnath2747@users.noreply.github.com> Date: Mon, 1 Dec 2025 09:47:50 +0530 Subject: [PATCH 5/9] adding logs + tests --- .../com/sap/cds/sdm/utilities/SDMUtils.java | 8 +++ .../SDMAttachmentsServiceHandlerTest.java | 33 ++++++---- .../handler/SDMServiceGenericHandlerTest.java | 63 +++++++++++++++++++ 3 files changed, 91 insertions(+), 13 deletions(-) diff --git a/sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java b/sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java index c005ef83d..b8505e71f 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java +++ b/sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java @@ -27,8 +27,11 @@ import org.apache.http.util.EntityUtils; import org.json.JSONArray; import org.json.JSONObject; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; public class SDMUtils { + private static final Logger logger = LoggerFactory.getLogger(CacheConfig.class); private SDMUtils() { // Doesn't do anything @@ -56,6 +59,10 @@ public static Set FileNameContainsWhitespace( public static Set FileNameDuplicateInDrafts( List data, String composition, String targetEntity, String upIdKey) { + logger.info("Checking for duplicate filenames in draft attachments..."); + logger.info("Using upIdKey: " + upIdKey); + logger.info("Target Entity: " + targetEntity); + logger.info("Composition: " + composition); Set uniqueFilenames = new HashSet<>(); Set duplicateFilenames = new HashSet<>(); for (Map entity : data) { @@ -71,6 +78,7 @@ public static Set FileNameDuplicateInDrafts( String upId = (String) attachment.get(upIdKey); String fileRepositorySpecific = filenameInRequest + "#" + repositoryInRequest + "#" + upId; + logger.info("Filename key check : " + fileRepositorySpecific); if (!uniqueFilenames.add(fileRepositorySpecific)) { duplicateFilenames.add(filenameInRequest); } diff --git a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java index bf8ac63f2..5d00d5f11 100644 --- a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java +++ b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java @@ -342,7 +342,7 @@ public void testCreateNonVersionedDuplicate() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(eventContext.getUserInfo()).thenReturn(userInfo); + when(mockContext.getUserInfo()).thenReturn(userInfo); when(userInfo.getTenant()).thenReturn("t123"); RepoValue repoValue = new RepoValue(); repoValue.setVirusScanEnabled(false); @@ -363,6 +363,7 @@ public void testCreateNonVersionedDuplicate() throws IOException { .thenReturn(mockResult); when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) .thenReturn(mockResult); + when(mockResult.rowCount()).thenReturn(0L); // Use assertThrows to expect a ServiceException and validate the message ServiceException thrown = assertThrows( @@ -414,7 +415,7 @@ public void testCreateNonVersionedDIDuplicate() throws IOException { when(mockAssocType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(eventContext.getUserInfo()).thenReturn(userInfo); + when(mockContext.getUserInfo()).thenReturn(userInfo); when(userInfo.getTenant()).thenReturn("t123"); RepoValue repoValue = new RepoValue(); repoValue.setVirusScanEnabled(false); @@ -444,6 +445,7 @@ public void testCreateNonVersionedDIDuplicate() throws IOException { .thenReturn(mockResult); when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) .thenReturn(mockResult); + when(mockResult.rowCount()).thenReturn(0L); SDMCredentials mockSdmCredentials = mock(SDMCredentials.class); when(tokenHandler.getSDMCredentials()).thenReturn(mockSdmCredentials); @@ -501,7 +503,7 @@ public void testCreateNonVersionedDIVirus() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(eventContext.getUserInfo()).thenReturn(userInfo); + when(mockContext.getUserInfo()).thenReturn(userInfo); when(userInfo.getTenant()).thenReturn("t123"); RepoValue repoValue = new RepoValue(); repoValue.setVirusScanEnabled(false); @@ -535,6 +537,7 @@ public void testCreateNonVersionedDIVirus() throws IOException { .thenReturn(mockResult); when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) .thenReturn(mockResult); + when(mockResult.rowCount()).thenReturn(0L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); when(tokenHandler.getSDMCredentials()).thenReturn(mockSdmCredentials); when(mockContext.getAttachmentEntity()).thenReturn(mockDraftEntity); @@ -651,7 +654,7 @@ public void testCreateNonVersionedDIOther() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(eventContext.getUserInfo()).thenReturn(userInfo); + when(mockContext.getUserInfo()).thenReturn(userInfo); when(userInfo.getTenant()).thenReturn("t123"); RepoValue repoValue = new RepoValue(); repoValue.setVirusScanEnabled(false); @@ -678,8 +681,7 @@ public void testCreateNonVersionedDIOther() throws IOException { .thenReturn(mockResult); when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) - .thenReturn(mockResult); + when(mockResult.rowCount()).thenReturn(0L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); when(tokenHandler.getSDMCredentials()).thenReturn(mockSdmCredentials); @@ -997,7 +999,7 @@ public void testCreateNonVersionedDISuccess() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(eventContext.getUserInfo()).thenReturn(userInfo); + when(mockContext.getUserInfo()).thenReturn(userInfo); when(userInfo.getTenant()).thenReturn("t123"); RepoValue repoValue = new RepoValue(); repoValue.setVirusScanEnabled(false); @@ -1031,6 +1033,7 @@ public void testCreateNonVersionedDISuccess() throws IOException { .thenReturn(mockResult); when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) .thenReturn(mockResult); + when(mockResult.rowCount()).thenReturn(0L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); when(mockContext.getAttachmentEntity()).thenReturn(mockDraftEntity); when(mockDraftEntity.getQualifiedName()).thenReturn("some.qualified.name"); @@ -1074,7 +1077,7 @@ public void testCreateVirusEnabledDisableLargeFileDISuccess() throws IOException when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(eventContext.getUserInfo()).thenReturn(userInfo); + when(mockContext.getUserInfo()).thenReturn(userInfo); when(userInfo.getTenant()).thenReturn("t123"); RepoValue repoValue = new RepoValue(); repoValue.setVirusScanEnabled(true); @@ -1109,6 +1112,7 @@ public void testCreateVirusEnabledDisableLargeFileDISuccess() throws IOException .thenReturn(mockResult); when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) .thenReturn(mockResult); + when(mockResult.rowCount()).thenReturn(0L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); when(mockContext.getAttachmentEntity()).thenReturn(mockDraftEntity); when(mockDraftEntity.getQualifiedName()).thenReturn("some.qualified.name"); @@ -1255,7 +1259,7 @@ public void testCreateNonVersionedNameConstraint() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(eventContext.getUserInfo()).thenReturn(userInfo); + when(mockContext.getUserInfo()).thenReturn(userInfo); when(userInfo.getTenant()).thenReturn("t123"); RepoValue repoValue = new RepoValue(); repoValue.setVirusScanEnabled(false); @@ -1279,6 +1283,7 @@ public void testCreateNonVersionedNameConstraint() throws IOException { .thenReturn(mockResult); when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) .thenReturn(mockResult); + when(mockResult.rowCount()).thenReturn(0L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); sdmUtilsMockedStatic .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) @@ -1537,7 +1542,7 @@ public void testMaxCountErrorMessagei18n() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(eventContext.getUserInfo()).thenReturn(userInfo); + when(mockContext.getUserInfo()).thenReturn(userInfo); when(userInfo.getTenant()).thenReturn("t123"); RepoValue repoValue = new RepoValue(); repoValue.setVirusScanEnabled(false); @@ -1564,6 +1569,7 @@ public void testMaxCountErrorMessagei18n() throws IOException { .thenReturn(mockResult); when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) .thenReturn(mockResult); + when(mockResult.rowCount()).thenReturn(1L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); when(tokenHandler.getSDMCredentials()).thenReturn(mockSdmCredentials); when(mockContext.getParameterInfo()).thenReturn(parameterInfo); @@ -1609,7 +1615,7 @@ public void testMaxCountErrorMessage() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(eventContext.getUserInfo()).thenReturn(userInfo); + when(mockContext.getUserInfo()).thenReturn(userInfo); when(userInfo.getTenant()).thenReturn("t123"); RepoValue repoValue = new RepoValue(); repoValue.setVirusScanEnabled(false); @@ -1636,6 +1642,7 @@ public void testMaxCountErrorMessage() throws IOException { .thenReturn(mockResult); when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) .thenReturn(mockResult); + when(mockResult.rowCount()).thenReturn(1L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); when(tokenHandler.getSDMCredentials()).thenReturn(mockSdmCredentials); when(mockContext.getParameterInfo()).thenReturn(parameterInfo); @@ -1683,7 +1690,7 @@ public void testMaxCountError() throws IOException { when(mockAssociationType.refs()).thenReturn(Stream.of(mockCqnElementRef)); when(mockCqnElementRef.path()).thenReturn("ID"); when(mockContext.getAttachmentIds()).thenReturn(mockAttachmentIds); - when(eventContext.getUserInfo()).thenReturn(userInfo); + when(mockContext.getUserInfo()).thenReturn(userInfo); when(userInfo.getTenant()).thenReturn("t123"); RepoValue repoValue = new RepoValue(); repoValue.setVirusScanEnabled(false); @@ -1729,7 +1736,7 @@ public void testMaxCountError() throws IOException { @Test public void throwAttachmetDraftEntityException() throws IOException { - when(eventContext.getUserInfo()).thenReturn(userInfo); + when(mockContext.getUserInfo()).thenReturn(userInfo); when(userInfo.getTenant()).thenReturn("t123"); RepoValue repoValue = new RepoValue(); repoValue.setVirusScanEnabled(false); diff --git a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMServiceGenericHandlerTest.java b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMServiceGenericHandlerTest.java index 151c64637..2fbfe3ebb 100644 --- a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMServiceGenericHandlerTest.java +++ b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMServiceGenericHandlerTest.java @@ -1534,6 +1534,13 @@ void testRevertLinksForComposition() throws Exception { when(model.findEntity("AdminService.Attachments")).thenReturn(Optional.of(activeEntity)); CdsElement upElement = mock(CdsElement.class); + CdsElement upAssociation = mock(CdsElement.class); + CdsAssociationType associationType = mock(CdsAssociationType.class); + CqnElementRef mockRef = mock(CqnElementRef.class); + when(draftEntity.findAssociation("up_")).thenReturn(Optional.of(upAssociation)); + when(upAssociation.getType()).thenReturn(associationType); + when(associationType.refs()).thenReturn(Stream.of(mockRef)); + when(mockRef.path()).thenReturn("ID"); when(draftEntity.elements()).thenReturn(Stream.of(upElement)); when(upElement.getName()).thenReturn("up__ID"); @@ -1602,6 +1609,13 @@ void testRevertLinksForComposition_NoLinksToRevert() throws Exception { when(model.findEntity("AdminService.Attachments")).thenReturn(Optional.of(activeEntity)); CdsElement upElement = mock(CdsElement.class); + CdsElement upAssociation = mock(CdsElement.class); + CdsAssociationType associationType = mock(CdsAssociationType.class); + CqnElementRef mockRef = mock(CqnElementRef.class); + when(draftEntity.findAssociation("up_")).thenReturn(Optional.of(upAssociation)); + when(upAssociation.getType()).thenReturn(associationType); + when(associationType.refs()).thenReturn(Stream.of(mockRef)); + when(mockRef.path()).thenReturn("ID"); when(draftEntity.elements()).thenReturn(Stream.of(upElement)); when(upElement.getName()).thenReturn("up__ID"); @@ -1655,6 +1669,13 @@ void testRevertLinksForComposition_SameUrls() throws Exception { when(model.findEntity("AdminService.Attachments")).thenReturn(Optional.of(activeEntity)); CdsElement upElement = mock(CdsElement.class); + CdsElement upAssociation = mock(CdsElement.class); + CdsAssociationType associationType = mock(CdsAssociationType.class); + CqnElementRef mockRef = mock(CqnElementRef.class); + when(draftEntity.findAssociation("up_")).thenReturn(Optional.of(upAssociation)); + when(upAssociation.getType()).thenReturn(associationType); + when(associationType.refs()).thenReturn(Stream.of(mockRef)); + when(mockRef.path()).thenReturn("ID"); when(draftEntity.elements()).thenReturn(Stream.of(upElement)); when(upElement.getName()).thenReturn("up__ID"); @@ -1905,6 +1926,13 @@ void testRevertNestedEntityLinks_ComplexAttachments() throws Exception { .thenReturn(Optional.of(attachmentActiveEntity)); CdsElement upElement = mock(CdsElement.class); + CdsElement upAssociation = mock(CdsElement.class); + CdsAssociationType upAssocType = mock(CdsAssociationType.class); + CqnElementRef mockRef = mock(CqnElementRef.class); + when(attachmentDraftEntity.findAssociation("up_")).thenReturn(Optional.of(upAssociation)); + when(upAssociation.getType()).thenReturn(upAssocType); + when(upAssocType.refs()).thenReturn(Stream.of(mockRef)); + when(mockRef.path()).thenReturn("ID"); when(attachmentDraftEntity.elements()).thenReturn(Stream.of(upElement)); when(upElement.getName()).thenReturn("up__ID"); @@ -2638,6 +2666,20 @@ void testProcessNestedEntityComposition() throws Exception { // Mock upId key extraction for attachment entities CdsElement upElement1 = mock(CdsElement.class); CdsElement upElement2 = mock(CdsElement.class); + CdsElement upAssociation1 = mock(CdsElement.class); + CdsAssociationType upAssocType1 = mock(CdsAssociationType.class); + CqnElementRef mockRef1 = mock(CqnElementRef.class); + when(attachmentDraftEntity1.findAssociation("up_")).thenReturn(Optional.of(upAssociation1)); + when(upAssociation1.getType()).thenReturn(upAssocType1); + when(upAssocType1.refs()).thenReturn(Stream.of(mockRef1)); + when(mockRef1.path()).thenReturn("ID"); + CdsElement upAssociation2 = mock(CdsElement.class); + CdsAssociationType upAssocType2 = mock(CdsAssociationType.class); + CqnElementRef mockRef2 = mock(CqnElementRef.class); + when(attachmentDraftEntity2.findAssociation("up_")).thenReturn(Optional.of(upAssociation2)); + when(upAssociation2.getType()).thenReturn(upAssocType2); + when(upAssocType2.refs()).thenReturn(Stream.of(mockRef2)); + when(mockRef2.path()).thenReturn("ID"); when(attachmentDraftEntity1.elements()).thenReturn(Stream.of(upElement1)); when(attachmentDraftEntity2.elements()).thenReturn(Stream.of(upElement2)); when(upElement1.getName()).thenReturn("up__ID"); @@ -2971,6 +3013,27 @@ void testProcessNestedEntityComposition_MultipleAttachmentPaths() throws Excepti CdsElement upElement1 = mock(CdsElement.class); CdsElement upElement2 = mock(CdsElement.class); CdsElement upElement3 = mock(CdsElement.class); + CdsElement upAssociation1 = mock(CdsElement.class); + CdsAssociationType upAssocType1 = mock(CdsAssociationType.class); + CqnElementRef mockRef1 = mock(CqnElementRef.class); + when(attachmentDraftEntity1.findAssociation("up_")).thenReturn(Optional.of(upAssociation1)); + when(upAssociation1.getType()).thenReturn(upAssocType1); + when(upAssocType1.refs()).thenReturn(Stream.of(mockRef1)); + when(mockRef1.path()).thenReturn("ID"); + CdsElement upAssociation2 = mock(CdsElement.class); + CdsAssociationType upAssocType2 = mock(CdsAssociationType.class); + CqnElementRef mockRef2 = mock(CqnElementRef.class); + when(attachmentDraftEntity2.findAssociation("up_")).thenReturn(Optional.of(upAssociation2)); + when(upAssociation2.getType()).thenReturn(upAssocType2); + when(upAssocType2.refs()).thenReturn(Stream.of(mockRef2)); + when(mockRef2.path()).thenReturn("ID"); + CdsElement upAssociation3 = mock(CdsElement.class); + CdsAssociationType upAssocType3 = mock(CdsAssociationType.class); + CqnElementRef mockRef3 = mock(CqnElementRef.class); + when(attachmentDraftEntity3.findAssociation("up_")).thenReturn(Optional.of(upAssociation3)); + when(upAssociation3.getType()).thenReturn(upAssocType3); + when(upAssocType3.refs()).thenReturn(Stream.of(mockRef3)); + when(mockRef3.path()).thenReturn("ID"); when(attachmentDraftEntity1.elements()).thenReturn(Stream.of(upElement1)); when(attachmentDraftEntity2.elements()).thenReturn(Stream.of(upElement2)); when(attachmentDraftEntity3.elements()).thenReturn(Stream.of(upElement3)); From 9274e40cc8a11c1404e52780d4048d0e79fc2245 Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+rishikunnath2747@users.noreply.github.com> Date: Wed, 3 Dec 2025 10:37:01 +0530 Subject: [PATCH 6/9] uts --- .../handler/SDMServiceGenericHandler.java | 3 + .../SDMCreateAttachmentsHandlerTest.java | 7 +++ .../SDMAttachmentsServiceHandlerTest.java | 62 ++++++++----------- .../handler/SDMServiceGenericHandlerTest.java | 47 ++++---------- 4 files changed, 48 insertions(+), 71 deletions(-) diff --git a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java index fe53221c7..2cf970e87 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java +++ b/sdm/src/main/java/com/sap/cds/sdm/service/handler/SDMServiceGenericHandler.java @@ -197,6 +197,9 @@ private void revertLinksForComposition( CdsEntity activeEntity = model.findEntity(attachmentCompositionDefinition).get(); final String upIdKey = SDMUtils.getUpIdKey(draftEntity); + if (upIdKey == null || upIdKey.isEmpty()) { + return; + } String parentKeyName = upIdKey.replaceFirst("^up__", ""); Object parentId = parentKeys.get(parentKeyName); diff --git a/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandlerTest.java b/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandlerTest.java index d774879b6..f37b09fea 100644 --- a/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandlerTest.java +++ b/sdm/src/test/java/unit/com/sap/cds/sdm/handler/applicationservice/SDMCreateAttachmentsHandlerTest.java @@ -135,6 +135,12 @@ public void testUpdateNameWithDuplicateFilenames() throws IOException { CdsEntity targetEntity = mock(CdsEntity.class); when(targetEntity.getQualifiedName()).thenReturn("TestEntity"); when(context.getTarget()).thenReturn(targetEntity); + + // Mock the attachment entity + CdsEntity attachmentEntity = mock(CdsEntity.class); + when(context.getModel().findEntity("compositionDefinition")) + .thenReturn(Optional.of(attachmentEntity)); + // Make validateFileName execute its real implementation, and stub helper methods sdmUtilsMockedStatic .when(() -> SDMUtils.FileNameContainsWhitespace(anyList(), anyString(), anyString())) @@ -144,6 +150,7 @@ public void testUpdateNameWithDuplicateFilenames() throws IOException { () -> SDMUtils.FileNameContainsRestrictedCharaters(anyList(), anyString(), anyString())) .thenReturn(Collections.emptyList()); + sdmUtilsMockedStatic.when(() -> SDMUtils.getUpIdKey(attachmentEntity)).thenReturn("upId"); sdmUtilsMockedStatic .when( () -> diff --git a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java index 5d00d5f11..382ceb6db 100644 --- a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java +++ b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMAttachmentsServiceHandlerTest.java @@ -359,9 +359,8 @@ public void testCreateNonVersionedDuplicate() throws IOException { sdmUtilsMockedStatic .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("0__null"); - when(dbQuery.getAttachmentsForUPID(any(), any(), anyString(), anyString())) - .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + when(dbQuery.getAttachmentsForUPID(any(), any(), any(), any())).thenReturn(mockResult); + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), any(), any())) .thenReturn(mockResult); when(mockResult.rowCount()).thenReturn(0L); // Use assertThrows to expect a ServiceException and validate the message @@ -443,7 +442,7 @@ public void testCreateNonVersionedDIDuplicate() throws IOException { .thenReturn("0__null"); when(dbQuery.getAttachmentsForUPID(mockDraftEntity, persistenceService, "upid", "up__ID")) .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), any(), any())) .thenReturn(mockResult); when(mockResult.rowCount()).thenReturn(0L); SDMCredentials mockSdmCredentials = mock(SDMCredentials.class); @@ -533,9 +532,8 @@ public void testCreateNonVersionedDIVirus() throws IOException { sdmUtilsMockedStatic .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("0__null"); - when(dbQuery.getAttachmentsForUPID(any(), any(), anyString(), anyString())) - .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + when(dbQuery.getAttachmentsForUPID(any(), any(), any(), any())).thenReturn(mockResult); + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), any(), any())) .thenReturn(mockResult); when(mockResult.rowCount()).thenReturn(0L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); @@ -677,9 +675,8 @@ public void testCreateNonVersionedDIOther() throws IOException { sdmUtilsMockedStatic .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("0__null"); - when(dbQuery.getAttachmentsForUPID(any(), any(), anyString(), anyString())) - .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + when(dbQuery.getAttachmentsForUPID(any(), any(), any(), any())).thenReturn(mockResult); + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), any(), any())) .thenReturn(mockResult); when(mockResult.rowCount()).thenReturn(0L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); @@ -753,9 +750,8 @@ public void testCreateNonVersionedDIUnauthorizedI18n() throws IOException { when(documentUploadService.createDocument(any(), any(), anyBoolean())) .thenReturn(mockCreateResult); doReturn(false).when(handlerSpy).duplicateCheck(any(), any(), any()); - when(dbQuery.getAttachmentsForUPID(any(), any(), anyString(), anyString())) - .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + when(dbQuery.getAttachmentsForUPID(any(), any(), any(), any())).thenReturn(mockResult); + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), any(), any())) .thenReturn(mockResult); when(mockResult.list()).thenReturn(nonEmptyRowList); when(sdmService.getFolderId(any(), any(), any(), anyBoolean())).thenReturn("folderid"); @@ -839,9 +835,8 @@ public void testCreateNonVersionedDIUnauthorized() throws IOException { when(documentUploadService.createDocument(any(), any(), anyBoolean())) .thenReturn(mockCreateResult); doReturn(false).when(handlerSpy).duplicateCheck(any(), any(), any()); - when(dbQuery.getAttachmentsForUPID(any(), any(), anyString(), anyString())) - .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + when(dbQuery.getAttachmentsForUPID(any(), any(), any(), any())).thenReturn(mockResult); + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), any(), any())) .thenReturn(mockResult); when(mockResult.list()).thenReturn(nonEmptyRowList); when(sdmService.getFolderId(any(), any(), any(), anyBoolean())).thenReturn("folderid"); @@ -931,9 +926,8 @@ public void testCreateNonVersionedDIBlocked() throws IOException { when(documentUploadService.createDocument(any(), any(), anyBoolean())) .thenReturn(mockCreateResult); doReturn(false).when(handlerSpy).duplicateCheck(any(), any(), any()); - when(dbQuery.getAttachmentsForUPID(any(), any(), anyString(), anyString())) - .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + when(dbQuery.getAttachmentsForUPID(any(), any(), any(), any())).thenReturn(mockResult); + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), any(), any())) .thenReturn(mockResult); when(mockResult.list()).thenReturn(nonEmptyRowList); when(sdmService.getFolderId(any(), any(), any(), anyBoolean())).thenReturn("folderid"); @@ -1029,9 +1023,8 @@ public void testCreateNonVersionedDISuccess() throws IOException { sdmUtilsMockedStatic .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("0__null"); - when(dbQuery.getAttachmentsForUPID(any(), any(), anyString(), anyString())) - .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + when(dbQuery.getAttachmentsForUPID(any(), any(), any(), any())).thenReturn(mockResult); + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), any(), any())) .thenReturn(mockResult); when(mockResult.rowCount()).thenReturn(0L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); @@ -1108,9 +1101,8 @@ public void testCreateVirusEnabledDisableLargeFileDISuccess() throws IOException sdmUtilsMockedStatic .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("0__null"); - when(dbQuery.getAttachmentsForUPID(any(), any(), anyString(), anyString())) - .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + when(dbQuery.getAttachmentsForUPID(any(), any(), any(), any())).thenReturn(mockResult); + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), any(), any())) .thenReturn(mockResult); when(mockResult.rowCount()).thenReturn(0L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); @@ -1279,9 +1271,8 @@ public void testCreateNonVersionedNameConstraint() throws IOException { when(mockContext.getParameterInfo()).thenReturn(mockParameterInfo); // Mock getParameterInfo when(mockParameterInfo.getHeaders()).thenReturn(mockHeaders); // Mock getHeaders try (MockedStatic sdmUtilsMockedStatic = Mockito.mockStatic(SDMUtils.class)) { - when(dbQuery.getAttachmentsForUPID(any(), any(), anyString(), anyString())) - .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + when(dbQuery.getAttachmentsForUPID(any(), any(), any(), any())).thenReturn(mockResult); + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), any(), any())) .thenReturn(mockResult); when(mockResult.rowCount()).thenReturn(0L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); @@ -1565,9 +1556,8 @@ public void testMaxCountErrorMessagei18n() throws IOException { sdmUtilsMockedStatic .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("1__Only 1 Attachment is allowed"); - when(dbQuery.getAttachmentsForUPID(any(), any(), anyString(), anyString())) - .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + when(dbQuery.getAttachmentsForUPID(any(), any(), any(), any())).thenReturn(mockResult); + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), any(), any())) .thenReturn(mockResult); when(mockResult.rowCount()).thenReturn(1L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); @@ -1638,9 +1628,8 @@ public void testMaxCountErrorMessage() throws IOException { sdmUtilsMockedStatic .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("1__Only 1 Attachment is allowed"); - when(dbQuery.getAttachmentsForUPID(any(), any(), anyString(), anyString())) - .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + when(dbQuery.getAttachmentsForUPID(any(), any(), any(), any())).thenReturn(mockResult); + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), any(), any())) .thenReturn(mockResult); when(mockResult.rowCount()).thenReturn(1L); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); @@ -1711,9 +1700,8 @@ public void testMaxCountError() throws IOException { sdmUtilsMockedStatic .when(() -> SDMUtils.getAttachmentCountAndMessage(anyList(), any())) .thenReturn("1__null"); - when(dbQuery.getAttachmentsForUPID(any(), any(), anyString(), anyString())) - .thenReturn(mockResult); - when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), anyString(), anyString())) + when(dbQuery.getAttachmentsForUPID(any(), any(), any(), any())).thenReturn(mockResult); + when(dbQuery.getAttachmentsForUPIDAndRepository(any(), any(), any(), any())) .thenReturn(mockResult); SDMCredentials mockSdmCredentials = Mockito.mock(SDMCredentials.class); diff --git a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMServiceGenericHandlerTest.java b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMServiceGenericHandlerTest.java index 2fbfe3ebb..f5eed8b38 100644 --- a/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMServiceGenericHandlerTest.java +++ b/sdm/src/test/java/unit/com/sap/cds/sdm/service/handler/SDMServiceGenericHandlerTest.java @@ -1533,17 +1533,6 @@ void testRevertLinksForComposition() throws Exception { when(model.findEntity("AdminService.Attachments_drafts")).thenReturn(Optional.of(draftEntity)); when(model.findEntity("AdminService.Attachments")).thenReturn(Optional.of(activeEntity)); - CdsElement upElement = mock(CdsElement.class); - CdsElement upAssociation = mock(CdsElement.class); - CdsAssociationType associationType = mock(CdsAssociationType.class); - CqnElementRef mockRef = mock(CqnElementRef.class); - when(draftEntity.findAssociation("up_")).thenReturn(Optional.of(upAssociation)); - when(upAssociation.getType()).thenReturn(associationType); - when(associationType.refs()).thenReturn(Stream.of(mockRef)); - when(mockRef.path()).thenReturn("ID"); - when(draftEntity.elements()).thenReturn(Stream.of(upElement)); - when(upElement.getName()).thenReturn("up__ID"); - Result draftLinksResult = mock(Result.class); Row draftLinkRow = mock(Row.class); when(draftLinksResult.iterator()).thenReturn(Arrays.asList(draftLinkRow).iterator()); @@ -1569,6 +1558,8 @@ void testRevertLinksForComposition() throws Exception { .thenReturn(draftLinksResult) .thenReturn(activeResult); + sdmUtilsMock.when(() -> SDMUtils.getUpIdKey(draftEntity)).thenReturn("up__ID"); + Method method = SDMServiceGenericHandler.class.getDeclaredMethod( "revertLinksForComposition", DraftCancelEventContext.class, Map.class, String.class); @@ -1587,7 +1578,7 @@ void testRevertLinksForComposition() throws Exception { } }); - verify(persistenceService, atLeast(1)).run(any(CqnSelect.class)); + verify(persistenceService, times(2)).run(any(CqnSelect.class)); verify(tokenHandler, times(1)).getSDMCredentials(); verify(context, times(1)).getUserInfo(); } @@ -1608,17 +1599,6 @@ void testRevertLinksForComposition_NoLinksToRevert() throws Exception { when(model.findEntity("AdminService.Attachments_drafts")).thenReturn(Optional.of(draftEntity)); when(model.findEntity("AdminService.Attachments")).thenReturn(Optional.of(activeEntity)); - CdsElement upElement = mock(CdsElement.class); - CdsElement upAssociation = mock(CdsElement.class); - CdsAssociationType associationType = mock(CdsAssociationType.class); - CqnElementRef mockRef = mock(CqnElementRef.class); - when(draftEntity.findAssociation("up_")).thenReturn(Optional.of(upAssociation)); - when(upAssociation.getType()).thenReturn(associationType); - when(associationType.refs()).thenReturn(Stream.of(mockRef)); - when(mockRef.path()).thenReturn("ID"); - when(draftEntity.elements()).thenReturn(Stream.of(upElement)); - when(upElement.getName()).thenReturn("up__ID"); - Result emptyResult = mock(Result.class); when(emptyResult.iterator()).thenReturn(Collections.emptyIterator()); when(persistenceService.run(any(CqnSelect.class))).thenReturn(emptyResult); @@ -1629,6 +1609,8 @@ void testRevertLinksForComposition_NoLinksToRevert() throws Exception { when(context.getUserInfo()).thenReturn(userInfo); when(userInfo.isSystemUser()).thenReturn(true); + sdmUtilsMock.when(() -> SDMUtils.getUpIdKey(draftEntity)).thenReturn("up__ID"); + Method method = SDMServiceGenericHandler.class.getDeclaredMethod( "revertLinksForComposition", DraftCancelEventContext.class, Map.class, String.class); @@ -1668,17 +1650,6 @@ void testRevertLinksForComposition_SameUrls() throws Exception { when(model.findEntity("AdminService.Attachments_drafts")).thenReturn(Optional.of(draftEntity)); when(model.findEntity("AdminService.Attachments")).thenReturn(Optional.of(activeEntity)); - CdsElement upElement = mock(CdsElement.class); - CdsElement upAssociation = mock(CdsElement.class); - CdsAssociationType associationType = mock(CdsAssociationType.class); - CqnElementRef mockRef = mock(CqnElementRef.class); - when(draftEntity.findAssociation("up_")).thenReturn(Optional.of(upAssociation)); - when(upAssociation.getType()).thenReturn(associationType); - when(associationType.refs()).thenReturn(Stream.of(mockRef)); - when(mockRef.path()).thenReturn("ID"); - when(draftEntity.elements()).thenReturn(Stream.of(upElement)); - when(upElement.getName()).thenReturn("up__ID"); - Result draftLinksResult = mock(Result.class); Row draftLinkRow = mock(Row.class); when(draftLinksResult.iterator()).thenReturn(Arrays.asList(draftLinkRow).iterator()); @@ -1703,6 +1674,8 @@ void testRevertLinksForComposition_SameUrls() throws Exception { when(context.getUserInfo()).thenReturn(userInfo); when(userInfo.isSystemUser()).thenReturn(false); + sdmUtilsMock.when(() -> SDMUtils.getUpIdKey(draftEntity)).thenReturn("up__ID"); + Method method = SDMServiceGenericHandler.class.getDeclaredMethod( "revertLinksForComposition", DraftCancelEventContext.class, Map.class, String.class); @@ -1945,6 +1918,9 @@ void testRevertNestedEntityLinks_ComplexAttachments() throws Exception { Result emptyDraftLinksResult = mock(Result.class); when(emptyDraftLinksResult.iterator()).thenReturn(Collections.emptyIterator()); + // Mock SDMUtils.getUpIdKey to return non-null value + sdmUtilsMock.when(() -> SDMUtils.getUpIdKey(attachmentDraftEntity)).thenReturn("up__ID"); + try (var attachmentUtilsMock = mockStatic( com.sap.cds.sdm.handler.applicationservice.helper.AttachmentsHandlerUtils.class)) { @@ -3048,6 +3024,9 @@ void testProcessNestedEntityComposition_MultipleAttachmentPaths() throws Excepti when(context.getUserInfo()).thenReturn(userInfo); when(userInfo.isSystemUser()).thenReturn(false); + // Mock SDMUtils.getUpIdKey to return non-null value for all attachment entities + sdmUtilsMock.when(() -> SDMUtils.getUpIdKey(any(CdsEntity.class))).thenReturn("up__ID"); + // Mock the static method call try (var attachmentUtilsMock = mockStatic( From 773b1293ffe9e08344669a4c32684842d631fdf8 Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+rishikunnath2747@users.noreply.github.com> Date: Wed, 3 Dec 2025 10:42:34 +0530 Subject: [PATCH 7/9] removing logging statements --- sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java | 4 ---- 1 file changed, 4 deletions(-) diff --git a/sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java b/sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java index b8505e71f..327758e8d 100644 --- a/sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java +++ b/sdm/src/main/java/com/sap/cds/sdm/utilities/SDMUtils.java @@ -59,10 +59,6 @@ public static Set FileNameContainsWhitespace( public static Set FileNameDuplicateInDrafts( List data, String composition, String targetEntity, String upIdKey) { - logger.info("Checking for duplicate filenames in draft attachments..."); - logger.info("Using upIdKey: " + upIdKey); - logger.info("Target Entity: " + targetEntity); - logger.info("Composition: " + composition); Set uniqueFilenames = new HashSet<>(); Set duplicateFilenames = new HashSet<>(); for (Map entity : data) { From 7c03dfaf5b35f96753c5c703f038e485a62f99ab Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+rishikunnath2747@users.noreply.github.com> Date: Fri, 5 Dec 2025 20:21:49 +0530 Subject: [PATCH 8/9] change pom for testing --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index fdec1f3cc..d5a84cfb9 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ - 1.6.2-SNAPSHOT + 1.0.0-RC1 17 ${java.version} ${java.version} From ba226ae4146dbb3a6d729f1086aeecb7067fdc6c Mon Sep 17 00:00:00 2001 From: Rishi Kunnath <82925475+rishikunnath2747@users.noreply.github.com> Date: Sat, 6 Dec 2025 17:21:42 +0530 Subject: [PATCH 9/9] fix pom --- pom.xml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pom.xml b/pom.xml index d5a84cfb9..fdec1f3cc 100644 --- a/pom.xml +++ b/pom.xml @@ -23,7 +23,7 @@ - 1.0.0-RC1 + 1.6.2-SNAPSHOT 17 ${java.version} ${java.version}