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 60708b671..2ae1af567 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 @@ -277,8 +277,15 @@ private void revertLinksForComposition( CdsModel model = context.getModel(); String draftEntityName = attachmentCompositionDefinition + "_drafts"; - CdsEntity draftEntity = model.findEntity(draftEntityName).get(); - CdsEntity activeEntity = model.findEntity(attachmentCompositionDefinition).get(); + Optional draftEntityOpt = model.findEntity(draftEntityName); + Optional activeEntityOpt = model.findEntity(attachmentCompositionDefinition); + if (!draftEntityOpt.isPresent() || !activeEntityOpt.isPresent()) { + logger.debug( + "Entity not found in model, skipping revert for: {}", attachmentCompositionDefinition); + return; + } + CdsEntity draftEntity = draftEntityOpt.get(); + CdsEntity activeEntity = activeEntityOpt.get(); final String upIdKey = SDMUtils.getUpIdKey(draftEntity); if (upIdKey == null || upIdKey.isEmpty()) { @@ -299,6 +306,9 @@ private void revertLinksForComposition( Result draftLinks = persistenceService.run(selectDraftLinks); logger.debug("Found {} draft links to process", draftLinks.rowCount()); + if (draftLinks.rowCount() == 0) { + return; + } SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials(); Boolean isSystemUser = context.getUserInfo().isSystemUser(); diff --git a/sdm/src/main/resources/cds/com.sap.cds/sdm/attachments.cds b/sdm/src/main/resources/cds/com.sap.cds/sdm/attachments.cds index cc82cb922..0ec521ebe 100644 --- a/sdm/src/main/resources/cds/com.sap.cds/sdm/attachments.cds +++ b/sdm/src/main/resources/cds/com.sap.cds/sdm/attachments.cds @@ -22,8 +22,6 @@ extend aspect Attachments with { type : String @(UI: {IsImageURL: true}) default 'sap-icon://document'; uploadStatus : UploadStatusCode default 'uploading' @readonly ; uploadStatusNav : Association to one UploadScanStates on uploadStatusNav.code = uploadStatus; - } actions { - action downloadSelectedAttachments(ids: String) returns String; } entity UploadScanStates : CodeList { key code : UploadStatusCode @Common.Text: name @Common.TextArrangement: #TextOnly; 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 21dcb10f9..4a0aa163c 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 @@ -2149,6 +2149,7 @@ void testRevertLinksForComposition() throws Exception { Result draftLinksResult = mock(Result.class); Row draftLinkRow = mock(Row.class); + when(draftLinksResult.rowCount()).thenReturn(1L); when(draftLinksResult.iterator()).thenReturn(Arrays.asList(draftLinkRow).iterator()); when(persistenceService.run(any(CqnSelect.class))).thenReturn(draftLinksResult); @@ -2244,8 +2245,8 @@ void testRevertLinksForComposition_NoLinksToRevert() throws Exception { }); verify(persistenceService, times(1)).run(any(CqnSelect.class)); - verify(tokenHandler, times(1)).getSDMCredentials(); - verify(context, times(1)).getUserInfo(); + verify(tokenHandler, never()).getSDMCredentials(); + verify(context, never()).getUserInfo(); } @Test @@ -2266,6 +2267,7 @@ void testRevertLinksForComposition_SameUrls() throws Exception { Result draftLinksResult = mock(Result.class); Row draftLinkRow = mock(Row.class); + when(draftLinksResult.rowCount()).thenReturn(1L); when(draftLinksResult.iterator()).thenReturn(Arrays.asList(draftLinkRow).iterator()); when(draftLinkRow.get("ID")).thenReturn("attachment123");