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

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion sdm/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
<test-generation-folder>src/test/gen</test-generation-folder>
<maven.compiler.source>17</maven.compiler.source>
<maven.compiler.target>17</maven.compiler.target>
<attachments_version>1.3.1</attachments_version>
<attachments_version>1.4.0</attachments_version>
<lombok.version>1.18.36</lombok.version>
<jacoco.version>0.8.7</jacoco.version>
<ehcache-version>3.10.8</ehcache-version>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ public Map<String, Object> getUaaCredentials() {
DefaultServiceBindingAccessor.getInstance().getServiceBindings();
ServiceBinding sdmBinding =
allServiceBindings.stream()
.filter(binding -> "sdm".equalsIgnoreCase(binding.getServiceName().orElse(null)))
.filter(binding -> binding.getTags().contains("sdm"))
.findFirst()
.orElseThrow(() -> new IllegalStateException("SDM binding not found"));
return sdmBinding.getCredentials();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,8 @@ public AttachmentModificationResult createAttachment(CreateAttachmentInput input
return new AttachmentModificationResult(
Boolean.TRUE.equals(createContext.getIsInternalStored()),
createContext.getContentId(),
createContext.getData().getStatus());
createContext.getData().getStatus(),
Instant.now());
}

@Override
Expand Down
3 changes: 2 additions & 1 deletion sdm/src/main/java/com/sap/cds/sdm/service/SDMService.java
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ public String getFolderIdByPath(

public JSONObject getRepositoryInfo(SDMCredentials sdmCredentials) throws IOException;

public int deleteDocument(String cmisaction, String objectId, String user) throws IOException;
public int deleteDocument(String cmisaction, String objectId, String user, Boolean isSystemUser)
throws IOException;

public void readDocument(
String objectId, SDMCredentials sdmCredentials, AttachmentReadEventContext context)
Expand Down
10 changes: 7 additions & 3 deletions sdm/src/main/java/com/sap/cds/sdm/service/SDMServiceImpl.java
Original file line number Diff line number Diff line change
Expand Up @@ -660,13 +660,17 @@ public Map<String, RepoValue> fetchRepositoryData(JSONObject repoInfo, String re
}

@Override
public int deleteDocument(String cmisaction, String objectId, String user) {
public int deleteDocument(String cmisaction, String objectId, String user, Boolean isSytemUser) {
logger.info(
"Deleting document - action: {}, objectId: {}, user: {}", cmisaction, objectId, user);
"Deleting document - action: {}, objectId: {}, user: {},isSystemUser :{}",
cmisaction,
objectId,
user,
isSytemUser);
long startTime = System.currentTimeMillis();
SDMCredentials sdmCredentials = tokenHandler.getSDMCredentials();
HttpClient httpClient;
if (user.equals(SDMConstants.SYSTEM_USER)) {
if (isSytemUser) {
logger.debug("Using TECHNICAL_USER_FLOW for deletion");
httpClient = tokenHandler.getHttpClient(binding, connectionPool, null, TECHNICAL_USER_FLOW);
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
import com.sap.cds.services.utils.StringUtils;
import java.io.IOException;
import java.io.InputStream;
import java.time.Instant;
import java.util.*;
import java.util.stream.Collectors;
import org.json.JSONObject;
Expand Down Expand Up @@ -107,12 +108,20 @@ public void markAttachmentAsDeleted(AttachmentMarkAsDeletedEventContext context)
if (cmisDocuments.isEmpty()) {
// deleteFolder API
logger.info("Deleting folder: {} for entity: {}", folderId, entity);
sdmService.deleteDocument("deleteTree", folderId, context.getDeletionUserInfo().getName());
sdmService.deleteDocument(
"deleteTree",
folderId,
context.getDeletionUserInfo().getName(),
context.getDeletionUserInfo().getIsSystemUser());
logger.info("Folder deleted successfully: {}", folderId);
} else {
if (!isObjectIdPresent(cmisDocuments, objectId)) {
logger.info("Deleting document: {} from repository", objectId);
sdmService.deleteDocument("delete", objectId, context.getDeletionUserInfo().getName());
sdmService.deleteDocument(
"delete",
objectId,
context.getDeletionUserInfo().getName(),
context.getDeletionUserInfo().getIsSystemUser());
logger.info("Document deleted successfully: {}", objectId);
} else {
logger.debug("ObjectId {} is still referenced, not deleting", objectId);
Expand Down Expand Up @@ -516,6 +525,7 @@ private void handleCreateDocumentResult(
eventContext.setContentId(
existing.getObjectId() + ":" + existing.getFolderId() + ":" + activeEntityName);
eventContext.getData().setStatus("Clean");
eventContext.getData().setScannedAt(Instant.now());
eventContext.getData().setContent(null);
eventContext.setCompleted();
return;
Expand Down Expand Up @@ -620,6 +630,7 @@ private void finalizeContext(
+ ":"
+ eventContext.getAttachmentEntity().getQualifiedName());
eventContext.getData().setStatus("Clean");
eventContext.getData().setScannedAt(Instant.now());
eventContext.getData().setContent(null);
eventContext.setCompleted();
logger.debug("Attachment context finalized and marked as completed");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1525,13 +1525,20 @@ private void handleCopyFailure(
logger.error("Copy failure detected, initiating cleanup. Error: {}", e.getMessage());
if (!folderExists) {
logger.debug("Deleting newly created folder: {}", folderId);
sdmService.deleteDocument("deleteTree", folderId, context.getUserInfo().getName());
sdmService.deleteDocument(
"deleteTree",
folderId,
context.getUserInfo().getName(),
context.getUserInfo().isSystemUser());
} else {
logger.debug(
"Deleting {} copied attachments from existing folder", attachmentsMetadata.size());
for (Map<String, String> attachmentMetadata : attachmentsMetadata) {
sdmService.deleteDocument(
"delete", attachmentMetadata.get("cmis:objectId"), context.getUserInfo().getName());
"delete",
attachmentMetadata.get("cmis:objectId"),
context.getUserInfo().getName(),
context.getUserInfo().isSystemUser());
}
}
throw new ServiceException(e.getMessage());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ public void testGetSDMCredentials() {
mockCredentials.put("uaa", mockUaa);
mockCredentials.put("uri", "https://mock.service.url");

Mockito.when(mockServiceBinding.getServiceName()).thenReturn(Optional.of("sdm"));
Mockito.when(mockServiceBinding.getTags()).thenReturn(Collections.singletonList("sdm"));
Mockito.when(mockServiceBinding.getCredentials()).thenReturn(mockCredentials);

List<ServiceBinding> mockServiceBindings = Collections.singletonList(mockServiceBinding);
Expand Down Expand Up @@ -202,7 +202,7 @@ public void testGetHttpClientForOnboardFlow() {
mockCredentials.put("uaa", mockUaa);
mockCredentials.put("uri", "https://mock.service.url");

Mockito.when(mockServiceBinding.getServiceName()).thenReturn(Optional.of("sdm"));
Mockito.when(mockServiceBinding.getTags()).thenReturn(Collections.singletonList("sdm"));
Mockito.when(mockServiceBinding.getCredentials()).thenReturn(mockCredentials);

List<ServiceBinding> mockServiceBindings = Collections.singletonList(mockServiceBinding);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -809,10 +809,14 @@ public void testDeleteFolder() throws IOException {
when(response.getEntity()).thenReturn(entity);
when(mockContext.getDeletionUserInfo()).thenReturn(deletionUserInfo);
when(deletionUserInfo.getName()).thenReturn("system-internal");
when(deletionUserInfo.getIsSystemUser()).thenReturn(true);
SDMServiceImpl sdmServiceImpl = new SDMServiceImpl(binding, connectionPool, tokenHandler);
int actualResponse =
sdmServiceImpl.deleteDocument(
"deleteTree", "objectId", mockContext.getDeletionUserInfo().getName());
"deleteTree",
"objectId",
mockContext.getDeletionUserInfo().getName(),
mockContext.getDeletionUserInfo().getIsSystemUser());
assertEquals(200, actualResponse);
} finally {
mockWebServer.shutdown();
Expand Down Expand Up @@ -841,7 +845,10 @@ public void testDeleteFolderAuthorities() throws IOException {
SDMServiceImpl sdmServiceImpl = new SDMServiceImpl(binding, connectionPool, tokenHandler);
int actualResponse =
sdmServiceImpl.deleteDocument(
"deleteTree", "objectId", mockContext.getDeletionUserInfo().getName());
"deleteTree",
"objectId",
mockContext.getDeletionUserInfo().getName(),
mockContext.getDeletionUserInfo().getIsSystemUser());
assertEquals(200, actualResponse);
} finally {
mockWebServer.shutdown();
Expand Down Expand Up @@ -903,10 +910,14 @@ public void testDeleteDocument() throws IOException {
when(tokenHandler.getSDMCredentials()).thenReturn(mockSdmCredentials);
when(mockContext.getDeletionUserInfo()).thenReturn(deletionUserInfo);
when(deletionUserInfo.getName()).thenReturn("system-internal");
when(deletionUserInfo.getIsSystemUser()).thenReturn(true);
SDMServiceImpl sdmServiceImpl = new SDMServiceImpl(binding, connectionPool, tokenHandler);
int actualResponse =
sdmServiceImpl.deleteDocument(
"delete", "objectId", mockContext.getDeletionUserInfo().getName());
"delete",
"objectId",
mockContext.getDeletionUserInfo().getName(),
mockContext.getDeletionUserInfo().getIsSystemUser());
assertEquals(200, actualResponse);
} finally {
mockWebServer.shutdown();
Expand Down Expand Up @@ -934,7 +945,10 @@ public void testDeleteDocumentNamedUserFlow() throws IOException {
SDMServiceImpl sdmServiceImpl = new SDMServiceImpl(binding, connectionPool, tokenHandler);
int actualResponse =
sdmServiceImpl.deleteDocument(
"delete", "objectId", mockContext.getDeletionUserInfo().getName());
"delete",
"objectId",
mockContext.getDeletionUserInfo().getName(),
mockContext.getDeletionUserInfo().getIsSystemUser());
assertEquals(200, actualResponse);
} finally {
mockWebServer.shutdown();
Expand All @@ -961,10 +975,14 @@ public void testDeleteDocumentObjectNotFound() throws IOException {
when(tokenHandler.getSDMCredentials()).thenReturn(mockSdmCredentials);
when(mockContext.getDeletionUserInfo()).thenReturn(deletionUserInfo);
when(deletionUserInfo.getName()).thenReturn("system-internal");
when(deletionUserInfo.getIsSystemUser()).thenReturn(true);
SDMServiceImpl sdmServiceImpl = new SDMServiceImpl(binding, connectionPool, tokenHandler);
int actualResponse =
sdmServiceImpl.deleteDocument(
"delete", "ewdwe", mockContext.getDeletionUserInfo().getName());
"delete",
"ewdwe",
mockContext.getDeletionUserInfo().getName(),
mockContext.getDeletionUserInfo().getIsSystemUser());
assertEquals(404, actualResponse);
} finally {
mockWebServer.shutdown();
Expand Down Expand Up @@ -1296,12 +1314,16 @@ public void testDeleteDocumentThrowsServiceExceptionOnHttpClientError() throws I
SDMServiceImpl sdmServiceImpl = new SDMServiceImpl(binding, connectionPool, tokenHandler);
when(mockContext.getDeletionUserInfo()).thenReturn(deletionUserInfo);
when(deletionUserInfo.getName()).thenReturn("system-internal");
when(deletionUserInfo.getIsSystemUser()).thenReturn(true);
// Ensure ServiceException is thrown
assertThrows(
ServiceException.class,
() ->
sdmServiceImpl.deleteDocument(
"delete", "123", mockContext.getDeletionUserInfo().getName()));
"delete",
"123",
mockContext.getDeletionUserInfo().getName(),
mockContext.getDeletionUserInfo().getIsSystemUser()));
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1425,7 +1425,8 @@ public void testDocumentDeletion() throws IOException {
.deleteDocument(
"delete",
objectId,
attachmentMarkAsDeletedEventContext.getDeletionUserInfo().getName());
attachmentMarkAsDeletedEventContext.getDeletionUserInfo().getName(),
attachmentMarkAsDeletedEventContext.getDeletionUserInfo().getIsSystemUser());
}

@Test
Expand Down Expand Up @@ -1461,7 +1462,8 @@ public void testFolderDeletion() throws IOException {
.deleteDocument(
"deleteTree",
folderId,
attachmentMarkAsDeletedEventContext.getDeletionUserInfo().getName());
attachmentMarkAsDeletedEventContext.getDeletionUserInfo().getName(),
attachmentMarkAsDeletedEventContext.getDeletionUserInfo().getIsSystemUser());
}

@Test
Expand Down Expand Up @@ -2054,7 +2056,7 @@ public void testMarkAttachmentAsDeleted_WithNullObjectId() throws IOException {
handlerSpy.markAttachmentAsDeleted(deleteContext);

verify(deleteContext).setCompleted();
verify(sdmService, never()).deleteDocument(anyString(), anyString(), anyString());
verify(sdmService, never()).deleteDocument(anyString(), anyString(), anyString(), anyBoolean());
}

@Test
Expand All @@ -2068,7 +2070,7 @@ public void testMarkAttachmentAsDeleted_WithInsufficientContextValues() throws I
handlerSpy.markAttachmentAsDeleted(deleteContext);

verify(deleteContext).setCompleted();
verify(sdmService, never()).deleteDocument(anyString(), anyString(), anyString());
verify(sdmService, never()).deleteDocument(anyString(), anyString(), anyString(), anyBoolean());
}

@Test
Expand All @@ -2083,7 +2085,7 @@ public void testMarkAttachmentAsDeleted_WithEmptyString() throws IOException {
handlerSpy.markAttachmentAsDeleted(deleteContext);

verify(deleteContext).setCompleted();
verify(sdmService, never()).deleteDocument(anyString(), anyString(), anyString());
verify(sdmService, never()).deleteDocument(anyString(), anyString(), anyString(), anyBoolean());
}

@Test
Expand All @@ -2103,7 +2105,7 @@ public void testMarkAttachmentAsDeleted_DeleteFolderWhenNoAttachments() throws I

handlerSpy.markAttachmentAsDeleted(deleteContext);

verify(sdmService).deleteDocument("deleteTree", "folderId", "testUser");
verify(sdmService).deleteDocument("deleteTree", "folderId", "testUser", false);
verify(deleteContext).setCompleted();
}

Expand All @@ -2127,7 +2129,7 @@ public void testMarkAttachmentAsDeleted_DeleteObjectWhenNotPresent() throws IOEx

handlerSpy.markAttachmentAsDeleted(deleteContext);

verify(sdmService).deleteDocument("delete", "objectId", "testUser");
verify(sdmService).deleteDocument("delete", "objectId", "testUser", false);
verify(deleteContext).setCompleted();
}

Expand All @@ -2151,7 +2153,7 @@ public void testMarkAttachmentAsDeleted_ObjectIdPresent() throws IOException {

handlerSpy.markAttachmentAsDeleted(deleteContext);

verify(sdmService, never()).deleteDocument(anyString(), anyString(), anyString());
verify(sdmService, never()).deleteDocument(anyString(), anyString(), anyString(), anyBoolean());
verify(deleteContext).setCompleted();
}

Expand Down Expand Up @@ -2416,7 +2418,7 @@ public void testMarkAttachmentAsDeleted_MultipleObjectsInFolder() throws IOExcep
handlerSpy.markAttachmentAsDeleted(deleteContext);

// Should not call delete on either document since target is present
verify(sdmService, never()).deleteDocument(anyString(), anyString(), anyString());
verify(sdmService, never()).deleteDocument(anyString(), anyString(), anyString(), anyBoolean());
verify(deleteContext).setCompleted();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -242,7 +242,8 @@ void testCopyAttachments_AttachmentCopyFails() throws IOException {
});

// Verify that deleteDocument was called for cleanup of the first successful attachment
verify(sdmService, times(1)).deleteDocument(eq("delete"), eq(OBJECT_ID), eq("testUser"));
verify(sdmService, times(1))
.deleteDocument(eq("delete"), eq(OBJECT_ID), eq("testUser"), eq(false));
assertTrue(exception.getMessage().contains("Copy failed"));
}

Expand Down Expand Up @@ -279,7 +280,7 @@ void testCopyAttachments_AttachmentCopyFails_FolderDoesNotExist() throws IOExcep
});

// Should attempt to delete the folder
verify(sdmService, times(1)).deleteDocument(eq("deleteTree"), eq(FOLDER_ID), any());
verify(sdmService, times(1)).deleteDocument(eq("deleteTree"), eq(FOLDER_ID), any(), any());
assertTrue(ex.getMessage().contains("Copy failed"));
}

Expand Down Expand Up @@ -320,7 +321,7 @@ void testCopyAttachments_AttachmentCopyFails_FolderExists_AttachmentsDeleted()
});

// Should attempt to delete the copied attachment
verify(sdmService, times(1)).deleteDocument(eq("delete"), eq(OBJECT_ID), any());
verify(sdmService, times(1)).deleteDocument(eq("delete"), eq(OBJECT_ID), any(), any());
assertTrue(ex.getMessage().contains("Copy failed"));
}

Expand Down
Loading