@@ -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}
0 commit comments