Skip to content

Commit c0563ed

Browse files
Test scenarios for rename
1 parent 5be9fd1 commit c0563ed

2 files changed

Lines changed: 174 additions & 0 deletions

File tree

sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_Chapters_MultipleFacet.java

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6482,4 +6482,96 @@ void testRenameChapterAttachmentWithExtensionChange() throws IOException {
64826482
// Clean up
64836483
api.deleteEntity(appUrl, bookEntityName, newBookID);
64846484
}
6485+
6486+
@Test
6487+
@Order(77)
6488+
void testRenameChapterAttachmentWithExtensionChange_BeforeSave() throws IOException {
6489+
System.out.println(
6490+
"Test (77) : Upload chapter attachment in draft, rename changing extension before save across all facets - should return extension change warning");
6491+
6492+
for (int i = 0; i < facet.length; i++) {
6493+
// Step 1: Create a new book and chapter draft (do NOT save)
6494+
String newBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath);
6495+
if (newBookID.equals("Could not create entity")) {
6496+
fail("Could not create book for facet: " + facet[i]);
6497+
}
6498+
String newChapterID =
6499+
api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, newBookID);
6500+
if (newChapterID.equals("Could not create entity")) {
6501+
api.deleteEntityDraft(appUrl, bookEntityName, newBookID);
6502+
fail("Could not create chapter for facet: " + facet[i]);
6503+
}
6504+
6505+
// Step 2: Upload a PDF attachment to the chapter facet while book is still in draft (unsaved)
6506+
ClassLoader classLoader = getClass().getClassLoader();
6507+
File file = new File(classLoader.getResource("sample.pdf").getFile());
6508+
6509+
Map<String, Object> postData = new HashMap<>();
6510+
postData.put("up__ID", newChapterID);
6511+
postData.put("mimeType", "application/pdf");
6512+
postData.put("createdAt", new Date().toString());
6513+
postData.put("createdBy", "test@test.com");
6514+
postData.put("modifiedBy", "test@test.com");
6515+
6516+
String facetAttachmentID =
6517+
CreateandReturnFacetID(appUrl, serviceName, newChapterID, facet[i], postData, file);
6518+
if (facetAttachmentID == null) {
6519+
api.deleteEntityDraft(appUrl, bookEntityName, newBookID);
6520+
fail("Could not upload sample.pdf to chapter facet: " + facet[i]);
6521+
}
6522+
6523+
// Step 3: Rename the attachment changing extension from .pdf to .txt — book still not saved
6524+
String renameResponse =
6525+
api.renameAttachment(
6526+
appUrl,
6527+
chapterEntityName,
6528+
facet[i],
6529+
newChapterID,
6530+
facetAttachmentID,
6531+
"renamed_document.txt");
6532+
if (!"Renamed".equals(renameResponse)) {
6533+
api.deleteEntityDraft(appUrl, bookEntityName, newBookID);
6534+
fail("Could not rename chapter attachment on facet " + facet[i] + ": " + renameResponse);
6535+
}
6536+
6537+
// Step 4: Save the book — should receive extension change warning, not "Saved"
6538+
String saveWithWarningResponse =
6539+
api.saveEntityDraft(appUrl, bookEntityName, srvpath, newBookID);
6540+
assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]);
6541+
6542+
String expectedMessage =
6543+
"Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\".";
6544+
6545+
com.fasterxml.jackson.databind.JsonNode messagesNode =
6546+
new ObjectMapper().readTree(saveWithWarningResponse);
6547+
assertTrue(
6548+
messagesNode.isArray(),
6549+
"sap-messages response should be a JSON array for facet: " + facet[i]);
6550+
6551+
boolean foundExtensionError = false;
6552+
for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) {
6553+
if (messageNode.has("message")) {
6554+
String message = messageNode.get("message").asText();
6555+
if (message.contains("Changing the file extension is not allowed")) {
6556+
foundExtensionError = true;
6557+
assertEquals(
6558+
expectedMessage,
6559+
message,
6560+
"Extension change error message does not match for facet: " + facet[i]);
6561+
break;
6562+
}
6563+
}
6564+
}
6565+
6566+
assertTrue(
6567+
foundExtensionError,
6568+
"Expected extension change warning not found for facet: "
6569+
+ facet[i]
6570+
+ ". Full response: "
6571+
+ saveWithWarningResponse);
6572+
6573+
// Clean up
6574+
api.deleteEntity(appUrl, bookEntityName, newBookID);
6575+
}
6576+
}
64856577
}

sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_MultipleFacet.java

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6978,6 +6978,88 @@ void testRenameAttachmentWithExtensionChange() throws IOException {
69786978
api.deleteEntity(appUrl, entityName, newEntityID);
69796979
}
69806980

6981+
@Test
6982+
@Order(77)
6983+
void testRenameAttachmentWithExtensionChange_BeforeSave() throws IOException {
6984+
System.out.println(
6985+
"Test (77) : Upload attachment in draft, rename changing extension before save across all facets - should return extension change warning");
6986+
6987+
for (int i = 0; i < facet.length; i++) {
6988+
// Step 1: Create a new entity draft (do NOT save it yet)
6989+
String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath);
6990+
if (newEntityID.equals("Could not create entity")) {
6991+
fail("Could not create entity for facet: " + facet[i]);
6992+
}
6993+
6994+
// Step 2: Upload a PDF attachment while entity is still in draft (unsaved)
6995+
ClassLoader classLoader = getClass().getClassLoader();
6996+
File file = new File(classLoader.getResource("sample.pdf").getFile());
6997+
6998+
Map<String, Object> postData = new HashMap<>();
6999+
postData.put("up__ID", newEntityID);
7000+
postData.put("mimeType", "application/pdf");
7001+
postData.put("createdAt", new Date().toString());
7002+
postData.put("createdBy", "test@test.com");
7003+
postData.put("modifiedBy", "test@test.com");
7004+
7005+
String facetAttachmentID =
7006+
CreateandReturnFacetID(
7007+
appUrl, serviceName, entityName, facet[i], newEntityID, postData, file);
7008+
if (facetAttachmentID == null) {
7009+
api.deleteEntityDraft(appUrl, entityName, newEntityID);
7010+
fail("Could not upload sample.pdf to facet: " + facet[i]);
7011+
}
7012+
7013+
// Step 3: Rename the attachment changing extension from .pdf to .txt — entity still not saved
7014+
String renameResponse =
7015+
api.renameAttachment(
7016+
appUrl, entityName, facet[i], newEntityID, facetAttachmentID, "renamed_document.txt");
7017+
if (!"Renamed".equals(renameResponse)) {
7018+
api.deleteEntityDraft(appUrl, entityName, newEntityID);
7019+
fail("Could not rename attachment on facet " + facet[i] + ": " + renameResponse);
7020+
}
7021+
7022+
// Step 4: Save — should receive extension change warning, not "Saved"
7023+
String saveWithWarningResponse =
7024+
api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID);
7025+
assertNotNull(saveWithWarningResponse, "Response should not be null for facet: " + facet[i]);
7026+
7027+
String expectedMessage =
7028+
"Changing the file extension is not allowed. The file \"renamed_document.txt\" must retain its original extension \".pdf\".";
7029+
7030+
com.fasterxml.jackson.databind.JsonNode messagesNode =
7031+
new ObjectMapper().readTree(saveWithWarningResponse);
7032+
assertTrue(
7033+
messagesNode.isArray(),
7034+
"sap-messages response should be a JSON array for facet: " + facet[i]);
7035+
7036+
boolean foundExtensionError = false;
7037+
for (com.fasterxml.jackson.databind.JsonNode messageNode : messagesNode) {
7038+
if (messageNode.has("message")) {
7039+
String message = messageNode.get("message").asText();
7040+
if (message.contains("Changing the file extension is not allowed")) {
7041+
foundExtensionError = true;
7042+
assertEquals(
7043+
expectedMessage,
7044+
message,
7045+
"Extension change error message does not match for facet: " + facet[i]);
7046+
break;
7047+
}
7048+
}
7049+
}
7050+
7051+
assertTrue(
7052+
foundExtensionError,
7053+
"Expected extension change warning not found for facet: "
7054+
+ facet[i]
7055+
+ ". Full response: "
7056+
+ saveWithWarningResponse);
7057+
7058+
// Clean up
7059+
api.deleteEntity(appUrl, entityName, newEntityID);
7060+
}
7061+
}
7062+
69817063
// @Test
69827064
// @Order(77)
69837065
// void testUploadAttachmentExceedingMaximumFileSize() throws IOException {

0 commit comments

Comments
 (0)