From 5264bca937870645d1a329d294107a917c75f8ed Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Tue, 26 May 2026 11:50:06 +0530 Subject: [PATCH 1/8] Download all files test --- .../java/integration/com/sap/cds/sdm/Api.java | 52 + .../com/sap/cds/sdm/ApiInterface.java | 4 + .../integration/com/sap/cds/sdm/ApiMT.java | 48 + .../cds/sdm/IntegrationTest_SingleFacet.java | 11056 ++++++++-------- 4 files changed, 5765 insertions(+), 5395 deletions(-) diff --git a/sdm/src/test/java/integration/com/sap/cds/sdm/Api.java b/sdm/src/test/java/integration/com/sap/cds/sdm/Api.java index b8f92136c..a3fdacce0 100644 --- a/sdm/src/test/java/integration/com/sap/cds/sdm/Api.java +++ b/sdm/src/test/java/integration/com/sap/cds/sdm/Api.java @@ -1113,6 +1113,58 @@ public List> fetchEntityMetadataDraft( } } + public String downloadSelectedAttachments( + String appUrl, String entityName, String facetName, String entityID, List ids) + throws IOException { + String url = + "https://" + + appUrl + + "/odata/v4/" + + serviceName + + "/" + + entityName + + "(ID=" + + entityID + + ",IsActiveEntity=true)" + + "/" + + facetName + + "(up__ID=" + + entityID + + ",ID=" + + ids.get(0) + + ",IsActiveEntity=true)" + + "/" + + serviceName + + ".downloadSelectedAttachments"; + + String idsParam = String.join(",", ids); + String jsonPayload = "{\"ids\": \"" + idsParam + "\"}"; + + RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonPayload); + + Request request = + new Request.Builder().url(url).post(body).addHeader("Authorization", token).build(); + + try (Response response = executeWithRetry(request)) { + if (!response.isSuccessful()) { + throw new IOException( + "Could not download attachments: " + + response.code() + + " - " + + response.body().string()); + } + String responseBody = response.body().string(); + Map responseMap = objectMapper.readValue(responseBody, Map.class); + if (responseMap.containsKey("value")) { + return responseMap.get("value").toString(); + } + return responseBody; + } catch (IOException e) { + System.out.println("Error while downloading attachments: " + e.getMessage()); + throw new IOException(e); + } + } + public Map fetchChangelog( String appUrl, String entityName, String facetName, String entityID, String ID) throws IOException { diff --git a/sdm/src/test/java/integration/com/sap/cds/sdm/ApiInterface.java b/sdm/src/test/java/integration/com/sap/cds/sdm/ApiInterface.java index b41acae84..d15f1c090 100644 --- a/sdm/src/test/java/integration/com/sap/cds/sdm/ApiInterface.java +++ b/sdm/src/test/java/integration/com/sap/cds/sdm/ApiInterface.java @@ -118,6 +118,10 @@ public String openAttachment( String deleteEntityDraft(String appUrl, String entityName, String entityID); + public String downloadSelectedAttachments( + String appUrl, String entityName, String facetName, String entityID, List ids) + throws IOException; + public Map fetchChangelog( String appUrl, String entityName, String facetName, String entityID, String ID) throws IOException; diff --git a/sdm/src/test/java/integration/com/sap/cds/sdm/ApiMT.java b/sdm/src/test/java/integration/com/sap/cds/sdm/ApiMT.java index d4c380bc0..a51f8bd7a 100644 --- a/sdm/src/test/java/integration/com/sap/cds/sdm/ApiMT.java +++ b/sdm/src/test/java/integration/com/sap/cds/sdm/ApiMT.java @@ -1051,6 +1051,54 @@ public List> fetchEntityMetadataDraft( } } + public String downloadSelectedAttachments( + String appUrl, String entityName, String facetName, String entityID, List ids) + throws IOException { + String url = + "https://" + + appUrl + + "/api/admin/" + + entityName + + "(ID=" + + entityID + + ",IsActiveEntity=true)" + + "/" + + facetName + + "(up__ID=" + + entityID + + ",ID=" + + ids.get(0) + + ",IsActiveEntity=true)" + + "/AdminService.downloadSelectedAttachments"; + + String idsParam = String.join(",", ids); + String jsonPayload = "{\"ids\": \"" + idsParam + "\"}"; + + RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonPayload); + + Request request = + new Request.Builder().url(url).post(body).addHeader("Authorization", token).build(); + + try (Response response = executeWithRetry(request)) { + if (!response.isSuccessful()) { + throw new IOException( + "Could not download attachments: " + + response.code() + + " - " + + response.body().string()); + } + String responseBody = response.body().string(); + Map responseMap = objectMapper.readValue(responseBody, Map.class); + if (responseMap.containsKey("value")) { + return responseMap.get("value").toString(); + } + return responseBody; + } catch (IOException e) { + System.out.println("Error while downloading attachments: " + e.getMessage()); + throw new IOException(e); + } + } + public Map fetchChangelog( String appUrl, String entityName, String facetName, String entityID, String ID) throws IOException { diff --git a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java index 824b0d134..e46839470 100644 --- a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java +++ b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java @@ -9,11 +9,9 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardCopyOption; -import java.time.LocalDateTime; import java.util.*; -import java.util.stream.Collectors; import okhttp3.*; -import okio.ByteString; +import org.json.JSONArray; import org.json.JSONObject; import org.junit.jupiter.api.*; @@ -540,5829 +538,6097 @@ void testCreateAttachmentWithRestrictedCharacterInFilename() throws IOException } } - @Test - @Order(10) - void testDraftUpdateWithFileUploadDeleteAndCreate() throws IOException { - System.out.println("Test (10): Upload attachments, delete one and create entity"); - - boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (response != "Could not create entity") { - - entityID5 = response; - ClassLoader classLoader = getClass().getClassLoader(); - - File file = new File(classLoader.getResource("sample.pdf").getFile()); - Map postData1 = new HashMap<>(); - postData1.put("up__ID", entityID5); - postData1.put("mimeType", "application/pdf"); - postData1.put("createdAt", new Date().toString()); - postData1.put("createdBy", "test@test.com"); - postData1.put("modifiedBy", "test@test.com"); - - List createResponse1 = - api.createAttachment(appUrl, entityName, facetName, entityID5, srvpath, postData1, file); - if (createResponse1.get(0).equals("Attachment created")) { - attachmentID7 = createResponse1.get(1); - } - - file = new File(classLoader.getResource("sample.txt").getFile()); - Map postData2 = new HashMap<>(); - postData2.put("up__ID", entityID5); - postData2.put("mimeType", "application/txt"); - postData2.put("createdAt", new Date().toString()); - postData2.put("createdBy", "test@test.com"); - postData2.put("modifiedBy", "test@test.com"); - - List createResponse2 = - api.createAttachment(appUrl, entityName, facetName, entityID5, srvpath, postData2, file); - if (createResponse2.get(0).equals("Attachment created")) { - attachmentID8 = createResponse2.get(1); - } - response = api.deleteAttachment(appUrl, entityName, facetName, entityID5, attachmentID8); - if (response.equals("Deleted")) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); - - if (response.equals("Saved")) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("Failed to create entity after deleting one attachment"); - } - } - - @Test - @Order(11) - void testUpdateEntityDraft() throws IOException { - System.out.println("Test (11): Update entity in draft"); - boolean testStatus = false; - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - - File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); - Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID5); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID5); - if (response.equals("Entity in draft mode")) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, entityID5, srvpath, postData, tempFile); - String check = createResponse.get(0); - if (check.equals("Attachment created")) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); - if (response.equals("Saved")) { - testStatus = true; - } - } - } - if (!testStatus) { - fail("update entity draft with uploading attachment failed"); - } - api.deleteEntity(appUrl, entityName, entityID5); - } - - @Test - @Order(12) - void testRenameSingleAttachment() { - System.out.println("Test (12) : Rename single attachment"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - String name = "sample123"; - if (response == "Entity in draft mode") { - response = api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, name); - if (response.equals("Renamed")) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (response.equals("Saved")) { - testStatus = true; - } - } else { - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - if (!testStatus) { - fail("Attachment was not renamed"); - } - } - - @Test - @Order(13) - void testRenameAttachmentWithUnsupportedCharacter() { - System.out.println("Test (13) : Rename single attachment with unsupported characters"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - String name = "invalid/name"; - if (response == "Entity in draft mode") { - response = api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, name); - if (response.equals("Renamed")) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - "{\"error\":{\"code\":\"400\",\"message\":\"\\\"invalid/name\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; - if (response.equals(expected)) { - api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, "sample123"); - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if ("Saved".equals(response)) testStatus = true; - } - } else { - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - if (!testStatus) { - fail("Attachment was renamed with unsupported characters"); - } - } - - @Test - @Order(14) - void testRenameMultipleAttachments() { - System.out.println("Test (14) : Rename multiple attachments"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - String name1 = "sample1234"; - String name2 = "sample12345"; - if (response == "Entity in draft mode") { - String response1 = - api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID2, name1); - String response2 = - api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID3, name2); - if (response1.equals("Renamed") && response2.equals("Renamed")) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (response.equals("Saved")) { - testStatus = true; - } - } else { - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - if (!testStatus) { - fail("Attachment was not renamed"); - } - } - - @Test - @Order(15) - void testRenameSingleAttachmentDuplicate() { - System.out.println("Test (15) : Rename single attachment duplicate"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - String name = "sample123"; - String name2 = "sample123456"; - if (response == "Entity in draft mode") { - response = api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID3, name); - if (response.equals("Renamed")) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"sample123\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; - if (response.equals(expected)) { - response = - api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID3, name2); - if (response.equals("Renamed")) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (response.equals("Saved")) { - testStatus = true; - } - } - } - } else { - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - if (!testStatus) { - fail("Attachment was renamed"); - } - } - - @Test - @Order(16) - void testRenameMultipleAttachmentsWithOneUnsupportedCharacter() { - System.out.println( - "Test (16) : Rename multiple attachments where one name has unsupported characters"); - Boolean testStatus = false; - - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - - if (response.equals("Entity in draft mode")) { - String validName1 = "valid_attachment1.pdf"; - String invalidName2 = "invalid/attachment2.pdf"; - - String renameResponse1 = - api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, validName1); - String renameResponse2 = - api.renameAttachment( - appUrl, entityName, facetName, entityID, attachmentID2, invalidName2); - - if (renameResponse1.equals("Renamed") && renameResponse2.equals("Renamed")) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - "{\"error\":{\"code\":\"400\",\"message\":\"\\\"invalid/attachment2.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; - if (response.equals(expected)) { - api.renameAttachment( - appUrl, entityName, facetName, entityID, attachmentID2, "sample1234"); - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if ("Saved".equals(response)) testStatus = true; - } - } else { - api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - - if (!testStatus) { - fail("Multiple renames should have failed due to one unsupported characters"); - } - } - - @Test - @Order(17) - void testRenameSingleAttachmentWithoutSDMRole() throws IOException { - System.out.println("Test (17) : Rename attachments where user don't have SDM Roles"); - boolean testStatus = false; - String apiResponse = apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, entityID); - String name = "sample123"; // Renaming the attachment - if (apiResponse == "Entity in draft mode") { - apiResponse = - apiNoRoles.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, name); - if (apiResponse.equals("Renamed")) { - apiResponse = apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); - String expected = - "[{\"code\":\"\",\"message\":\"Could not update the following files. \\n" - + // - "\\n" - + // - "\\t\\u2022 valid_attachment1.pdf\\n" - + // - "\\n" - + // - "You do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - if (apiResponse.equals(expected)) { - testStatus = true; - } - } else { - apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); - } - } - if (!testStatus) { - fail("Attachment got renamed without SDM roles."); - } - } - - @Test - @Order(18) - void testRenameToValidateNames() throws IOException { - System.out.println("Test (18) : Rename attachments to validate names"); - boolean testStatus = false, successCount = true; - String generatedID = ""; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - entityID3 = response; - String[] filetoUpload = {"sample.pdf", "sample.txt", "sample.exe", "sample2.pdf"}; - String[] names = {"Restricted/Character", " ", "duplicateName.pdf", "duplicateName.pdf"}; - - ClassLoader classLoader = getClass().getClassLoader(); - Map postData = new HashMap<>(); - postData.put("up__ID", entityID3); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - for (int i = 0; i < filetoUpload.length; i++) { - File file = new File(classLoader.getResource(filetoUpload[i]).getFile()); - List createResponse = - api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData, file); - generatedID = createResponse.get(1); - response = - api.renameAttachment(appUrl, entityName, facetName, entityID3, generatedID, names[i]); - successCount &= "Renamed".equals(response); - } - if (successCount) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - String expected = - "{\"error\":{\"code\":\"400\",\"message\":\"The object name cannot be empty or consist entirely of space characters. Enter a value.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"Restricted/Character\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An object named \\\"duplicateName.pdf\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - if (response.equals(expected)) { - response = api.deleteEntityDraft(appUrl, entityName, entityID3); - if (response.equals("Entity Draft Deleted")) testStatus = true; - } - } - if (!testStatus) fail("Could not create entity"); - } else { - fail("Could not create entity"); - return; - } - } - - @Test - @Order(19) - void testDeleteSingleAttachment() throws IOException { - System.out.println("Test (19) : Delete single attachment"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - if (response == "Entity in draft mode") { - response = api.deleteAttachment(appUrl, entityName, facetName, entityID, attachmentID1); - if (response == "Deleted") { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (response == "Saved") { - response = api.readAttachment(appUrl, entityName, facetName, entityID, attachmentID1); - if (response.equals("Could not read Attachment")) { - testStatus = true; - } - } - } - } - if (!testStatus) { - fail("Could not read Attachment"); - } - } - - @Test - @Order(20) - void testDeleteMultipleAttachments() throws IOException { - System.out.println("Test (20) : Delete multiple attachments"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - if (response == "Entity in draft mode") { - String response1 = - api.deleteAttachment(appUrl, entityName, facetName, entityID, attachmentID2); - String response2 = - api.deleteAttachment(appUrl, entityName, facetName, entityID, attachmentID3); - if (response1 == "Deleted" && response2 == "Deleted") { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - if (response == "Saved") { - response1 = api.readAttachment(appUrl, entityName, facetName, entityID, attachmentID2); - response2 = api.readAttachment(appUrl, entityName, facetName, entityID, attachmentID3); - if (response1.equals("Could not read Attachment") - && response2.equals("Could not read Attachment")) { - testStatus = true; - } - } - } - } - if (!testStatus) { - fail("Could not delete attachment"); - } - } - - @Test - @Order(21) - void testUploadBlockedMimeType() throws IOException { - System.out.println("Test (21): Upload blocked mimeType .rtf"); - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - entityID2 = response; - - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID2); - postData.put("mimeType", "application/rtf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment(appUrl, entityName, facetName, entityID2, srvpath, postData, file); - String actualResponse = createResponse.get(0); - String expectedJson = - "{\"error\":{\"code\":\"500\",\"message\":\"This file type is not allowed in this repository. Contact your administrator for assistance.\"}}"; - - if (expectedJson.equals(actualResponse)) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - if ("Saved".equals(response)) { - testStatus = true; - } - } else { - api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - } - } - if (!testStatus) { - fail("Attachment got uploaded with blocked .rtf MIME type"); - } - } - - @Test - @Order(22) - void testDeleteEntity() { - System.out.println("Test (22) : Delete entity"); - Boolean testStatus = false; - String response = api.deleteEntity(appUrl, entityName, entityID); - String response2 = api.deleteEntity(appUrl, entityName, entityID2); - if (response == "Entity Deleted" && response2 == "Entity Deleted") { - testStatus = true; - } - if (!testStatus) { - fail("Could not delete entity"); - } - } - - @Test - @Order(23) - void testUpdateValidSecondaryProperty_beforeEntityIsSaved_singleAttachment() throws IOException { - System.out.println("Test (23): Rename & Update secondary property before entity is saved"); - System.out.println("Creating entity"); - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (response != "Could not create entity") { - entityID3 = response; - System.out.println("Entity created"); - System.out.println("Creating attachment"); - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID3); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData, file); - String check = createResponse.get(0); - if (check.equals("Attachment created")) { - attachmentID1 = createResponse.get(1); - System.out.println("Attachment created"); - String name1 = "sample1234.pdf"; - String secondaryPropertyString = "sample12345"; - Integer secondaryPropertyInt = 1234; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - System.out.println("Renaming and updating secondary properties for attachment"); - String response1 = - api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - // Update secondary properties for String - String dropdownValue1 = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - // Update secondary properties for DateTime - RequestBody bodyDateTime = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // Update secondary properties for Boolean - RequestBody bodyBoolean = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - if (response1 == "Renamed" - && updateSecondaryPropertyResponse1 == "Updated" - && updateSecondaryPropertyResponse2 == "Updated" - && updateSecondaryPropertyResponse3 == "Updated" - && updateSecondaryPropertyResponse4 == "Updated") { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response.equals("Saved")) { - System.out.println("Entity saved"); - testStatus = true; - System.out.println("Renamed & updated Secondary properties for attachment"); - } - } - } - } - if (!testStatus) { - fail("Could not update secondary property before entity is saved"); - } - } - - @Test - @Order(24) - void testUpdateValidSecondaryProperty_afterEntityIsSaved_singleAttachment() { - System.out.println("Test (24): Rename & Update secondary property after entity is saved"); - System.out.println("Editing entity"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response == "Entity in draft mode") { - String name1 = "sample.pdf"; - String secondaryPropertyString = "sample"; - Integer secondaryPropertyInt = 12; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - System.out.println("Renaming and updating secondary properties for attachment"); - String response1 = - api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - // Update secondary properties for String - String dropdownValue1 = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - // Update secondary properties for DateTime - RequestBody bodyDateTime = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // Update secondary properties for Boolean - RequestBody bodyBoolean = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - if (response1 == "Renamed" - && updateSecondaryPropertyResponse1 == "Updated" - && updateSecondaryPropertyResponse2 == "Updated" - && updateSecondaryPropertyResponse3 == "Updated" - && updateSecondaryPropertyResponse4 == "Updated") { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response.equals("Saved")) { - System.out.println("Entity saved"); - testStatus = true; - System.out.println("Renamed & updated Secondary properties for attachment"); - } - } - String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - if (deleteEntityResponse != "Entity Deleted") { - fail("Could not delete entity"); - } - } - if (!testStatus) { - fail("Could not update secondary property after entity is saved"); - } - } - - @Test - @Order(25) - void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_singleAttachment() - throws IOException { - System.out.println( - "Test (25): Rename & Update invalid secondary property before entity is saved"); - System.out.println("Creating entity"); - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!"Could not create entity".equals(response)) { - entityID3 = response; - System.out.println("Entity created"); - System.out.println("Creating attachment"); - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID3); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData, file); - String check = createResponse.get(0); - if ("Attachment created".equals(check)) { - attachmentID1 = createResponse.get(1); - System.out.println("Attachment created"); - String name1 = "sample1234.pdf"; - - // Dropdown values for secondaryPropertyString - String[] dropdownValues = {"A", "B", "C"}; - // Select one dropdown value (e.g., "A") - String secondaryPropertyString = dropdownValues[0]; - - Integer secondaryPropertyInt = 1234; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - String invalidProperty = "testid"; - - System.out.println("Renaming and updating invalid secondary properties for attachment"); - String response1 = - api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - - // Update secondary properties for String using dropdown selected value as object with code - - String dropdownValue1 = integrationTestUtils.getDropDownValue(); - String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - RequestBody bodyDropdown1 = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown1); - - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - - // Update secondary properties for DateTime - RequestBody bodyDateTime = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - - // Update secondary properties for Boolean - RequestBody bodyBoolean = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - - // Update invalid secondary property - String updateSecondaryPropertyResponse5 = - api.updateInvalidSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, invalidProperty); - - if ("Renamed".equals(response1) - && "Updated".equals(updateSecondaryPropertyResponse1) - && "Updated".equals(updateSecondaryPropertyResponse2) - && "Updated".equals(updateSecondaryPropertyResponse3) - && "Updated".equals(updateSecondaryPropertyResponse4) - && "Updated".equals(updateSecondaryPropertyResponse5)) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - Map attachmentMetadata = - api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); - assertEquals("sample.pdf", attachmentMetadata.get("fileName")); - assertNull(attachmentMetadata.get("customProperty3")); - assertNull(attachmentMetadata.get("customProperty4")); - assertNull(attachmentMetadata.get("customProperty1_code")); - assertNull(attachmentMetadata.get("customProperty2")); - assertNull(attachmentMetadata.get("customProperty6")); - assertNull(attachmentMetadata.get("customProperty5")); - - String expectedResponse = - "[{\"code\":\"\",\"message\":\"The following secondary properties are not supported.\\n" - + // - "\\n" - + // - "\\t\\u2022 id1\\n" - + // - "\\n" - + // - "Please contact your administrator for assistance with any necessary adjustments.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - if (response.equals(expectedResponse)) { - System.out.println("Entity saved"); - testStatus = true; - System.out.println( - "Rename & update secondary properties for attachment is unsuccessfull"); - } - } - } - } - if (!testStatus) { - fail("Could not update secondary property before entity is saved"); - } - } - - @Test - @Order(26) - void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_singleAttachment() throws IOException { - System.out.println( - "Test (26): Rename & Update invalid secondary property after entity is saved"); - System.out.println("Editing entity"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response == "Entity in draft mode") { - String name1 = "sample.pdf"; - String secondaryPropertyString = "A"; - Integer secondaryPropertyInt = 12; - LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - String invalidProperty = "testidinvalid"; - System.out.println("Renaming and updating invalid secondary properties for attachment"); - String response1 = - api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - // Update secondary properties for DateTime - RequestBody bodyDateTime = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - String updateSecondaryPropertyResponse3 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // Update secondary properties for Boolean - RequestBody bodyBoolean = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse4 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - // Update invalid secondary property - String updateSecondaryPropertyResponse5 = - api.updateInvalidSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, invalidProperty); - if (response1 == "Renamed" - && updateSecondaryPropertyResponse1 == "Updated" - && updateSecondaryPropertyResponse2 == "Updated" - && updateSecondaryPropertyResponse3 == "Updated" - && updateSecondaryPropertyResponse4 == "Updated" - && updateSecondaryPropertyResponse5 == "Updated") { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - Map attachmentMetadata = - api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); - assertEquals("sample.pdf", attachmentMetadata.get("fileName")); - assertNull(attachmentMetadata.get("customProperty3")); - assertNull(attachmentMetadata.get("customProperty4")); - assertNull(attachmentMetadata.get("customProperty1_code")); - assertNull(attachmentMetadata.get("customProperty2")); - assertNull(attachmentMetadata.get("customProperty6")); - assertNull(attachmentMetadata.get("customProperty5")); - - String expectedResponse = - "[{\"code\":\"\",\"message\":\"The following secondary properties are not supported.\\n" - + // - "\\n" - + // - "\\t\\u2022 id1\\n" - + // - "\\n" - + // - "Please contact your administrator for assistance with any necessary adjustments.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - if (response.equals(expectedResponse)) { - System.out.println("Entity saved"); - testStatus = true; - System.out.println( - "Rename & update secondary properties for attachment is unsuccessfull"); - } - } - String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - if (deleteEntityResponse != "Entity Deleted") { - fail("Could not delete entity"); - } - } - if (!testStatus) { - fail("Could not update secondary property before entity is saved"); - } - } - - @Test - @Order(27) - void testUpdateValidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() - throws IOException { - System.out.println( - "Test (27): Rename & Update valid secondary properties for multiple attachments before entity is saved"); - System.out.println("Creating entity"); - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (response != "Could not create entity") { - entityID3 = response; - - System.out.println("Entity created"); - - System.out.println("Creating attachment PDF"); - ClassLoader classLoader = getClass().getClassLoader(); - - File file = new File(classLoader.getResource("sample.pdf").getFile()); - Map postData1 = new HashMap<>(); - postData1.put("up__ID", entityID3); - postData1.put("mimeType", "application/pdf"); - postData1.put("createdAt", new Date().toString()); - postData1.put("createdBy", "test@test.com"); - postData1.put("modifiedBy", "test@test.com"); - - List createResponse1 = - api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData1, file); - if (createResponse1.get(0).equals("Attachment created")) { - attachmentID1 = createResponse1.get(1); - System.out.println("Attachment created"); - } - - System.out.println("Creating attachment TXT"); - file = new File(classLoader.getResource("sample.txt").getFile()); - Map postData2 = new HashMap<>(); - postData2.put("up__ID", entityID3); - postData2.put("mimeType", "application/txt"); - postData2.put("createdAt", new Date().toString()); - postData2.put("createdBy", "test@test.com"); - postData2.put("modifiedBy", "test@test.com"); - - List createResponse2 = - api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData2, file); - if (createResponse2.get(0).equals("Attachment created")) { - attachmentID2 = createResponse2.get(1); - System.out.println("Attachment created"); - } - - System.out.println("Creating attachment EXE"); - file = new File(classLoader.getResource("sample.exe").getFile()); - Map postData3 = new HashMap<>(); - postData3.put("up__ID", entityID3); - postData3.put("mimeType", "application/exe"); - postData3.put("createdAt", new Date().toString()); - postData3.put("createdBy", "test@test.com"); - postData3.put("modifiedBy", "test@test.com"); - - List createResponse3 = - api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData3, file); - if (createResponse3.get(0).equals("Attachment created")) { - attachmentID3 = createResponse3.get(1); - System.out.println("Attachment created"); - } - - String check1 = createResponse1.get(0); - String check2 = createResponse2.get(0); - String check3 = createResponse3.get(0); - if (check1.equals("Attachment created") - && check2.equals("Attachment created") - && check3.equals("Attachment created")) { - Boolean attachment1Updated = false; - Boolean attachment2Updated = false; - Boolean attachment3Updated = false; - - String name1 = "sample1234.pdf"; - Integer secondaryPropertyInt1 = 1234; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - System.out.println("Renaming and updating secondary properties for attachment PDF"); - String responsePDF1 = - api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - // Update secondary properties for String - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponsePDF1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponsePDF2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - // Update secondary properties for DateTime - RequestBody bodyDateTime = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponsePDF3 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // Update secondary properties for Boolean - RequestBody bodyBoolean = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponsePDF4 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - if (responsePDF1 == "Renamed" - && updateSecondaryPropertyResponsePDF1 == "Updated" - && updateSecondaryPropertyResponsePDF2 == "Updated" - && updateSecondaryPropertyResponsePDF3 == "Updated" - && updateSecondaryPropertyResponsePDF4 == "Updated") { - System.out.println("Renamed & updated Secondary properties for attachment PDF"); - attachment1Updated = true; - } - - System.out.println("Updating secondary properties for attachment TXT"); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponseTXT1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); - if (updateSecondaryPropertyResponseTXT1 == "Updated") { - System.out.println("Updated Secondary properties for attachment TXT"); - attachment2Updated = true; - } - Integer secondaryPropertyInt3 = 1234; - LocalDateTime secondaryPropertyDateTime3 = LocalDateTime.now(); - System.out.println("Updating secondary properties for attachment EXE"); - // Update secondary properties for String - String dropdownValue1 = integrationTestUtils.getDropDownValue(); - String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - RequestBody bodyDropdown1 = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - String updateSecondaryPropertyResponseEXE1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown1); - // Update secondary properties for Integer - RequestBody bodyInt3 = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - String updateSecondaryPropertyResponseEXE2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); - // Update secondary properties for DateTime - RequestBody bodyDateTime3 = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime3 + "\"\n}")); - String updateSecondaryPropertyResponseEXE3 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID3, bodyDateTime3); - - if (updateSecondaryPropertyResponseEXE1 == "Updated" - && updateSecondaryPropertyResponseEXE2 == "Updated" - && updateSecondaryPropertyResponseEXE3 == "Updated") { - System.out.println("Updated Secondary properties for attachment EXE"); - attachment3Updated = true; - } - - if (attachment1Updated && attachment2Updated && attachment3Updated) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response.equals("Saved")) { - System.out.println("Entity saved"); - testStatus = true; - System.out.println("Renamed & updated Secondary properties for attachments"); - } - } - } - } - if (!testStatus) { - fail("Could not update secondary property before entity is saved"); - } - } - - @Test - @Order(28) - void testUpdateValidSecondaryProperty_afterEntityIsSaved_multipleAttachments() { - System.out.println( - "Test (28): Rename & Update valid secondary properties for multiple attachments after entity is saved"); - System.out.println("Editing entity"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response == "Entity in draft mode") { - Boolean attachment1Updated = false; - Boolean attachment2Updated = false; - Boolean attachment3Updated = false; - - String name1 = "sample1.pdf"; - Integer secondaryPropertyInt1 = 12; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - System.out.println("Renaming and updating secondary properties for attachment PDF"); - String responsePDF1 = - api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - // Update secondary properties for String - String dropdownValue1 = integrationTestUtils.getDropDownValue(); - String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - RequestBody bodyDropdown1 = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - String updateSecondaryPropertyResponsePDF1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown1); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponsePDF2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - // Update secondary properties for DateTime - RequestBody bodyDateTime = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponsePDF3 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // Update secondary properties for Boolean - RequestBody bodyBoolean = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponsePDF4 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - - if (responsePDF1 == "Renamed" - && updateSecondaryPropertyResponsePDF1 == "Updated" - && updateSecondaryPropertyResponsePDF2 == "Updated" - && updateSecondaryPropertyResponsePDF3 == "Updated" - && updateSecondaryPropertyResponsePDF4 == "Updated") { - System.out.println("Renamed & updated Secondary properties for attachment PDF"); - attachment1Updated = true; - } - - System.out.println("Updating secondary properties for attachment TXT"); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponseTXT1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); - if (updateSecondaryPropertyResponseTXT1 == "Updated") { - System.out.println("Updated Secondary properties for attachment TXT"); - attachment2Updated = true; - } - - Integer secondaryPropertyInt3 = 123; - LocalDateTime secondaryPropertyDateTime3 = LocalDateTime.now(); - System.out.println("Updating secondary properties for attachment EXE"); - // Update secondary properties for String - String dropdownValue2 = integrationTestUtils.getDropDownValue(); - String jsonDropdown2 = "{ \"customProperty1_code\" : \"" + dropdownValue2 + "\" }"; - RequestBody bodyDropdown2 = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown2); - String updateSecondaryPropertyResponseEXE1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown2); - // Update secondary properties for Integer - RequestBody bodyInt3 = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - String updateSecondaryPropertyResponseEXE2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); - // Update secondary properties for DateTime - RequestBody bodyDateTime3 = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime3 + "\"\n}")); - String updateSecondaryPropertyResponseEXE3 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID3, bodyDateTime3); - - if (updateSecondaryPropertyResponseEXE1 == "Updated" - && updateSecondaryPropertyResponseEXE2 == "Updated" - && updateSecondaryPropertyResponseEXE3 == "Updated") { - System.out.println("Updated Secondary properties for attachment EXE"); - attachment3Updated = true; - } - - if (attachment1Updated && attachment2Updated && attachment3Updated) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response.equals("Saved")) { - System.out.println("Entity saved"); - testStatus = true; - System.out.println("Renamed & updated Secondary properties for attachments"); - } - } - String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - if (deleteEntityResponse != "Entity Deleted") { - fail("Could not delete entity"); - } - } - if (!testStatus) { - fail("Could not update secondary property after entity is saved"); - } - } - - @Test - @Order(29) - void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() - throws IOException { - System.out.println( - "Test (29): Rename & Update invalid and valid secondary properties for multiple attachments before entity is saved"); - System.out.println("Creating entity"); - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (response != "Could not create entity") { - entityID3 = response; - - System.out.println("Entity created"); - - System.out.println("Creating attachment PDF"); - ClassLoader classLoader = getClass().getClassLoader(); - - File file = new File(classLoader.getResource("sample.pdf").getFile()); - Map postData1 = new HashMap<>(); - postData1.put("up__ID", entityID3); - postData1.put("mimeType", "application/pdf"); - postData1.put("createdAt", new Date().toString()); - postData1.put("createdBy", "test@test.com"); - postData1.put("modifiedBy", "test@test.com"); - - List createResponse1 = - api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData1, file); - if (createResponse1.get(0).equals("Attachment created")) { - attachmentID1 = createResponse1.get(1); - System.out.println("Attachment created"); - } - - System.out.println("Creating attachment TXT"); - file = new File(classLoader.getResource("sample.txt").getFile()); - Map postData2 = new HashMap<>(); - postData2.put("up__ID", entityID3); - postData2.put("mimeType", "application/txt"); - postData2.put("createdAt", new Date().toString()); - postData2.put("createdBy", "test@test.com"); - postData2.put("modifiedBy", "test@test.com"); - - List createResponse2 = - api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData2, file); - if (createResponse2.get(0).equals("Attachment created")) { - attachmentID2 = createResponse2.get(1); - System.out.println("Attachment created"); - } - - System.out.println("Creating attachment EXE"); - file = new File(classLoader.getResource("sample.exe").getFile()); - Map postData3 = new HashMap<>(); - postData3.put("up__ID", entityID3); - postData3.put("mimeType", "application/exe"); - postData3.put("createdAt", new Date().toString()); - postData3.put("createdBy", "test@test.com"); - postData3.put("modifiedBy", "test@test.com"); - - List createResponse3 = - api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData3, file); - if (createResponse3.get(0).equals("Attachment created")) { - attachmentID3 = createResponse3.get(1); - System.out.println("Attachment created"); - } - - String check1 = createResponse1.get(0); - String check2 = createResponse2.get(0); - String check3 = createResponse3.get(0); - if (check1.equals("Attachment created") - && check2.equals("Attachment created") - && check3.equals("Attachment created")) { - Boolean attachment1Updated = false; - Boolean attachment2Updated = false; - Boolean attachment3Updated = false; - - String name1 = "sample1234.pdf"; - Integer secondaryPropertyInt1 = 1234; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - String invalidPropertyPDF = "testidinvalidPDF"; - System.out.println("Renaming and updating invalid secondary properties for attachment PDF"); - String responsePDF1 = - api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - // Update secondary properties for String - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponsePDF1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyint = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponsePDF2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyint); - // Update secondary properties for DateTime - RequestBody bodyDateTime = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponsePDF3 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // Update secondary properties for Boolean - RequestBody bodyBoolean = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponsePDF4 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - // Update invalid secondary property - String updateSecondaryPropertyResponsePDF5 = - api.updateInvalidSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, invalidPropertyPDF); - if (responsePDF1 == "Renamed" - && updateSecondaryPropertyResponsePDF1 == "Updated" - && updateSecondaryPropertyResponsePDF2 == "Updated" - && updateSecondaryPropertyResponsePDF3 == "Updated" - && updateSecondaryPropertyResponsePDF4 == "Updated" - && updateSecondaryPropertyResponsePDF5 == "Updated") { - attachment1Updated = true; - } - - System.out.println("Updating valid secondary properties for attachment TXT"); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponseTXT1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); - if (updateSecondaryPropertyResponseTXT1 == "Updated") { - System.out.println("Updated Secondary properties for attachment TXT"); - attachment2Updated = true; - } - - Integer secondaryPropertyInt3 = 1234; - System.out.println("Updating valid secondary properties for attachment EXE"); - - // Update secondary properties for String - RequestBody bodyDropdown1 = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponseEXE1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown1); - // Update secondary properties for Integer - RequestBody bodyInt3 = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - String updateSecondaryPropertyResponseEXE2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); - - if (updateSecondaryPropertyResponseEXE1 == "Updated" - && updateSecondaryPropertyResponseEXE2 == "Updated") { - System.out.println("Updated Secondary properties for attachment EXE"); - attachment3Updated = true; - } - - if (attachment1Updated && attachment2Updated && attachment3Updated) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - Map attachmentMetadataPDF = - api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); - assertEquals("sample.pdf", attachmentMetadataPDF.get("fileName")); - assertNull(attachmentMetadataPDF.get("customProperty3")); - assertNull(attachmentMetadataPDF.get("customProperty4")); - assertNull(attachmentMetadataPDF.get("customProperty1_code")); - assertNull(attachmentMetadataPDF.get("customProperty2")); - assertNull(attachmentMetadataPDF.get("customProperty6")); - assertNull(attachmentMetadataPDF.get("customProperty5")); - - Map attachmentMetadataTXT = - api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID2); - assertEquals("sample.txt", attachmentMetadataTXT.get("fileName")); - assertNull(attachmentMetadataTXT.get("customProperty3")); - assertNull(attachmentMetadataTXT.get("customProperty4")); - assertNull(attachmentMetadataTXT.get("customProperty1_code")); - assertNull(attachmentMetadataTXT.get("customProperty2")); - assertTrue((Boolean) attachmentMetadataTXT.get("customProperty6")); - assertNull(attachmentMetadataTXT.get("customProperty5")); - - Map attachmentMetadataEXE = - api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID3); - assertEquals("sample.exe", attachmentMetadataEXE.get("fileName")); - assertNull(attachmentMetadataEXE.get("customProperty3")); - assertNull(attachmentMetadataEXE.get("customProperty4")); - assertEquals(dropdownValue, attachmentMetadataEXE.get("customProperty1_code")); - assertEquals(1234, attachmentMetadataEXE.get("customProperty2")); - - String expectedResponse = - "[{\"code\":\"\",\"message\":\"The following secondary properties are not supported.\\n" - + // - "\\n" - + // - "\\t\\u2022 id1\\n" - + // - "\\n" - + // - "Please contact your administrator for assistance with any necessary adjustments.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - if (response.equals(expectedResponse)) { - System.out.println("Entity saved"); - testStatus = true; - System.out.println( - "Rename & update unsuccessfull for invalid Secondary properties and successfull for valid property attachments"); - } - } - } - } - if (!testStatus) { - fail("Could not update secondary property before entity is saved"); - } - } - - @Test - @Order(30) - void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_multipleAttachments() - throws IOException { - System.out.println( - "Test (30): Rename & Update invalid and valid secondary properties for multiple attachments after entity is saved"); - System.out.println("Editing entity"); - Boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - if (response == "Entity in draft mode") { - Boolean attachment1Updated = false; - Boolean attachment2Updated = false; - Boolean attachment3Updated = false; - - String name1 = "sample.pdf"; - Integer secondaryPropertyInt1 = 12; - LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - String invalidPropertyPDF = "testidinvalidPDF"; - System.out.println("Renaming and updating invalid secondary properties for attachment PDF"); - String responsePDF1 = - api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - // Update secondary properties for String - String dropdownValue = integrationTestUtils.getDropDownValue(); - String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - RequestBody bodyDropdown = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponsePDF1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); - // Update secondary properties for Integer - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - String updateSecondaryPropertyResponsePDF2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - // Update secondary properties for DateTime - RequestBody bodyDateTime = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - String updateSecondaryPropertyResponsePDF3 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // Update secondary properties for Boolean - RequestBody bodyBoolean = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponsePDF4 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - // Update invalid secondary property - String updateSecondaryPropertyResponsePDF5 = - api.updateInvalidSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID1, invalidPropertyPDF); - if (responsePDF1 == "Renamed" - && updateSecondaryPropertyResponsePDF1 == "Updated" - && updateSecondaryPropertyResponsePDF2 == "Updated" - && updateSecondaryPropertyResponsePDF3 == "Updated" - && updateSecondaryPropertyResponsePDF4 == "Updated" - && updateSecondaryPropertyResponsePDF5 == "Updated") { - attachment1Updated = true; - } - - System.out.println("Updating valid secondary properties for attachment TXT"); - // Update secondary properties for Boolean - RequestBody bodyBool = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + false + "\n}")); - String updateSecondaryPropertyResponseTXT1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); - if (updateSecondaryPropertyResponseTXT1 == "Updated") { - System.out.println("Updated Secondary properties for attachment TXT"); - attachment2Updated = true; - } - - Integer secondaryPropertyInt3 = 12; - System.out.println("Updating valid secondary properties for attachment EXE"); - - // Update secondary properties for String - RequestBody bodyDropdown1 = - RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - String updateSecondaryPropertyResponseEXE1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown1); - // Update secondary properties for Integer - RequestBody bodyInt3 = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8( - "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - String updateSecondaryPropertyResponseEXE2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); - - if (updateSecondaryPropertyResponseEXE1 == "Updated" - && updateSecondaryPropertyResponseEXE2 == "Updated") { - System.out.println("Updated Secondary properties for attachment EXE"); - attachment3Updated = true; - } - - if (attachment1Updated && attachment2Updated && attachment3Updated) { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - Map attachmentMetadataPDF = - api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); - assertEquals("sample.pdf", attachmentMetadataPDF.get("fileName")); - assertNull(attachmentMetadataPDF.get("customProperty3")); - assertNull(attachmentMetadataPDF.get("customProperty4")); - assertNull(attachmentMetadataPDF.get("customProperty1_code")); - assertNull(attachmentMetadataPDF.get("customProperty2")); - assertNull(attachmentMetadataPDF.get("customProperty6")); - assertNull(attachmentMetadataPDF.get("customProperty5")); - - Map attachmentMetadataTXT = - api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID2); - assertEquals("sample.txt", attachmentMetadataTXT.get("fileName")); - assertNull(attachmentMetadataTXT.get("customProperty3")); - assertNull(attachmentMetadataTXT.get("customProperty4")); - assertNull(attachmentMetadataTXT.get("customProperty1_code")); - assertNull(attachmentMetadataTXT.get("customProperty2")); - assertFalse((Boolean) attachmentMetadataTXT.get("customProperty6")); - assertNull(attachmentMetadataTXT.get("customProperty5")); - - Map attachmentMetadataEXE = - api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID3); - assertEquals("sample.exe", attachmentMetadataEXE.get("fileName")); - assertNull(attachmentMetadataEXE.get("customProperty3")); - assertNull(attachmentMetadataEXE.get("customProperty4")); - assertEquals(dropdownValue, attachmentMetadataEXE.get("customProperty1_code")); - assertEquals(12, attachmentMetadataEXE.get("customProperty2")); - - String expectedResponse = - "[{\"code\":\"\",\"message\":\"The following secondary properties are not supported.\\n" - + // - "\\n" - + // - "\\t\\u2022 id1\\n" - + // - "\\n" - + // - "Please contact your administrator for assistance with any necessary adjustments.\\n" - + // - "\\n" - + // - "Table: attachments\\n" - + // - "Page: IntegrationTestEntity\",\"numericSeverity\":3}]"; - if (response.equals(expectedResponse)) { - System.out.println("Entity saved"); - testStatus = true; - System.out.println( - "Rename & update unsuccessfull for invalid Secondary properties and successfull for valid property attachments"); - } - String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - if (deleteEntityResponse != "Entity Deleted") { - fail("Could not delete entity"); - } - } - } - if (!testStatus) { - fail("Could not update secondary property before entity is saved"); - } - } - - @Test - @Order(31) - void testNAttachments_NewEntity() throws IOException { - System.out.println( - "Test (31): Creating new entity and checking only max 4 attachments are allowed to be uploaded"); - System.out.println("Creating entity"); - Boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (response != "Could not create entity") { - entityID4 = response; - - System.out.println("Entity created"); - - System.out.println("Creating attachment PDF"); - ClassLoader classLoader = getClass().getClassLoader(); - - File file = new File(classLoader.getResource("sample.pdf").getFile()); - Map postData1 = new HashMap<>(); - postData1.put("up__ID", entityID4); - postData1.put("mimeType", "application/pdf"); - postData1.put("createdAt", new Date().toString()); - postData1.put("createdBy", "test@test.com"); - postData1.put("modifiedBy", "test@test.com"); - - List createResponse1 = - api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData1, file); - if (createResponse1.get(0).equals("Attachment created")) { - attachmentID1 = createResponse1.get(1); - System.out.println("Attachment created"); - } - - System.out.println("Creating attachment TXT"); - file = new File(classLoader.getResource("sample.txt").getFile()); - Map postData2 = new HashMap<>(); - postData2.put("up__ID", entityID4); - postData2.put("mimeType", "application/txt"); - postData2.put("createdAt", new Date().toString()); - postData2.put("createdBy", "test@test.com"); - postData2.put("modifiedBy", "test@test.com"); - - List createResponse2 = - api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData2, file); - if (createResponse2.get(0).equals("Attachment created")) { - attachmentID2 = createResponse2.get(1); - System.out.println("Attachment created"); - } - - System.out.println("Creating attachment EXE"); - file = new File(classLoader.getResource("sample.exe").getFile()); - Map postData3 = new HashMap<>(); - postData3.put("up__ID", entityID4); - postData3.put("mimeType", "application/exe"); - postData3.put("createdAt", new Date().toString()); - postData3.put("createdBy", "test@test.com"); - postData3.put("modifiedBy", "test@test.com"); - - List createResponse3 = - api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData3, file); - if (createResponse3.get(0).equals("Attachment created")) { - attachmentID3 = createResponse3.get(1); - System.out.println("Attachment created"); - } - - System.out.println("Creating second attachment pdf"); - file = new File(classLoader.getResource("sample1.pdf").getFile()); - Map postData4 = new HashMap<>(); - postData4.put("up__ID", entityID4); - postData4.put("mimeType", "application/pdf"); - postData4.put("createdAt", new Date().toString()); - postData4.put("createdBy", "test@test.com"); - postData4.put("modifiedBy", "test@test.com"); - - List createResponse4 = - api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData3, file); - if (createResponse4.get(0).equals("Attachment created")) { - attachmentID4 = createResponse4.get(1); - System.out.println("Attachment created"); - } - - System.out.println("Creating third attachment pdf"); - file = new File(classLoader.getResource("sample2.pdf").getFile()); - Map postData5 = new HashMap<>(); - postData5.put("up__ID", entityID4); - postData5.put("mimeType", "application/pdf"); - postData5.put("createdAt", new Date().toString()); - postData5.put("createdBy", "test@test.com"); - postData5.put("modifiedBy", "test@test.com"); - - List createResponse5 = - api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData3, file); - if (createResponse5.get(0).equals("Only 4 attachments allowed.")) { - testStatus = true; - attachmentID5 = createResponse5.get(1); - System.out.println("Expected error received: Only 4 attachments allowed."); - } - String check = createResponse5.get(0); - if (check.equals("Attachment created")) { - testStatus = false; - } else { - response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID4); - if (response.equals("Saved")) { - String expectedJson = - "{\"error\":{\"code\":\"500\",\"message\":\"Cannot upload more than 4 attachments.\"}}"; - ObjectMapper objectMapper = new ObjectMapper(); - JsonNode actualJsonNode = objectMapper.readTree(check); - JsonNode expectedJsonNode = objectMapper.readTree(expectedJson); - if (expectedJsonNode.equals(actualJsonNode)) { - testStatus = true; - } - } - } - } - if (!testStatus) { - fail("Attachment was created"); - } - } - - @Test - @Order(32) - void testUploadNAttachments() throws IOException { - System.out.println("Test (32): Upload maximum 4 attachments in an exsisting entity"); - - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.exe").getFile()); - - boolean testStatus = false; - String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID4); - System.out.println("response: " + response); - - if ("Entity in draft mode".equals(response)) { - for (int i = 1; i <= 5; i++) { - // Ensure only one file is uploaded at a time and complete before next - File tempFile = File.createTempFile("sample_" + i + "_", ".exe"); - Files.copy(originalFile.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - - Map postData = new HashMap<>(); - postData.put("up__ID", entityID4); - postData.put("mimeType", "application/exe"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, entityID4, srvpath, postData, tempFile); - - String resultMessage = createResponse.get(0); - System.out.println("Result message for attachment " + i + ": " + resultMessage); - - String expectedResponse = - "{\"error\":{\"code\":\"500\",\"message\":\"Cannot upload more than 4 attachments.\"}}"; - if (resultMessage.equals(expectedResponse)) { - ObjectMapper objectMapper = new ObjectMapper(); - JsonNode actualJsonNode = objectMapper.readTree(resultMessage); - JsonNode expectedJsonNode = objectMapper.readTree(expectedResponse); - if (expectedJsonNode.equals(actualJsonNode)) { - testStatus = true; - } - } else { - testStatus = false; - } - tempFile.delete(); - } - if (!testStatus) { - fail("5th attachment did not trigger the expected error."); - } - // Delete the newly created entity - String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID4); - if (deleteEntityResponse != "Entity Deleted") { - fail("Could not delete entity"); - } else { - System.out.println("Successfully deleted the test entity4"); - } - } - } - - @Test - @Order(33) - void testDiscardDraftWithoutAttachments() { - System.out.println("Test (33) : Discard draft without adding attachments"); - - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - - if (response.equals("Could not create entity")) { - fail("Could not create entity"); - } - - response = api.deleteEntityDraft(appUrl, entityName, response); - if (!response.equals("Entity Draft Deleted")) { - fail("Draft was not discarded properly"); - } - } - - @Test - @Order(34) - void testDiscardDraftWithAttachments() throws IOException { - System.out.println("Test (34) : Discard draft with attachments"); - boolean testStatus = false; - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!response.equals("Could not create entity")) { - entityID7 = response; - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - - Map postData1 = new HashMap<>(); - postData1.put("up__ID", entityID7); - postData1.put("mimeType", "application/pdf"); - postData1.put("createdAt", new Date().toString()); - postData1.put("createdBy", "test@test.com"); - postData1.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment(appUrl, entityName, facetName, entityID7, srvpath, postData1, file); - if (createResponse.get(0).equals("Attachment created")) { - attachmentID1 = createResponse.get(1); - } - String check = createResponse.get(0); - if (check.equals("Attachment created")) { - response = api.deleteEntityDraft(appUrl, entityName, entityID7); - } - if (response.equals("Entity Draft Deleted")) { - testStatus = true; - } - } - if (!testStatus) { - fail("Draft was not discarded properly"); - } - } - - @Test - @Order(35) - void testCopyAttachmentsSuccessNewEntity() throws IOException { - System.out.println("Test (35): Copy attachments from one entity to another new entity"); - List attachments = new ArrayList<>(); - copyAttachmentSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyAttachmentTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!copyAttachmentSourceEntity.equals("Could not create entity") - && !copyAttachmentTargetEntity.equals("Could not create entity")) { - ClassLoader classLoader = getClass().getClassLoader(); - List files = new ArrayList<>(); - files.add(new File(classLoader.getResource("sample.pdf").getFile())); - files.add(new File(classLoader.getResource("sample1.pdf").getFile())); - Map postData = new HashMap<>(); - postData.put("up__ID", entityID7); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, copyAttachmentSourceEntity, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - attachments.add(createResponse.get(1)); - } else { - fail("Could not create attachment"); - } - } - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - List> attachmentsMetadata = new ArrayList<>(); - Map fetchAttachmentMetadataResponse; - for (String attachment : attachments) { - try { - fetchAttachmentMetadataResponse = - api.fetchMetadata( - appUrl, entityName, facetName, copyAttachmentSourceEntity, attachment); - attachmentsMetadata.add(fetchAttachmentMetadataResponse); - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } - for (Map metadata : attachmentsMetadata) { - if (metadata.containsKey("objectId")) { - sourceObjectIds.add(metadata.get("objectId").toString()); - } else { - fail("Attachment metadata does not contain objectId"); - } - } - - if (sourceObjectIds.size() == 2) { - String copyResponse; - copyResponse = - api.copyAttachment( - appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); - if (copyResponse.equals("Attachments copied successfully")) { - String saveEntityResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - if (saveEntityResponse.equals("Saved")) { - List> fetchEntityMetadataResponse; - fetchEntityMetadataResponse = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); - targetAttachmentIds = - fetchEntityMetadataResponse.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - String readResponse; - for (String targetAttachmentId : targetAttachmentIds) { - readResponse = - api.readAttachment( - appUrl, - entityName, - facetName, - copyAttachmentTargetEntity, - targetAttachmentId); - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment"); - } - } - } else { - fail("Could not save entity after copying attachments: " + saveEntityResponse); - } - } else { - fail("Could not copy attachments: " + copyResponse); - } - } else { - fail("Could not fetch objects Ids for all attachments"); - } - } else { - fail("Could not create entities"); - } - } - - @Test - @Order(36) - void testCopyAttachmentsUnsuccessfulNewEntity() throws IOException { - System.out.println("Test (36): Copy attachments from one entity to another new entity"); - String editResponse1 = - api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - copyAttachmentTargetEntityEmpty = - api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (editResponse1.equals("Entity in draft mode") - && !copyAttachmentTargetEntityEmpty.equals("Could not create entity")) { - sourceObjectIds.add("incorrectObjectId"); - if (sourceObjectIds.size() == 3) { - try { - api.copyAttachment( - appUrl, entityName, facetName, copyAttachmentTargetEntityEmpty, sourceObjectIds); - fail("Copy attachments did not throw an error"); - } catch (IOException e) { - String saveEntityResponse1 = - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - String saveEntityResponse2 = - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntityEmpty); - String deleteResponse = - api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntityEmpty); - if (!saveEntityResponse1.equals("Saved") - || !saveEntityResponse2.equals("Saved") - || !deleteResponse.equals("Entity Deleted")) { - fail("Could not save entities"); - } - } - } else { - fail("Could not fetch objects Ids for all attachments"); - } - } else { - fail("Could not edit entities"); - } - } - - @Test - @Order(37) - void testCopyAttachmentWithNotesField() throws IOException { - System.out.println( - "Test (37): Create entity with attachment containing notes, copy to new entity and verify notes field"); - Boolean testStatus = false; - // Create source entity - copyCustomSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyCustomSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } - - // Create and upload attachment to source entity - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - Map postData = new HashMap<>(); - postData.put("up__ID", copyCustomSourceEntity); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } - - String sourceAttachmentId = createResponse.get(1); - - // Update attachment with notes field - String notesValue = "This is a test note for copy attachment verification"; - MediaType mediaType = MediaType.parse("application/json"); - String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - RequestBody updateBody = RequestBody.create(jsonPayload, mediaType); - - String updateResponse = - api.updateSecondaryProperty( - appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, updateBody); - - if (!updateResponse.equals("Updated")) { - fail("Could not update attachment notes field"); - } - - // Save source entity - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity"); - } - - // Fetch attachment metadata to get objectId - Map sourceAttachmentMetadata = - api.fetchMetadata( - appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + // @Test + // @Order(10) + // void testDraftUpdateWithFileUploadDeleteAndCreate() throws IOException { + // System.out.println("Test (10): Upload attachments, delete one and create entity"); - if (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId"); - } + // boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (response != "Could not create entity") { + + // entityID5 = response; + // ClassLoader classLoader = getClass().getClassLoader(); + + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // Map postData1 = new HashMap<>(); + // postData1.put("up__ID", entityID5); + // postData1.put("mimeType", "application/pdf"); + // postData1.put("createdAt", new Date().toString()); + // postData1.put("createdBy", "test@test.com"); + // postData1.put("modifiedBy", "test@test.com"); + + // List createResponse1 = + // api.createAttachment(appUrl, entityName, facetName, entityID5, srvpath, postData1, + // file); + // if (createResponse1.get(0).equals("Attachment created")) { + // attachmentID7 = createResponse1.get(1); + // } - // Store objectId in array - String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - if (sourceObjectIds.isEmpty()) { - sourceObjectIds.add(sourceObjectId); - } else { - sourceObjectIds.set(0, sourceObjectId); - } + // file = new File(classLoader.getResource("sample.txt").getFile()); + // Map postData2 = new HashMap<>(); + // postData2.put("up__ID", entityID5); + // postData2.put("mimeType", "application/txt"); + // postData2.put("createdAt", new Date().toString()); + // postData2.put("createdBy", "test@test.com"); + // postData2.put("modifiedBy", "test@test.com"); + + // List createResponse2 = + // api.createAttachment(appUrl, entityName, facetName, entityID5, srvpath, postData2, + // file); + // if (createResponse2.get(0).equals("Attachment created")) { + // attachmentID8 = createResponse2.get(1); + // } + // response = api.deleteAttachment(appUrl, entityName, facetName, entityID5, attachmentID8); + // if (response.equals("Deleted")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); - String sourceNoteValue = - sourceAttachmentMetadata.get("note") != null - ? sourceAttachmentMetadata.get("note").toString() - : null; - - if (!notesValue.equals(sourceNoteValue)) { - fail( - "Notes field was not properly set in source attachment. Expected: " - + notesValue - + ", Got: " - + sourceNoteValue); - } + // if (response.equals("Saved")) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("Failed to create entity after deleting one attachment"); + // } + // } - // Create target entity - copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyCustomTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // @Test + // @Order(11) + // void testUpdateEntityDraft() throws IOException { + // System.out.println("Test (11): Update entity in draft"); + // boolean testStatus = false; + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new + // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - // Copy attachment to target entity - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(0)); // Use objectId from array + // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); + // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID5); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity: " + copyResponse); - } + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID5); + // if (response.equals("Entity in draft mode")) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, entityID5, srvpath, postData, tempFile); + // String check = createResponse.get(0); + // if (check.equals("Attachment created")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); + // if (response.equals("Saved")) { + // testStatus = true; + // } + // } + // } + // if (!testStatus) { + // fail("update entity draft with uploading attachment failed"); + // } + // api.deleteEntity(appUrl, entityName, entityID5); + // } - // Save target entity - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity"); - } + // @Test + // @Order(12) + // void testRenameSingleAttachment() { + // System.out.println("Test (12) : Rename single attachment"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // String name = "sample123"; + // if (response == "Entity in draft mode") { + // response = api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, + // name); + // if (response.equals("Renamed")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Saved")) { + // testStatus = true; + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } + // if (!testStatus) { + // fail("Attachment was not renamed"); + // } + // } - // Fetch target entity attachments metadata - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // @Test + // @Order(13) + // void testRenameAttachmentWithUnsupportedCharacter() { + // System.out.println("Test (13) : Rename single attachment with unsupported characters"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // String name = "invalid/name"; + // if (response == "Entity in draft mode") { + // response = api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, + // name); + // if (response.equals("Renamed")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"invalid/name\\\" contains unsupported + // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\"}}"; + // if (response.equals(expected)) { + // api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, + // "sample123"); + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if ("Saved".equals(response)) testStatus = true; + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } + // if (!testStatus) { + // fail("Attachment was renamed with unsupported characters"); + // } + // } - if (targetAttachmentsMetadata.isEmpty()) { - fail("No attachments found in target entity"); - } + // @Test + // @Order(14) + // void testRenameMultipleAttachments() { + // System.out.println("Test (14) : Rename multiple attachments"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // String name1 = "sample1234"; + // String name2 = "sample12345"; + // if (response == "Entity in draft mode") { + // String response1 = + // api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID2, name1); + // String response2 = + // api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID3, name2); + // if (response1.equals("Renamed") && response2.equals("Renamed")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Saved")) { + // testStatus = true; + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } + // if (!testStatus) { + // fail("Attachment was not renamed"); + // } + // } - // Verify the copied attachment has the same notes value - Map copiedAttachmentMetadata = targetAttachmentsMetadata.get(0); - String copiedNoteValue = - copiedAttachmentMetadata.get("note") != null - ? copiedAttachmentMetadata.get("note").toString() - : null; - - if (!notesValue.equals(copiedNoteValue)) { - fail( - "Notes field was not properly copied. Expected: " - + notesValue - + ", Got: " - + copiedNoteValue); - } + // @Test + // @Order(15) + // void testRenameSingleAttachmentDuplicate() { + // System.out.println("Test (15) : Rename single attachment duplicate"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // String name = "sample123"; + // String name2 = "sample123456"; + // if (response == "Entity in draft mode") { + // response = api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID3, + // name); + // if (response.equals("Renamed")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"sample123\\\" already + // exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\"}}"; + // if (response.equals(expected)) { + // response = + // api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID3, + // name2); + // if (response.equals("Renamed")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response.equals("Saved")) { + // testStatus = true; + // } + // } + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } + // if (!testStatus) { + // fail("Attachment was renamed"); + // } + // } - // Verify attachment content can be read from target entity - String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - String readResponse = - api.readAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + // @Test + // @Order(16) + // void testRenameMultipleAttachmentsWithOneUnsupportedCharacter() { + // System.out.println( + // "Test (16) : Rename multiple attachments where one name has unsupported characters"); + // Boolean testStatus = false; + + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + + // if (response.equals("Entity in draft mode")) { + // String validName1 = "valid_attachment1.pdf"; + // String invalidName2 = "invalid/attachment2.pdf"; + + // String renameResponse1 = + // api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, + // validName1); + // String renameResponse2 = + // api.renameAttachment( + // appUrl, entityName, facetName, entityID, attachmentID2, invalidName2); + + // if (renameResponse1.equals("Renamed") && renameResponse2.equals("Renamed")) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"invalid/attachment2.pdf\\\" contains + // unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\"}}"; + // if (response.equals(expected)) { + // api.renameAttachment( + // appUrl, entityName, facetName, entityID, attachmentID2, "sample1234"); + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if ("Saved".equals(response)) testStatus = true; + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } - if (readResponse.equals("OK")) { - testStatus = true; - } - if (!testStatus) { - fail("Could not verify that notes field was copied from source to target attachment"); - } - } + // if (!testStatus) { + // fail("Multiple renames should have failed due to one unsupported characters"); + // } + // } - @Test - @Order(38) - void testCopyAttachmentWithSecondaryPropertiesField() throws IOException { - System.out.println( - "Test (38): Verify that secondary properties are preserved when copying attachments between entities"); - Boolean testStatus = false; + // @Test + // @Order(17) + // void testRenameSingleAttachmentWithoutSDMRole() throws IOException { + // System.out.println("Test (17) : Rename attachments where user don't have SDM Roles"); + // boolean testStatus = false; + // String apiResponse = apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, entityID); + // String name = "sample123"; // Renaming the attachment + // if (apiResponse == "Entity in draft mode") { + // apiResponse = + // apiNoRoles.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, + // name); + // if (apiResponse.equals("Renamed")) { + // apiResponse = apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // String expected = + // "[{\"code\":\"\",\"message\":\"Could not update the following files. \\n" + // + // + // "\\n" + // + // + // "\\t\\u2022 valid_attachment1.pdf\\n" + // + // + // "\\n" + // + // + // "You do not have the required permissions to update attachments. Kindly contact + // the admin\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + // if (apiResponse.equals(expected)) { + // testStatus = true; + // } + // } else { + // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // } + // } + // if (!testStatus) { + // fail("Attachment got renamed without SDM roles."); + // } + // } - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity"); - } + // @Test + // @Order(18) + // void testRenameToValidateNames() throws IOException { + // System.out.println("Test (18) : Rename attachments to validate names"); + // boolean testStatus = false, successCount = true; + // String generatedID = ""; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // entityID3 = response; + // String[] filetoUpload = {"sample.pdf", "sample.txt", "sample.exe", "sample2.pdf"}; + // String[] names = {"Restricted/Character", " ", "duplicateName.pdf", + // "duplicateName.pdf"}; + + // ClassLoader classLoader = getClass().getClassLoader(); + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID3); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // for (int i = 0; i < filetoUpload.length; i++) { + // File file = new File(classLoader.getResource(filetoUpload[i]).getFile()); + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData, + // file); + // generatedID = createResponse.get(1); + // response = + // api.renameAttachment(appUrl, entityName, facetName, entityID3, generatedID, + // names[i]); + // successCount &= "Renamed".equals(response); + // } + // if (successCount) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // String expected = + // "{\"error\":{\"code\":\"400\",\"message\":\"The object name cannot be empty or + // consist entirely of space characters. Enter a value.\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"Restricted/Character\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An object named \\\"duplicateName.pdf\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + // if (response.equals(expected)) { + // response = api.deleteEntityDraft(appUrl, entityName, entityID3); + // if (response.equals("Entity Draft Deleted")) testStatus = true; + // } + // } + // if (!testStatus) fail("Could not create entity"); + // } else { + // fail("Could not create entity"); + // return; + // } + // } - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample1.pdf").getFile()); + // @Test + // @Order(19) + // void testDeleteSingleAttachment() throws IOException { + // System.out.println("Test (19) : Delete single attachment"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response == "Entity in draft mode") { + // response = api.deleteAttachment(appUrl, entityName, facetName, entityID, attachmentID1); + // if (response == "Deleted") { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response == "Saved") { + // response = api.readAttachment(appUrl, entityName, facetName, entityID, attachmentID1); + // if (response.equals("Could not read Attachment")) { + // testStatus = true; + // } + // } + // } + // } + // if (!testStatus) { + // fail("Could not read Attachment"); + // } + // } - Map postData = new HashMap<>(); - postData.put("up__ID", copyCustomSourceEntity); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // @Test + // @Order(20) + // void testDeleteMultipleAttachments() throws IOException { + // System.out.println("Test (20) : Delete multiple attachments"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response == "Entity in draft mode") { + // String response1 = + // api.deleteAttachment(appUrl, entityName, facetName, entityID, attachmentID2); + // String response2 = + // api.deleteAttachment(appUrl, entityName, facetName, entityID, attachmentID3); + // if (response1 == "Deleted" && response2 == "Deleted") { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + // if (response == "Saved") { + // response1 = api.readAttachment(appUrl, entityName, facetName, entityID, attachmentID2); + // response2 = api.readAttachment(appUrl, entityName, facetName, entityID, attachmentID3); + // if (response1.equals("Could not read Attachment") + // && response2.equals("Could not read Attachment")) { + // testStatus = true; + // } + // } + // } + // } + // if (!testStatus) { + // fail("Could not delete attachment"); + // } + // } - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + // @Test + // @Order(21) + // void testUploadBlockedMimeType() throws IOException { + // System.out.println("Test (21): Upload blocked mimeType .rtf"); + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // entityID2 = response; + + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new + // File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID2); + // postData.put("mimeType", "application/rtf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, entityID2, srvpath, postData, + // file); + // String actualResponse = createResponse.get(0); + // String expectedJson = + // "{\"error\":{\"code\":\"500\",\"message\":\"This file type is not allowed in this + // repository. Contact your administrator for assistance.\"}}"; + + // if (expectedJson.equals(actualResponse)) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + // if ("Saved".equals(response)) { + // testStatus = true; + // } + // } else { + // api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + // } + // } + // if (!testStatus) { + // fail("Attachment got uploaded with blocked .rtf MIME type"); + // } + // } - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } + // @Test + // @Order(22) + // void testDeleteEntity() { + // System.out.println("Test (22) : Delete entity"); + // Boolean testStatus = false; + // String response = api.deleteEntity(appUrl, entityName, entityID); + // String response2 = api.deleteEntity(appUrl, entityName, entityID2); + // if (response == "Entity Deleted" && response2 == "Entity Deleted") { + // testStatus = true; + // } + // if (!testStatus) { + // fail("Could not delete entity"); + // } + // } - String sourceAttachmentId = createResponse.get(1); - - // Update attachment with secondary properties - // DocumentInfoRecordBoolean : Set to true - RequestBody bodyBoolean = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyBoolean); - - if (!updateSecondaryPropertyResponse1.equals("Updated")) { - fail( - "Could not update attachment DocumentInfoRecordBoolean field. Response: " - + updateSecondaryPropertyResponse1); - } + // @Test + // @Order(23) + // void testUpdateValidSecondaryProperty_beforeEntityIsSaved_singleAttachment() throws IOException + // { + // System.out.println("Test (23): Rename & Update secondary property before entity is saved"); + // System.out.println("Creating entity"); + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (response != "Could not create entity") { + // entityID3 = response; + // System.out.println("Entity created"); + // System.out.println("Creating attachment"); + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID3); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData, + // file); + // String check = createResponse.get(0); + // if (check.equals("Attachment created")) { + // attachmentID1 = createResponse.get(1); + // System.out.println("Attachment created"); + // String name1 = "sample1234.pdf"; + // String secondaryPropertyString = "sample12345"; + // Integer secondaryPropertyInt = 1234; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // System.out.println("Renaming and updating secondary properties for attachment"); + // String response1 = + // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + // // Update secondary properties for String + // String dropdownValue1 = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + // // Update secondary properties for DateTime + // RequestBody bodyDateTime = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // // Update secondary properties for Boolean + // RequestBody bodyBoolean = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + // if (response1 == "Renamed" + // && updateSecondaryPropertyResponse1 == "Updated" + // && updateSecondaryPropertyResponse2 == "Updated" + // && updateSecondaryPropertyResponse3 == "Updated" + // && updateSecondaryPropertyResponse4 == "Updated") { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // if (response.equals("Saved")) { + // System.out.println("Entity saved"); + // testStatus = true; + // System.out.println("Renamed & updated Secondary properties for attachment"); + // } + // } + // } + // } + // if (!testStatus) { + // fail("Could not update secondary property before entity is saved"); + // } + // } - // customProperty2 : Set to 12345 - Integer customProperty2Value = 12345; - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty2\" : " + customProperty2Value + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyInt); - - if (!updateSecondaryPropertyResponse2.equals("Updated")) { - fail( - "Could not update attachment customProperty2 field. Response: " - + updateSecondaryPropertyResponse2); - } + // @Test + // @Order(24) + // void testUpdateValidSecondaryProperty_afterEntityIsSaved_singleAttachment() { + // System.out.println("Test (24): Rename & Update secondary property after entity is saved"); + // System.out.println("Editing entity"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + // if (response == "Entity in draft mode") { + // String name1 = "sample.pdf"; + // String secondaryPropertyString = "sample"; + // Integer secondaryPropertyInt = 12; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // System.out.println("Renaming and updating secondary properties for attachment"); + // String response1 = + // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + // // Update secondary properties for String + // String dropdownValue1 = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + // // Update secondary properties for DateTime + // RequestBody bodyDateTime = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // // Update secondary properties for Boolean + // RequestBody bodyBoolean = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + // if (response1 == "Renamed" + // && updateSecondaryPropertyResponse1 == "Updated" + // && updateSecondaryPropertyResponse2 == "Updated" + // && updateSecondaryPropertyResponse3 == "Updated" + // && updateSecondaryPropertyResponse4 == "Updated") { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // if (response.equals("Saved")) { + // System.out.println("Entity saved"); + // testStatus = true; + // System.out.println("Renamed & updated Secondary properties for attachment"); + // } + // } + // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + // if (deleteEntityResponse != "Entity Deleted") { + // fail("Could not delete entity"); + // } + // } + // if (!testStatus) { + // fail("Could not update secondary property after entity is saved"); + // } + // } - // Save source entity - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity. Response: " + saveSourceResponse); - } + // @Test + // @Order(25) + // void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_singleAttachment() + // throws IOException { + // System.out.println( + // "Test (25): Rename & Update invalid secondary property before entity is saved"); + // System.out.println("Creating entity"); + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!"Could not create entity".equals(response)) { + // entityID3 = response; + // System.out.println("Entity created"); + // System.out.println("Creating attachment"); + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID3); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData, + // file); + // String check = createResponse.get(0); + // if ("Attachment created".equals(check)) { + // attachmentID1 = createResponse.get(1); + // System.out.println("Attachment created"); + // String name1 = "sample1234.pdf"; + + // // Dropdown values for secondaryPropertyString + // String[] dropdownValues = {"A", "B", "C"}; + // // Select one dropdown value (e.g., "A") + // String secondaryPropertyString = dropdownValues[0]; + + // Integer secondaryPropertyInt = 1234; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // String invalidProperty = "testid"; + + // System.out.println("Renaming and updating invalid secondary properties for attachment"); + // String response1 = + // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + + // // Update secondary properties for String using dropdown selected value as object with + // code + + // String dropdownValue1 = integrationTestUtils.getDropDownValue(); + // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + // RequestBody bodyDropdown1 = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown1); + + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + + // // Update secondary properties for DateTime + // RequestBody bodyDateTime = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + + // // Update secondary properties for Boolean + // RequestBody bodyBoolean = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + + // // Update invalid secondary property + // String updateSecondaryPropertyResponse5 = + // api.updateInvalidSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, invalidProperty); + + // if ("Renamed".equals(response1) + // && "Updated".equals(updateSecondaryPropertyResponse1) + // && "Updated".equals(updateSecondaryPropertyResponse2) + // && "Updated".equals(updateSecondaryPropertyResponse3) + // && "Updated".equals(updateSecondaryPropertyResponse4) + // && "Updated".equals(updateSecondaryPropertyResponse5)) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // Map attachmentMetadata = + // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); + // assertEquals("sample.pdf", attachmentMetadata.get("fileName")); + // assertNull(attachmentMetadata.get("customProperty3")); + // assertNull(attachmentMetadata.get("customProperty4")); + // assertNull(attachmentMetadata.get("customProperty1_code")); + // assertNull(attachmentMetadata.get("customProperty2")); + // assertNull(attachmentMetadata.get("customProperty6")); + // assertNull(attachmentMetadata.get("customProperty5")); + + // String expectedResponse = + // "[{\"code\":\"\",\"message\":\"The following secondary properties are not + // supported.\\n" + // + // + // "\\n" + // + // + // "\\t\\u2022 id1\\n" + // + // + // "\\n" + // + // + // "Please contact your administrator for assistance with any necessary + // adjustments.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + // if (response.equals(expectedResponse)) { + // System.out.println("Entity saved"); + // testStatus = true; + // System.out.println( + // "Rename & update secondary properties for attachment is unsuccessfull"); + // } + // } + // } + // } + // if (!testStatus) { + // fail("Could not update secondary property before entity is saved"); + // } + // } - // Fetch attachment metadata to get objectId and verify secondary properties - Map sourceAttachmentMetadata = - api.fetchMetadata( - appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + // @Test + // @Order(26) + // void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_singleAttachment() throws + // IOException { + // System.out.println( + // "Test (26): Rename & Update invalid secondary property after entity is saved"); + // System.out.println("Editing entity"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + // if (response == "Entity in draft mode") { + // String name1 = "sample.pdf"; + // String secondaryPropertyString = "A"; + // Integer secondaryPropertyInt = 12; + // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + // String invalidProperty = "testidinvalid"; + // System.out.println("Renaming and updating invalid secondary properties for attachment"); + // String response1 = + // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + // // Update secondary properties for DateTime + // RequestBody bodyDateTime = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + // String updateSecondaryPropertyResponse3 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // // Update secondary properties for Boolean + // RequestBody bodyBoolean = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse4 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + // // Update invalid secondary property + // String updateSecondaryPropertyResponse5 = + // api.updateInvalidSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, invalidProperty); + // if (response1 == "Renamed" + // && updateSecondaryPropertyResponse1 == "Updated" + // && updateSecondaryPropertyResponse2 == "Updated" + // && updateSecondaryPropertyResponse3 == "Updated" + // && updateSecondaryPropertyResponse4 == "Updated" + // && updateSecondaryPropertyResponse5 == "Updated") { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // Map attachmentMetadata = + // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); + // assertEquals("sample.pdf", attachmentMetadata.get("fileName")); + // assertNull(attachmentMetadata.get("customProperty3")); + // assertNull(attachmentMetadata.get("customProperty4")); + // assertNull(attachmentMetadata.get("customProperty1_code")); + // assertNull(attachmentMetadata.get("customProperty2")); + // assertNull(attachmentMetadata.get("customProperty6")); + // assertNull(attachmentMetadata.get("customProperty5")); + + // String expectedResponse = + // "[{\"code\":\"\",\"message\":\"The following secondary properties are not + // supported.\\n" + // + // + // "\\n" + // + // + // "\\t\\u2022 id1\\n" + // + // + // "\\n" + // + // + // "Please contact your administrator for assistance with any necessary + // adjustments.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + // if (response.equals(expectedResponse)) { + // System.out.println("Entity saved"); + // testStatus = true; + // System.out.println( + // "Rename & update secondary properties for attachment is unsuccessfull"); + // } + // } + // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + // if (deleteEntityResponse != "Entity Deleted") { + // fail("Could not delete entity"); + // } + // } + // if (!testStatus) { + // fail("Could not update secondary property before entity is saved"); + // } + // } - if (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId"); - } + // @Test + // @Order(27) + // void testUpdateValidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() + // throws IOException { + // System.out.println( + // "Test (27): Rename & Update valid secondary properties for multiple attachments before + // entity is saved"); + // System.out.println("Creating entity"); + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (response != "Could not create entity") { + // entityID3 = response; + + // System.out.println("Entity created"); + + // System.out.println("Creating attachment PDF"); + // ClassLoader classLoader = getClass().getClassLoader(); + + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // Map postData1 = new HashMap<>(); + // postData1.put("up__ID", entityID3); + // postData1.put("mimeType", "application/pdf"); + // postData1.put("createdAt", new Date().toString()); + // postData1.put("createdBy", "test@test.com"); + // postData1.put("modifiedBy", "test@test.com"); + + // List createResponse1 = + // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData1, + // file); + // if (createResponse1.get(0).equals("Attachment created")) { + // attachmentID1 = createResponse1.get(1); + // System.out.println("Attachment created"); + // } - // Store objectId in array for reuse - String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - if (sourceObjectIds.size() < 2) { - sourceObjectIds.add(sourceObjectId); - } else { - sourceObjectIds.set(1, sourceObjectId); - } + // System.out.println("Creating attachment TXT"); + // file = new File(classLoader.getResource("sample.txt").getFile()); + // Map postData2 = new HashMap<>(); + // postData2.put("up__ID", entityID3); + // postData2.put("mimeType", "application/txt"); + // postData2.put("createdAt", new Date().toString()); + // postData2.put("createdBy", "test@test.com"); + // postData2.put("modifiedBy", "test@test.com"); + + // List createResponse2 = + // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData2, + // file); + // if (createResponse2.get(0).equals("Attachment created")) { + // attachmentID2 = createResponse2.get(1); + // System.out.println("Attachment created"); + // } - // Verify all secondary properties in source attachment - Boolean sourceCustomProperty6 = - sourceAttachmentMetadata.get("customProperty6") != null - ? (Boolean) sourceAttachmentMetadata.get("customProperty6") - : null; - Integer sourceCustomProperty2 = - sourceAttachmentMetadata.get("customProperty2") != null - ? (Integer) sourceAttachmentMetadata.get("customProperty2") - : null; - - if (sourceCustomProperty6 == null || !sourceCustomProperty6) { - fail( - "DocumentInfoRecordBoolean was not properly set in source attachment. Expected: true, Got: " - + sourceCustomProperty6); - } + // System.out.println("Creating attachment EXE"); + // file = new File(classLoader.getResource("sample.exe").getFile()); + // Map postData3 = new HashMap<>(); + // postData3.put("up__ID", entityID3); + // postData3.put("mimeType", "application/exe"); + // postData3.put("createdAt", new Date().toString()); + // postData3.put("createdBy", "test@test.com"); + // postData3.put("modifiedBy", "test@test.com"); + + // List createResponse3 = + // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData3, + // file); + // if (createResponse3.get(0).equals("Attachment created")) { + // attachmentID3 = createResponse3.get(1); + // System.out.println("Attachment created"); + // } - if (!customProperty2Value.equals(sourceCustomProperty2)) { - fail( - "customProperty2 was not properly set in source attachment. Expected: " - + customProperty2Value - + ", Got: " - + sourceCustomProperty2); - } + // String check1 = createResponse1.get(0); + // String check2 = createResponse2.get(0); + // String check3 = createResponse3.get(0); + // if (check1.equals("Attachment created") + // && check2.equals("Attachment created") + // && check3.equals("Attachment created")) { + // Boolean attachment1Updated = false; + // Boolean attachment2Updated = false; + // Boolean attachment3Updated = false; + + // String name1 = "sample1234.pdf"; + // Integer secondaryPropertyInt1 = 1234; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // System.out.println("Renaming and updating secondary properties for attachment PDF"); + // String responsePDF1 = + // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + // // Update secondary properties for String + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponsePDF1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponsePDF2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + // // Update secondary properties for DateTime + // RequestBody bodyDateTime = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponsePDF3 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // // Update secondary properties for Boolean + // RequestBody bodyBoolean = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponsePDF4 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + // if (responsePDF1 == "Renamed" + // && updateSecondaryPropertyResponsePDF1 == "Updated" + // && updateSecondaryPropertyResponsePDF2 == "Updated" + // && updateSecondaryPropertyResponsePDF3 == "Updated" + // && updateSecondaryPropertyResponsePDF4 == "Updated") { + // System.out.println("Renamed & updated Secondary properties for attachment PDF"); + // attachment1Updated = true; + // } + + // System.out.println("Updating secondary properties for attachment TXT"); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponseTXT1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); + // if (updateSecondaryPropertyResponseTXT1 == "Updated") { + // System.out.println("Updated Secondary properties for attachment TXT"); + // attachment2Updated = true; + // } + // Integer secondaryPropertyInt3 = 1234; + // LocalDateTime secondaryPropertyDateTime3 = LocalDateTime.now(); + // System.out.println("Updating secondary properties for attachment EXE"); + // // Update secondary properties for String + // String dropdownValue1 = integrationTestUtils.getDropDownValue(); + // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + // RequestBody bodyDropdown1 = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + // String updateSecondaryPropertyResponseEXE1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown1); + // // Update secondary properties for Integer + // RequestBody bodyInt3 = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + // String updateSecondaryPropertyResponseEXE2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); + // // Update secondary properties for DateTime + // RequestBody bodyDateTime3 = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime3 + "\"\n}")); + // String updateSecondaryPropertyResponseEXE3 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID3, bodyDateTime3); + + // if (updateSecondaryPropertyResponseEXE1 == "Updated" + // && updateSecondaryPropertyResponseEXE2 == "Updated" + // && updateSecondaryPropertyResponseEXE3 == "Updated") { + // System.out.println("Updated Secondary properties for attachment EXE"); + // attachment3Updated = true; + // } + + // if (attachment1Updated && attachment2Updated && attachment3Updated) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // if (response.equals("Saved")) { + // System.out.println("Entity saved"); + // testStatus = true; + // System.out.println("Renamed & updated Secondary properties for attachments"); + // } + // } + // } + // } + // if (!testStatus) { + // fail("Could not update secondary property before entity is saved"); + // } + // } - String editTargetResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!editTargetResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity"); - } + // @Test + // @Order(28) + // void testUpdateValidSecondaryProperty_afterEntityIsSaved_multipleAttachments() { + // System.out.println( + // "Test (28): Rename & Update valid secondary properties for multiple attachments after + // entity is saved"); + // System.out.println("Editing entity"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + // if (response == "Entity in draft mode") { + // Boolean attachment1Updated = false; + // Boolean attachment2Updated = false; + // Boolean attachment3Updated = false; + + // String name1 = "sample1.pdf"; + // Integer secondaryPropertyInt1 = 12; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // System.out.println("Renaming and updating secondary properties for attachment PDF"); + // String responsePDF1 = + // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + // // Update secondary properties for String + // String dropdownValue1 = integrationTestUtils.getDropDownValue(); + // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + // RequestBody bodyDropdown1 = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + // String updateSecondaryPropertyResponsePDF1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown1); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponsePDF2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + // // Update secondary properties for DateTime + // RequestBody bodyDateTime = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponsePDF3 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // // Update secondary properties for Boolean + // RequestBody bodyBoolean = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponsePDF4 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + + // if (responsePDF1 == "Renamed" + // && updateSecondaryPropertyResponsePDF1 == "Updated" + // && updateSecondaryPropertyResponsePDF2 == "Updated" + // && updateSecondaryPropertyResponsePDF3 == "Updated" + // && updateSecondaryPropertyResponsePDF4 == "Updated") { + // System.out.println("Renamed & updated Secondary properties for attachment PDF"); + // attachment1Updated = true; + // } - // Copy attachment to target entity - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(1)); // Use objectId from array + // System.out.println("Updating secondary properties for attachment TXT"); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponseTXT1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); + // if (updateSecondaryPropertyResponseTXT1 == "Updated") { + // System.out.println("Updated Secondary properties for attachment TXT"); + // attachment2Updated = true; + // } - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + // Integer secondaryPropertyInt3 = 123; + // LocalDateTime secondaryPropertyDateTime3 = LocalDateTime.now(); + // System.out.println("Updating secondary properties for attachment EXE"); + // // Update secondary properties for String + // String dropdownValue2 = integrationTestUtils.getDropDownValue(); + // String jsonDropdown2 = "{ \"customProperty1_code\" : \"" + dropdownValue2 + "\" }"; + // RequestBody bodyDropdown2 = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown2); + // String updateSecondaryPropertyResponseEXE1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown2); + // // Update secondary properties for Integer + // RequestBody bodyInt3 = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + // String updateSecondaryPropertyResponseEXE2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); + // // Update secondary properties for DateTime + // RequestBody bodyDateTime3 = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime3 + "\"\n}")); + // String updateSecondaryPropertyResponseEXE3 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID3, bodyDateTime3); + + // if (updateSecondaryPropertyResponseEXE1 == "Updated" + // && updateSecondaryPropertyResponseEXE2 == "Updated" + // && updateSecondaryPropertyResponseEXE3 == "Updated") { + // System.out.println("Updated Secondary properties for attachment EXE"); + // attachment3Updated = true; + // } - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity: " + copyResponse); - } + // if (attachment1Updated && attachment2Updated && attachment3Updated) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // if (response.equals("Saved")) { + // System.out.println("Entity saved"); + // testStatus = true; + // System.out.println("Renamed & updated Secondary properties for attachments"); + // } + // } + // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + // if (deleteEntityResponse != "Entity Deleted") { + // fail("Could not delete entity"); + // } + // } + // if (!testStatus) { + // fail("Could not update secondary property after entity is saved"); + // } + // } - // Save target entity - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity"); - } + // @Test + // @Order(29) + // void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() + // throws IOException { + // System.out.println( + // "Test (29): Rename & Update invalid and valid secondary properties for multiple + // attachments before entity is saved"); + // System.out.println("Creating entity"); + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (response != "Could not create entity") { + // entityID3 = response; + + // System.out.println("Entity created"); + + // System.out.println("Creating attachment PDF"); + // ClassLoader classLoader = getClass().getClassLoader(); + + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // Map postData1 = new HashMap<>(); + // postData1.put("up__ID", entityID3); + // postData1.put("mimeType", "application/pdf"); + // postData1.put("createdAt", new Date().toString()); + // postData1.put("createdBy", "test@test.com"); + // postData1.put("modifiedBy", "test@test.com"); + + // List createResponse1 = + // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData1, + // file); + // if (createResponse1.get(0).equals("Attachment created")) { + // attachmentID1 = createResponse1.get(1); + // System.out.println("Attachment created"); + // } - // Fetch target entity attachments metadata - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // System.out.println("Creating attachment TXT"); + // file = new File(classLoader.getResource("sample.txt").getFile()); + // Map postData2 = new HashMap<>(); + // postData2.put("up__ID", entityID3); + // postData2.put("mimeType", "application/txt"); + // postData2.put("createdAt", new Date().toString()); + // postData2.put("createdBy", "test@test.com"); + // postData2.put("modifiedBy", "test@test.com"); + + // List createResponse2 = + // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData2, + // file); + // if (createResponse2.get(0).equals("Attachment created")) { + // attachmentID2 = createResponse2.get(1); + // System.out.println("Attachment created"); + // } - if (targetAttachmentsMetadata.isEmpty()) { - fail("No attachments found in target entity"); - } + // System.out.println("Creating attachment EXE"); + // file = new File(classLoader.getResource("sample.exe").getFile()); + // Map postData3 = new HashMap<>(); + // postData3.put("up__ID", entityID3); + // postData3.put("mimeType", "application/exe"); + // postData3.put("createdAt", new Date().toString()); + // postData3.put("createdBy", "test@test.com"); + // postData3.put("modifiedBy", "test@test.com"); + + // List createResponse3 = + // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData3, + // file); + // if (createResponse3.get(0).equals("Attachment created")) { + // attachmentID3 = createResponse3.get(1); + // System.out.println("Attachment created"); + // } - // Verify the copied attachment has the same secondary properties - // Find the attachment we just copied by matching the filename - Map copiedAttachmentMetadata = - targetAttachmentsMetadata.stream() - .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) - .findFirst() - .orElse(null); + // String check1 = createResponse1.get(0); + // String check2 = createResponse2.get(0); + // String check3 = createResponse3.get(0); + // if (check1.equals("Attachment created") + // && check2.equals("Attachment created") + // && check3.equals("Attachment created")) { + // Boolean attachment1Updated = false; + // Boolean attachment2Updated = false; + // Boolean attachment3Updated = false; + + // String name1 = "sample1234.pdf"; + // Integer secondaryPropertyInt1 = 1234; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // String invalidPropertyPDF = "testidinvalidPDF"; + // System.out.println("Renaming and updating invalid secondary properties for attachment + // PDF"); + // String responsePDF1 = + // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + // // Update secondary properties for String + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponsePDF1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyint = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponsePDF2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyint); + // // Update secondary properties for DateTime + // RequestBody bodyDateTime = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponsePDF3 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // // Update secondary properties for Boolean + // RequestBody bodyBoolean = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponsePDF4 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + // // Update invalid secondary property + // String updateSecondaryPropertyResponsePDF5 = + // api.updateInvalidSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, invalidPropertyPDF); + // if (responsePDF1 == "Renamed" + // && updateSecondaryPropertyResponsePDF1 == "Updated" + // && updateSecondaryPropertyResponsePDF2 == "Updated" + // && updateSecondaryPropertyResponsePDF3 == "Updated" + // && updateSecondaryPropertyResponsePDF4 == "Updated" + // && updateSecondaryPropertyResponsePDF5 == "Updated") { + // attachment1Updated = true; + // } + + // System.out.println("Updating valid secondary properties for attachment TXT"); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponseTXT1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); + // if (updateSecondaryPropertyResponseTXT1 == "Updated") { + // System.out.println("Updated Secondary properties for attachment TXT"); + // attachment2Updated = true; + // } + + // Integer secondaryPropertyInt3 = 1234; + // System.out.println("Updating valid secondary properties for attachment EXE"); + + // // Update secondary properties for String + // RequestBody bodyDropdown1 = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponseEXE1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown1); + // // Update secondary properties for Integer + // RequestBody bodyInt3 = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + // String updateSecondaryPropertyResponseEXE2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); + + // if (updateSecondaryPropertyResponseEXE1 == "Updated" + // && updateSecondaryPropertyResponseEXE2 == "Updated") { + // System.out.println("Updated Secondary properties for attachment EXE"); + // attachment3Updated = true; + // } + + // if (attachment1Updated && attachment2Updated && attachment3Updated) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // Map attachmentMetadataPDF = + // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); + // assertEquals("sample.pdf", attachmentMetadataPDF.get("fileName")); + // assertNull(attachmentMetadataPDF.get("customProperty3")); + // assertNull(attachmentMetadataPDF.get("customProperty4")); + // assertNull(attachmentMetadataPDF.get("customProperty1_code")); + // assertNull(attachmentMetadataPDF.get("customProperty2")); + // assertNull(attachmentMetadataPDF.get("customProperty6")); + // assertNull(attachmentMetadataPDF.get("customProperty5")); + + // Map attachmentMetadataTXT = + // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID2); + // assertEquals("sample.txt", attachmentMetadataTXT.get("fileName")); + // assertNull(attachmentMetadataTXT.get("customProperty3")); + // assertNull(attachmentMetadataTXT.get("customProperty4")); + // assertNull(attachmentMetadataTXT.get("customProperty1_code")); + // assertNull(attachmentMetadataTXT.get("customProperty2")); + // assertTrue((Boolean) attachmentMetadataTXT.get("customProperty6")); + // assertNull(attachmentMetadataTXT.get("customProperty5")); + + // Map attachmentMetadataEXE = + // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID3); + // assertEquals("sample.exe", attachmentMetadataEXE.get("fileName")); + // assertNull(attachmentMetadataEXE.get("customProperty3")); + // assertNull(attachmentMetadataEXE.get("customProperty4")); + // assertEquals(dropdownValue, attachmentMetadataEXE.get("customProperty1_code")); + // assertEquals(1234, attachmentMetadataEXE.get("customProperty2")); + + // String expectedResponse = + // "[{\"code\":\"\",\"message\":\"The following secondary properties are not + // supported.\\n" + // + // + // "\\n" + // + // + // "\\t\\u2022 id1\\n" + // + // + // "\\n" + // + // + // "Please contact your administrator for assistance with any necessary + // adjustments.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + // if (response.equals(expectedResponse)) { + // System.out.println("Entity saved"); + // testStatus = true; + // System.out.println( + // "Rename & update unsuccessfull for invalid Secondary properties and successfull + // for valid property attachments"); + // } + // } + // } + // } + // if (!testStatus) { + // fail("Could not update secondary property before entity is saved"); + // } + // } - if (copiedAttachmentMetadata == null) { - fail("Could not find the copied attachment with file in target entity"); - } + // @Test + // @Order(30) + // void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_multipleAttachments() + // throws IOException { + // System.out.println( + // "Test (30): Rename & Update invalid and valid secondary properties for multiple + // attachments after entity is saved"); + // System.out.println("Editing entity"); + // Boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + // if (response == "Entity in draft mode") { + // Boolean attachment1Updated = false; + // Boolean attachment2Updated = false; + // Boolean attachment3Updated = false; + + // String name1 = "sample.pdf"; + // Integer secondaryPropertyInt1 = 12; + // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + // String invalidPropertyPDF = "testidinvalidPDF"; + // System.out.println("Renaming and updating invalid secondary properties for attachment + // PDF"); + // String responsePDF1 = + // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + // // Update secondary properties for String + // String dropdownValue = integrationTestUtils.getDropDownValue(); + // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + // RequestBody bodyDropdown = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponsePDF1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); + // // Update secondary properties for Integer + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + // String updateSecondaryPropertyResponsePDF2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + // // Update secondary properties for DateTime + // RequestBody bodyDateTime = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + // String updateSecondaryPropertyResponsePDF3 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // // Update secondary properties for Boolean + // RequestBody bodyBoolean = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponsePDF4 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + // // Update invalid secondary property + // String updateSecondaryPropertyResponsePDF5 = + // api.updateInvalidSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID1, invalidPropertyPDF); + // if (responsePDF1 == "Renamed" + // && updateSecondaryPropertyResponsePDF1 == "Updated" + // && updateSecondaryPropertyResponsePDF2 == "Updated" + // && updateSecondaryPropertyResponsePDF3 == "Updated" + // && updateSecondaryPropertyResponsePDF4 == "Updated" + // && updateSecondaryPropertyResponsePDF5 == "Updated") { + // attachment1Updated = true; + // } - Boolean copiedCustomProperty6 = - copiedAttachmentMetadata.get("customProperty6") != null - ? (Boolean) copiedAttachmentMetadata.get("customProperty6") - : null; - Integer copiedCustomProperty2 = - copiedAttachmentMetadata.get("customProperty2") != null - ? (Integer) copiedAttachmentMetadata.get("customProperty2") - : null; - - // Verify DocumentInfoRecordBoolean - if (copiedCustomProperty6 == null || !copiedCustomProperty6) { - fail( - "DocumentInfoRecordBoolean as not properly copied. Expected: true, Got: " - + copiedCustomProperty6); - } + // System.out.println("Updating valid secondary properties for attachment TXT"); + // // Update secondary properties for Boolean + // RequestBody bodyBool = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + false + "\n}")); + // String updateSecondaryPropertyResponseTXT1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); + // if (updateSecondaryPropertyResponseTXT1 == "Updated") { + // System.out.println("Updated Secondary properties for attachment TXT"); + // attachment2Updated = true; + // } - // Verify customProperty2 - if (!customProperty2Value.equals(copiedCustomProperty2)) { - fail( - "customProperty2 was not properly copied. Expected: " - + customProperty2Value - + ", Got: " - + copiedCustomProperty2); - } + // Integer secondaryPropertyInt3 = 12; + // System.out.println("Updating valid secondary properties for attachment EXE"); + + // // Update secondary properties for String + // RequestBody bodyDropdown1 = + // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + // String updateSecondaryPropertyResponseEXE1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown1); + // // Update secondary properties for Integer + // RequestBody bodyInt3 = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8( + // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + // String updateSecondaryPropertyResponseEXE2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); + + // if (updateSecondaryPropertyResponseEXE1 == "Updated" + // && updateSecondaryPropertyResponseEXE2 == "Updated") { + // System.out.println("Updated Secondary properties for attachment EXE"); + // attachment3Updated = true; + // } - // Verify attachment content can be read from target entity - String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - String readResponse = - api.readAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + // if (attachment1Updated && attachment2Updated && attachment3Updated) { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + // Map attachmentMetadataPDF = + // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); + // assertEquals("sample.pdf", attachmentMetadataPDF.get("fileName")); + // assertNull(attachmentMetadataPDF.get("customProperty3")); + // assertNull(attachmentMetadataPDF.get("customProperty4")); + // assertNull(attachmentMetadataPDF.get("customProperty1_code")); + // assertNull(attachmentMetadataPDF.get("customProperty2")); + // assertNull(attachmentMetadataPDF.get("customProperty6")); + // assertNull(attachmentMetadataPDF.get("customProperty5")); + + // Map attachmentMetadataTXT = + // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID2); + // assertEquals("sample.txt", attachmentMetadataTXT.get("fileName")); + // assertNull(attachmentMetadataTXT.get("customProperty3")); + // assertNull(attachmentMetadataTXT.get("customProperty4")); + // assertNull(attachmentMetadataTXT.get("customProperty1_code")); + // assertNull(attachmentMetadataTXT.get("customProperty2")); + // assertFalse((Boolean) attachmentMetadataTXT.get("customProperty6")); + // assertNull(attachmentMetadataTXT.get("customProperty5")); + + // Map attachmentMetadataEXE = + // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID3); + // assertEquals("sample.exe", attachmentMetadataEXE.get("fileName")); + // assertNull(attachmentMetadataEXE.get("customProperty3")); + // assertNull(attachmentMetadataEXE.get("customProperty4")); + // assertEquals(dropdownValue, attachmentMetadataEXE.get("customProperty1_code")); + // assertEquals(12, attachmentMetadataEXE.get("customProperty2")); + + // String expectedResponse = + // "[{\"code\":\"\",\"message\":\"The following secondary properties are not + // supported.\\n" + // + // + // "\\n" + // + // + // "\\t\\u2022 id1\\n" + // + // + // "\\n" + // + // + // "Please contact your administrator for assistance with any necessary + // adjustments.\\n" + // + // + // "\\n" + // + // + // "Table: attachments\\n" + // + // + // "Page: IntegrationTestEntity\",\"numericSeverity\":3}]"; + // if (response.equals(expectedResponse)) { + // System.out.println("Entity saved"); + // testStatus = true; + // System.out.println( + // "Rename & update unsuccessfull for invalid Secondary properties and successfull for + // valid property attachments"); + // } + // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + // if (deleteEntityResponse != "Entity Deleted") { + // fail("Could not delete entity"); + // } + // } + // } + // if (!testStatus) { + // fail("Could not update secondary property before entity is saved"); + // } + // } - if (readResponse.equals("OK")) { - testStatus = true; - } - if (!testStatus) { - fail( - "Could not verify that all secondary properties were copied from source to target attachment"); - } - } + // @Test + // @Order(31) + // void testNAttachments_NewEntity() throws IOException { + // System.out.println( + // "Test (31): Creating new entity and checking only max 4 attachments are allowed to be + // uploaded"); + // System.out.println("Creating entity"); + // Boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (response != "Could not create entity") { + // entityID4 = response; + + // System.out.println("Entity created"); + + // System.out.println("Creating attachment PDF"); + // ClassLoader classLoader = getClass().getClassLoader(); + + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // Map postData1 = new HashMap<>(); + // postData1.put("up__ID", entityID4); + // postData1.put("mimeType", "application/pdf"); + // postData1.put("createdAt", new Date().toString()); + // postData1.put("createdBy", "test@test.com"); + // postData1.put("modifiedBy", "test@test.com"); + + // List createResponse1 = + // api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData1, + // file); + // if (createResponse1.get(0).equals("Attachment created")) { + // attachmentID1 = createResponse1.get(1); + // System.out.println("Attachment created"); + // } - @Test - @Order(39) - void testCopyAttachmentWithNotesAndSecondaryPropertiesField() throws IOException { - System.out.println( - "Test (39): Verify that both notes field and secondary properties are preserved during attachment copy"); - Boolean testStatus = false; + // System.out.println("Creating attachment TXT"); + // file = new File(classLoader.getResource("sample.txt").getFile()); + // Map postData2 = new HashMap<>(); + // postData2.put("up__ID", entityID4); + // postData2.put("mimeType", "application/txt"); + // postData2.put("createdAt", new Date().toString()); + // postData2.put("createdBy", "test@test.com"); + // postData2.put("modifiedBy", "test@test.com"); + + // List createResponse2 = + // api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData2, + // file); + // if (createResponse2.get(0).equals("Attachment created")) { + // attachmentID2 = createResponse2.get(1); + // System.out.println("Attachment created"); + // } - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity"); - } + // System.out.println("Creating attachment EXE"); + // file = new File(classLoader.getResource("sample.exe").getFile()); + // Map postData3 = new HashMap<>(); + // postData3.put("up__ID", entityID4); + // postData3.put("mimeType", "application/exe"); + // postData3.put("createdAt", new Date().toString()); + // postData3.put("createdBy", "test@test.com"); + // postData3.put("modifiedBy", "test@test.com"); + + // List createResponse3 = + // api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData3, + // file); + // if (createResponse3.get(0).equals("Attachment created")) { + // attachmentID3 = createResponse3.get(1); + // System.out.println("Attachment created"); + // } - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample2.pdf").getFile()); + // System.out.println("Creating second attachment pdf"); + // file = new File(classLoader.getResource("sample1.pdf").getFile()); + // Map postData4 = new HashMap<>(); + // postData4.put("up__ID", entityID4); + // postData4.put("mimeType", "application/pdf"); + // postData4.put("createdAt", new Date().toString()); + // postData4.put("createdBy", "test@test.com"); + // postData4.put("modifiedBy", "test@test.com"); + + // List createResponse4 = + // api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData3, + // file); + // if (createResponse4.get(0).equals("Attachment created")) { + // attachmentID4 = createResponse4.get(1); + // System.out.println("Attachment created"); + // } - Map postData = new HashMap<>(); - postData.put("up__ID", copyCustomSourceEntity); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // System.out.println("Creating third attachment pdf"); + // file = new File(classLoader.getResource("sample2.pdf").getFile()); + // Map postData5 = new HashMap<>(); + // postData5.put("up__ID", entityID4); + // postData5.put("mimeType", "application/pdf"); + // postData5.put("createdAt", new Date().toString()); + // postData5.put("createdBy", "test@test.com"); + // postData5.put("modifiedBy", "test@test.com"); + + // List createResponse5 = + // api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData3, + // file); + // if (createResponse5.get(0).equals("Only 4 attachments allowed.")) { + // testStatus = true; + // attachmentID5 = createResponse5.get(1); + // System.out.println("Expected error received: Only 4 attachments allowed."); + // } + // String check = createResponse5.get(0); + // if (check.equals("Attachment created")) { + // testStatus = false; + // } else { + // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID4); + // if (response.equals("Saved")) { + // String expectedJson = + // "{\"error\":{\"code\":\"500\",\"message\":\"Cannot upload more than 4 + // attachments.\"}}"; + // ObjectMapper objectMapper = new ObjectMapper(); + // JsonNode actualJsonNode = objectMapper.readTree(check); + // JsonNode expectedJsonNode = objectMapper.readTree(expectedJson); + // if (expectedJsonNode.equals(actualJsonNode)) { + // testStatus = true; + // } + // } + // } + // } + // if (!testStatus) { + // fail("Attachment was created"); + // } + // } - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + // @Test + // @Order(32) + // void testUploadNAttachments() throws IOException { + // System.out.println("Test (32): Upload maximum 4 attachments in an exsisting entity"); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment"); - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.exe").getFile()); + + // boolean testStatus = false; + // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID4); + // System.out.println("response: " + response); + + // if ("Entity in draft mode".equals(response)) { + // for (int i = 1; i <= 5; i++) { + // // Ensure only one file is uploaded at a time and complete before next + // File tempFile = File.createTempFile("sample_" + i + "_", ".exe"); + // Files.copy(originalFile.toPath(), tempFile.toPath(), + // StandardCopyOption.REPLACE_EXISTING); + + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID4); + // postData.put("mimeType", "application/exe"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, entityID4, srvpath, postData, tempFile); + + // String resultMessage = createResponse.get(0); + // System.out.println("Result message for attachment " + i + ": " + resultMessage); + + // String expectedResponse = + // "{\"error\":{\"code\":\"500\",\"message\":\"Cannot upload more than 4 + // attachments.\"}}"; + // if (resultMessage.equals(expectedResponse)) { + // ObjectMapper objectMapper = new ObjectMapper(); + // JsonNode actualJsonNode = objectMapper.readTree(resultMessage); + // JsonNode expectedJsonNode = objectMapper.readTree(expectedResponse); + // if (expectedJsonNode.equals(actualJsonNode)) { + // testStatus = true; + // } + // } else { + // testStatus = false; + // } + // tempFile.delete(); + // } + // if (!testStatus) { + // fail("5th attachment did not trigger the expected error."); + // } + // // Delete the newly created entity + // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID4); + // if (deleteEntityResponse != "Entity Deleted") { + // fail("Could not delete entity"); + // } else { + // System.out.println("Successfully deleted the test entity4"); + // } + // } + // } - String sourceAttachmentId = createResponse.get(1); + // @Test + // @Order(33) + // void testDiscardDraftWithoutAttachments() { + // System.out.println("Test (33) : Discard draft without adding attachments"); - // Update attachment with notes field - String notesValue = "This attachment has both notes and secondary properties for testing"; - MediaType mediaType = MediaType.parse("application/json"); - String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - String updateNotesResponse = - api.updateSecondaryProperty( - appUrl, - entityName, - facetName, - copyCustomSourceEntity, - sourceAttachmentId, - updateNotesBody); + // if (response.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - if (!updateNotesResponse.equals("Updated")) { - fail("Could not update attachment notes field"); - } + // response = api.deleteEntityDraft(appUrl, entityName, response); + // if (!response.equals("Entity Draft Deleted")) { + // fail("Draft was not discarded properly"); + // } + // } - // Update attachment with secondary properties - // DocumentInfoRecordBoolean : Set to true - RequestBody bodyBoolean = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - String updateSecondaryPropertyResponse1 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyBoolean); - - if (!updateSecondaryPropertyResponse1.equals("Updated")) { - fail( - "Could not update attachment DocumentInfoRecordBoolean (customProperty6) field. Response: " - + updateSecondaryPropertyResponse1); - } + // @Test + // @Order(34) + // void testDiscardDraftWithAttachments() throws IOException { + // System.out.println("Test (34) : Discard draft with attachments"); + // boolean testStatus = false; + // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!response.equals("Could not create entity")) { + // entityID7 = response; + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + + // Map postData1 = new HashMap<>(); + // postData1.put("up__ID", entityID7); + // postData1.put("mimeType", "application/pdf"); + // postData1.put("createdAt", new Date().toString()); + // postData1.put("createdBy", "test@test.com"); + // postData1.put("modifiedBy", "test@test.com"); + + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, entityID7, srvpath, postData1, + // file); + // if (createResponse.get(0).equals("Attachment created")) { + // attachmentID1 = createResponse.get(1); + // } + // String check = createResponse.get(0); + // if (check.equals("Attachment created")) { + // response = api.deleteEntityDraft(appUrl, entityName, entityID7); + // } + // if (response.equals("Entity Draft Deleted")) { + // testStatus = true; + // } + // } + // if (!testStatus) { + // fail("Draft was not discarded properly"); + // } + // } - // customProperty2 : Set to 99999 - Integer customProperty2Value = 99999; - RequestBody bodyInt = - RequestBody.create( - MediaType.parse("application/json"), - ByteString.encodeUtf8("{\n \"customProperty2\" : " + customProperty2Value + "\n}")); - String updateSecondaryPropertyResponse2 = - api.updateSecondaryProperty( - appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyInt); - - if (!updateSecondaryPropertyResponse2.equals("Updated")) { - fail( - "Could not update attachment customProperty2 field. Response: " - + updateSecondaryPropertyResponse2); - } + // @Test + // @Order(35) + // void testCopyAttachmentsSuccessNewEntity() throws IOException { + // System.out.println("Test (35): Copy attachments from one entity to another new entity"); + // List attachments = new ArrayList<>(); + // copyAttachmentSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyAttachmentTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!copyAttachmentSourceEntity.equals("Could not create entity") + // && !copyAttachmentTargetEntity.equals("Could not create entity")) { + // ClassLoader classLoader = getClass().getClassLoader(); + // List files = new ArrayList<>(); + // files.add(new File(classLoader.getResource("sample.pdf").getFile())); + // files.add(new File(classLoader.getResource("sample1.pdf").getFile())); + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID7); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyAttachmentSourceEntity, srvpath, postData, + // file); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment"); + // } + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + // List> attachmentsMetadata = new ArrayList<>(); + // Map fetchAttachmentMetadataResponse; + // for (String attachment : attachments) { + // try { + // fetchAttachmentMetadataResponse = + // api.fetchMetadata( + // appUrl, entityName, facetName, copyAttachmentSourceEntity, attachment); + // attachmentsMetadata.add(fetchAttachmentMetadataResponse); + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } + // for (Map metadata : attachmentsMetadata) { + // if (metadata.containsKey("objectId")) { + // sourceObjectIds.add(metadata.get("objectId").toString()); + // } else { + // fail("Attachment metadata does not contain objectId"); + // } + // } - // Save source entity - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity. Response: " + saveSourceResponse); - } + // if (sourceObjectIds.size() == 2) { + // String copyResponse; + // copyResponse = + // api.copyAttachment( + // appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); + // if (copyResponse.equals("Attachments copied successfully")) { + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // if (saveEntityResponse.equals("Saved")) { + // List> fetchEntityMetadataResponse; + // fetchEntityMetadataResponse = + // api.fetchEntityMetadata(appUrl, entityName, facetName, + // copyAttachmentTargetEntity); + // targetAttachmentIds = + // fetchEntityMetadataResponse.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // String readResponse; + // for (String targetAttachmentId : targetAttachmentIds) { + // readResponse = + // api.readAttachment( + // appUrl, + // entityName, + // facetName, + // copyAttachmentTargetEntity, + // targetAttachmentId); + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment"); + // } + // } + // } else { + // fail("Could not save entity after copying attachments: " + saveEntityResponse); + // } + // } else { + // fail("Could not copy attachments: " + copyResponse); + // } + // } else { + // fail("Could not fetch objects Ids for all attachments"); + // } + // } else { + // fail("Could not create entities"); + // } + // } - // Fetch attachment metadata to get objectId and verify notes and secondary properties - Map sourceAttachmentMetadata = - api.fetchMetadata( - appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + // @Test + // @Order(36) + // void testCopyAttachmentsUnsuccessfulNewEntity() throws IOException { + // System.out.println("Test (36): Copy attachments from one entity to another new entity"); + // String editResponse1 = + // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + // copyAttachmentTargetEntityEmpty = + // api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (editResponse1.equals("Entity in draft mode") + // && !copyAttachmentTargetEntityEmpty.equals("Could not create entity")) { + // sourceObjectIds.add("incorrectObjectId"); + // if (sourceObjectIds.size() == 3) { + // try { + // api.copyAttachment( + // appUrl, entityName, facetName, copyAttachmentTargetEntityEmpty, sourceObjectIds); + // fail("Copy attachments did not throw an error"); + // } catch (IOException e) { + // String saveEntityResponse1 = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + // String saveEntityResponse2 = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntityEmpty); + // String deleteResponse = + // api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntityEmpty); + // if (!saveEntityResponse1.equals("Saved") + // || !saveEntityResponse2.equals("Saved") + // || !deleteResponse.equals("Entity Deleted")) { + // fail("Could not save entities"); + // } + // } + // } else { + // fail("Could not fetch objects Ids for all attachments"); + // } + // } else { + // fail("Could not edit entities"); + // } + // } - if (!sourceAttachmentMetadata.containsKey("objectId")) { - fail("Source attachment metadata does not contain objectId"); - } + // @Test + // @Order(37) + // void testCopyAttachmentWithNotesField() throws IOException { + // System.out.println( + // "Test (37): Create entity with attachment containing notes, copy to new entity and verify + // notes field"); + // Boolean testStatus = false; + // // Create source entity + // copyCustomSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyCustomSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - if (sourceObjectIds.size() < 3) { - sourceObjectIds.add(sourceObjectId); - } else { - sourceObjectIds.set(2, sourceObjectId); - } + // // Create and upload attachment to source entity + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // Map postData = new HashMap<>(); + // postData.put("up__ID", copyCustomSourceEntity); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - String sourceNoteValue = - sourceAttachmentMetadata.get("note") != null - ? sourceAttachmentMetadata.get("note").toString() - : null; - - if (!notesValue.equals(sourceNoteValue)) { - fail( - "Notes field was not properly set in source attachment. Expected: " - + notesValue - + ", Got: " - + sourceNoteValue); - } + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - Boolean sourceCustomProperty6 = - sourceAttachmentMetadata.get("customProperty6") != null - ? (Boolean) sourceAttachmentMetadata.get("customProperty6") - : null; - Integer sourceCustomProperty2 = - sourceAttachmentMetadata.get("customProperty2") != null - ? (Integer) sourceAttachmentMetadata.get("customProperty2") - : null; - - if (sourceCustomProperty6 == null || !sourceCustomProperty6) { - fail( - "DocumentInfoRecordBoolean was not properly set in source attachment. Expected: true, Got: " - + sourceCustomProperty6); - } + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } - if (!customProperty2Value.equals(sourceCustomProperty2)) { - fail( - "customProperty2 was not properly set in source attachment. Expected: " - + customProperty2Value - + ", Got: " - + sourceCustomProperty2); - } + // String sourceAttachmentId = createResponse.get(1); - String editTargetResponse = - api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!editTargetResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity"); - } + // // Update attachment with notes field + // String notesValue = "This is a test note for copy attachment verification"; + // MediaType mediaType = MediaType.parse("application/json"); + // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + // RequestBody updateBody = RequestBody.create(jsonPayload, mediaType); - // Copy attachment to target entity - List objectIdsToCopy = new ArrayList<>(); - objectIdsToCopy.add(sourceObjectIds.get(2)); // Use objectId from array + // String updateResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, + // updateBody); - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); + // if (!updateResponse.equals("Updated")) { + // fail("Could not update attachment notes field"); + // } - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy attachment to target entity: " + copyResponse); - } + // // Save source entity + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity"); + // } - // Save target entity - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity"); - } + // // Fetch attachment metadata to get objectId + // Map sourceAttachmentMetadata = + // api.fetchMetadata( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - // Fetch target entity attachments metadata - List> targetAttachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId"); + // } - if (targetAttachmentsMetadata.isEmpty()) { - fail("No attachments found in target entity"); - } + // // Store objectId in array + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // if (sourceObjectIds.isEmpty()) { + // sourceObjectIds.add(sourceObjectId); + // } else { + // sourceObjectIds.set(0, sourceObjectId); + // } - // Verify the copied attachment has the same notes and secondary properties - // Find the attachment we just copied by matching the filename - Map copiedAttachmentMetadata = - targetAttachmentsMetadata.stream() - .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) - .findFirst() - .orElse(null); + // String sourceNoteValue = + // sourceAttachmentMetadata.get("note") != null + // ? sourceAttachmentMetadata.get("note").toString() + // : null; + + // if (!notesValue.equals(sourceNoteValue)) { + // fail( + // "Notes field was not properly set in source attachment. Expected: " + // + notesValue + // + ", Got: " + // + sourceNoteValue); + // } - if (copiedAttachmentMetadata == null) { - fail("Could not find the copied attachment with fil in target entity"); - } + // // Create target entity + // copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyCustomTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - // Verify notes field was copied - String copiedNoteValue = - copiedAttachmentMetadata.get("note") != null - ? copiedAttachmentMetadata.get("note").toString() - : null; - - if (!notesValue.equals(copiedNoteValue)) { - fail( - "Notes field was not properly copied. Expected: " - + notesValue - + ", Got: " - + copiedNoteValue); - } + // // Copy attachment to target entity + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjectIds.get(0)); // Use objectId from array - // Verify secondary properties were copied - Boolean copiedCustomProperty6 = - copiedAttachmentMetadata.get("customProperty6") != null - ? (Boolean) copiedAttachmentMetadata.get("customProperty6") - : null; - Integer copiedCustomProperty2 = - copiedAttachmentMetadata.get("customProperty2") != null - ? (Integer) copiedAttachmentMetadata.get("customProperty2") - : null; - - // Verify DocumentInfoRecordBoolean - if (copiedCustomProperty6 == null || !copiedCustomProperty6) { - fail( - "DocumentInfoRecordBoolean was not properly copied. Expected: true, Got: " - + copiedCustomProperty6); - } + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, + // objectIdsToCopy); - // Verify customProperty2 - if (!customProperty2Value.equals(copiedCustomProperty2)) { - fail( - "customProperty2 was not properly copied. Expected: " - + customProperty2Value - + ", Got: " - + copiedCustomProperty2); - } + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to target entity: " + copyResponse); + // } - // Verify attachment content can be read from target entity - String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - String readResponse = - api.readAttachment( - appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + // // Save target entity + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity"); + // } - if (readResponse.equals("OK")) { - testStatus = true; - } - if (!testStatus) { - fail( - "Could not verify that notes field and all secondary properties were copied from source to target attachment"); - } - api.deleteEntity(appUrl, entityName, copyCustomSourceEntity); - api.deleteEntity(appUrl, entityName, copyCustomTargetEntity); - } + // // Fetch target entity attachments metadata + // List> targetAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - @Test - @Order(40) - void testCopyAttachmentsSuccessExistingEntity() throws IOException { - System.out.println("Test (40): Copy attachments from one entity to another existing entity"); - List attachments = new ArrayList<>(); - ClassLoader classLoader = getClass().getClassLoader(); - List files = new ArrayList<>(); - File file1 = new File(classLoader.getResource("sample.pdf").getFile()); - File file2 = new File(classLoader.getResource("sample1.pdf").getFile()); - File tempFile1 = new File(System.getProperty("java.io.tmpdir"), "sample_copy_existing_1.pdf"); - Files.copy(file1.toPath(), tempFile1.toPath(), StandardCopyOption.REPLACE_EXISTING); - File tempFile2 = new File(System.getProperty("java.io.tmpdir"), "sample_copy_existing_2.pdf"); - Files.copy(file2.toPath(), tempFile2.toPath(), StandardCopyOption.REPLACE_EXISTING); - files.add(tempFile1); - files.add(tempFile2); - Map postData = new HashMap<>(); - postData.put("up__ID", entityID7); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); - String editResponse1 = - api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - String editResponse2 = - api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - if (editResponse1.equals("Entity in draft mode") - && editResponse2.equals("Entity in draft mode")) { - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, copyAttachmentSourceEntity, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - attachments.add(createResponse.get(1)); - } else { - fail("Could not create attachment"); - } - } - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - List> attachmentsMetadata = new ArrayList<>(); - Map fetchAttachmentMetadataResponse; - for (String attachment : attachments) { - try { - fetchAttachmentMetadataResponse = - api.fetchMetadata( - appUrl, entityName, facetName, copyAttachmentSourceEntity, attachment); - attachmentsMetadata.add(fetchAttachmentMetadataResponse); - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // if (targetAttachmentsMetadata.isEmpty()) { + // fail("No attachments found in target entity"); + // } - sourceObjectIds.clear(); - for (Map metadata : attachmentsMetadata) { - if (metadata.containsKey("objectId")) { - sourceObjectIds.add(metadata.get("objectId").toString()); - } else { - fail("Attachment metadata does not contain objectId"); - } - } + // // Verify the copied attachment has the same notes value + // Map copiedAttachmentMetadata = targetAttachmentsMetadata.get(0); + // String copiedNoteValue = + // copiedAttachmentMetadata.get("note") != null + // ? copiedAttachmentMetadata.get("note").toString() + // : null; + + // if (!notesValue.equals(copiedNoteValue)) { + // fail( + // "Notes field was not properly copied. Expected: " + // + notesValue + // + ", Got: " + // + copiedNoteValue); + // } - if (sourceObjectIds.size() == 2) { - String copyResponse; - copyResponse = - api.copyAttachment( - appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); - if (copyResponse.equals("Attachments copied successfully")) { - String saveEntityResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - if (saveEntityResponse.equals("Saved")) { - List> fetchEntityMetadataResponse; - fetchEntityMetadataResponse = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); - targetAttachmentIds = - fetchEntityMetadataResponse.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - String readResponse; - if (targetAttachmentIds.size() == 4) { - for (String targetAttachmentId : targetAttachmentIds) { - readResponse = - api.readAttachment( - appUrl, - entityName, - facetName, - copyAttachmentTargetEntity, - targetAttachmentId); - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment"); - } - } - } - // api.deleteEntity(appUrl, entityName, copyAttachmentSourceEntity); - // api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntity); - } else { - fail("Could not save entity after copying attachments: " + saveEntityResponse); - } - } else { - fail("Could not copy attachments: " + copyResponse); - } - } else { - fail("Could not fetch objects Ids for all attachments"); - } - } else { - fail("Could not edit entities"); - } - } + // // Verify attachment content can be read from target entity + // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + // String readResponse = + // api.readAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - @Test - @Order(41) - void testCopyAttachmentsUnsuccessfulExistingEntity() throws IOException { - System.out.println( - "Test (41): Copy attachments from one entity to another existing entity - unsuccessful"); - String editResponse1 = - api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - String editResponse2 = - api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - if (editResponse1.equals("Entity in draft mode") - && editResponse2.equals("Entity in draft mode")) { - sourceObjectIds.add("incorrectObjectId"); - if (sourceObjectIds.size() == 3) { - try { - api.copyAttachment( - appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); - fail("Copy attachments did not throw an error"); - } catch (IOException e) { - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntity); - api.deleteEntity(appUrl, entityName, copyAttachmentSourceEntity); - } - } else { - fail("Could not fetch objects Ids for all attachments"); - } - } else { - fail("Could not edit entities"); - } - } + // if (readResponse.equals("OK")) { + // testStatus = true; + // } + // if (!testStatus) { + // fail("Could not verify that notes field was copied from source to target attachment"); + // } + // } - @Test - @Order(42) - void testCreateLinkSuccess() throws IOException { - System.out.println("Test (42): Create link in entity"); - List attachments = new ArrayList<>(); - createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!createLinkEntity.equals("Could not create entity")) { - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse1 = - api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - String createLinkResponse2 = - api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName + "1", linkUrl); - if (createLinkResponse1.equals("Link created successfully") - && createLinkResponse2.equals("Link created successfully")) { - String saveEntityResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (saveEntityResponse.equals("Saved")) { - attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - String openAttachmentResponse; - for (String attachment : attachments) { - openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, createLinkEntity, attachment); - System.out.println("openAttachmentResponse: " + openAttachmentResponse); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open created link"); - } - } - } else { - fail("Could not save entity"); - } - } else { - fail("Could not create link"); - } - } else { - fail("Could not create entity"); - } - } + // @Test + // @Order(38) + // void testCopyAttachmentWithSecondaryPropertiesField() throws IOException { + // System.out.println( + // "Test (38): Verify that secondary properties are preserved when copying attachments + // between entities"); + // Boolean testStatus = false; + + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // copyCustomSourceEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit source entity"); + // } - @Test - @Order(43) - void testCreateLinkDifferentEntity() throws IOException { - System.out.println("Test (43): Create link with same name in different entity"); - String createLinkDifferentEntity = - api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!createLinkDifferentEntity.equals("Could not edit entity")) { - String linkName = "sample"; - String linkUrl = "https://example.com"; - String createResponse = - api.createLink( - appUrl, entityName, facetName, createLinkDifferentEntity, linkName, linkUrl); - if (!createResponse.equals("Link created successfully")) { - fail("Could not create link in different entity with same name"); - } - String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkDifferentEntity); - if (!response.equals("Saved")) { - fail("Could not save entity"); - } - response = api.deleteEntity(appUrl, entityName, createLinkDifferentEntity); - if (!response.equals("Entity Deleted")) { - fail("Could not delete entity"); - } - } else { - fail("Could not edit entity"); - } - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample1.pdf").getFile()); - @Test - @Order(44) - void testCreateLinkFailure() throws IOException { - System.out.println("Test (44): Create link fails due to invalid URL and name"); - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Could not edit entity")) { - String linkName = "sample"; - String linkUrl = "example.com"; - try { - api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - fail("Create link did not throw an error for invalid url"); - } catch (IOException e) { - String message = e.getMessage(); - int jsonStart = message.indexOf("{"); - String jsonPart = message.substring(jsonStart); - JSONObject json = new JSONObject(jsonPart); - String errorCode = json.getJSONObject("error").getString("code"); - String errorMessage = json.getJSONObject("error").getString("message"); - assertEquals("400018", errorCode); - assertTrue( - errorMessage.equals("Enter a value that is within the expected pattern.") - || errorMessage.equals("Enter a value that matches the expected pattern."), - "Unexpected error message: " + errorMessage); - } - try { - api.createLink( - appUrl, entityName, facetName, createLinkEntity, linkName + "//", "https://" + linkUrl); - fail("Create link did not throw an error for invalid name"); - } catch (IOException e) { - String message = e.getMessage(); - int jsonStart = message.indexOf("{"); - String jsonPart = message.substring(jsonStart); - JSONObject json = new JSONObject(jsonPart); - String errorCode = json.getJSONObject("error").getString("code"); - String errorMessage = json.getJSONObject("error").getString("message"); - String expected = - "\"sample//\" contains unsupported characters (‘/’ or ‘\\’). Rename and try again."; - assertEquals("500", errorCode); - assertEquals( - expected.replaceAll("\\s+", " ").trim(), errorMessage.replaceAll("\\s+", " ").trim()); - } - try { - api.createLink(appUrl, entityName, facetName, createLinkEntity, "", ""); - fail("Create link did not throw an error for empty name and url"); - } catch (IOException e) { - String message = e.getMessage(); - int jsonStart = message.indexOf("{"); - String jsonPart = message.substring(jsonStart); - JSONObject json = new JSONObject(jsonPart); - String errorCode = json.getJSONObject("error").getString("code"); - String errorMessage = json.getJSONObject("error").getString("message"); - String expected = "Provide the missing value."; - assertEquals("409008", errorCode); - assertEquals(expected, errorMessage); - } - try { - api.createLink( - appUrl, entityName, facetName, createLinkEntity, linkName, "https://" + linkUrl); - fail("Create link did not throw an error for duplicate name"); - } catch (IOException e) { - String message = e.getMessage(); - int jsonStart = message.indexOf("{"); - String jsonPart = message.substring(jsonStart); - JSONObject json = new JSONObject(jsonPart); - String errorCode = json.getJSONObject("error").getString("code"); - String errorMessage = json.getJSONObject("error").getString("message"); - assertEquals("500", errorCode); - assertEquals( - "An object named \"sample\" already exists. Rename the object and try again.", - errorMessage); - } - try { - for (int i = 2; i < 5; i++) { - api.createLink( - appUrl, entityName, facetName, createLinkEntity, linkName + i, "https://" + linkUrl); - } - fail("More than 5 links were created in the same entity"); - } catch (IOException e) { - String message = e.getMessage(); - int jsonStart = message.indexOf("{"); - String jsonPart = message.substring(jsonStart); - JSONObject json = new JSONObject(jsonPart); - String errorCode = json.getJSONObject("error").getString("code"); - String errorMessage = json.getJSONObject("error").getString("message"); - assertEquals("500", errorCode); - assertEquals("Cannot upload more than 4 attachments.", errorMessage); - } - String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!response.equals("Saved")) { - fail("Could not save entity"); - } - response = api.deleteEntity(appUrl, entityName, createLinkEntity); - if (!response.equals("Entity Deleted")) { - fail("Could not delete entity"); - } - } else { - fail("Could not edit entity"); - } - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", copyCustomSourceEntity); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - @Test - @Order(45) - void testCreateLinkNoSDMRoles() throws IOException { - System.out.println("Test (45): Create link fails due to no SDM roles assigned"); - String createLinkEntityNoSDMRoles = - apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!createLinkEntityNoSDMRoles.equals("Could not edit entity")) { - String linkName = "sample27"; - String linkUrl = "https://example.com"; - try { - apiNoRoles.createLink( - appUrl, entityName, facetName, createLinkEntityNoSDMRoles, linkName, linkUrl); - fail("Link got created without SDM roles"); - } catch (IOException e) { - String message = e.getMessage(); - int jsonStart = message.indexOf("{"); - String jsonPart = message.substring(jsonStart); - JSONObject json = new JSONObject(jsonPart); - String errorCode = json.getJSONObject("error").getString("code"); - String errorMessage = json.getJSONObject("error").getString("message"); - assertEquals("500", errorCode); - assertEquals( - "You do not have the required permissions to upload attachments. Please contact your administrator for access.", - errorMessage); - } - String response = - apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntityNoSDMRoles); - if (!response.equals("Saved")) { - fail("Could not save entity"); - } - response = api.deleteEntity(appUrl, entityName, createLinkEntityNoSDMRoles); - if (!response.equals("Entity Deleted")) { - fail("Could not delete entity"); - } - } else { - fail("Could not edit entity"); - } - } + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - @Test - @Order(46) - void testDeleteLink() throws IOException { - System.out.println("Test (46): Delete link in entity"); - List attachments = new ArrayList<>(); - String createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!createLinkEntity.equals("Could not create entity")) { - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - if (createLinkResponse.equals("Link created successfully")) { - String saveEntityResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (saveEntityResponse.equals("Saved")) { - attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - String editEntityResponse = - api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } - String deleteLinkResponse = - api.deleteAttachment( - appUrl, entityName, facetName, createLinkEntity, attachments.get(0)); - if (!deleteLinkResponse.equals("Deleted")) { - fail("Could not delete created link"); - } else { - saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } - attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - if (attachments.size() != 0) { - fail("Link wasn't deleted"); - } - String response = api.deleteEntity(appUrl, entityName, createLinkEntity); - if (!response.equals("Entity Deleted")) { - fail("Could not delete entity"); - } - } - } else { - fail("Could not save entity"); - } - } else { - fail("Could not create link"); - } - } else { - fail("Could not create entity"); - } - } + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } - @Test - @Order(47) - void testRenameLinkSuccess() throws IOException { - System.out.println("Test (47): Rename link in entity"); - List attachments = new ArrayList<>(); + // String sourceAttachmentId = createResponse.get(1); + + // // Update attachment with secondary properties + // // DocumentInfoRecordBoolean : Set to true + // RequestBody bodyBoolean = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, + // bodyBoolean); + + // if (!updateSecondaryPropertyResponse1.equals("Updated")) { + // fail( + // "Could not update attachment DocumentInfoRecordBoolean field. Response: " + // + updateSecondaryPropertyResponse1); + // } - createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (createLinkEntity.equals("Could not create entity")) { - fail("Could not create entity"); - } + // // customProperty2 : Set to 12345 + // Integer customProperty2Value = 12345; + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty2\" : " + customProperty2Value + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyInt); + + // if (!updateSecondaryPropertyResponse2.equals("Updated")) { + // fail( + // "Could not update attachment customProperty2 field. Response: " + // + updateSecondaryPropertyResponse2); + // } - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link"); - } + // // Save source entity + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity. Response: " + saveSourceResponse); + // } - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // // Fetch attachment metadata to get objectId and verify secondary properties + // Map sourceAttachmentMetadata = + // api.fetchMetadata( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId"); + // } - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // // Store objectId in array for reuse + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // if (sourceObjectIds.size() < 2) { + // sourceObjectIds.add(sourceObjectId); + // } else { + // sourceObjectIds.set(1, sourceObjectId); + // } - attachmentID9 = attachments.get(0); - String renameLinkResponse = - api.renameAttachment( - appUrl, entityName, facetName, createLinkEntity, attachments.get(0), "sampleRenamed"); - if (!renameLinkResponse.equals("Renamed")) fail("Could not Renamed created link"); + // // Verify all secondary properties in source attachment + // Boolean sourceCustomProperty6 = + // sourceAttachmentMetadata.get("customProperty6") != null + // ? (Boolean) sourceAttachmentMetadata.get("customProperty6") + // : null; + // Integer sourceCustomProperty2 = + // sourceAttachmentMetadata.get("customProperty2") != null + // ? (Integer) sourceAttachmentMetadata.get("customProperty2") + // : null; + + // if (sourceCustomProperty6 == null || !sourceCustomProperty6) { + // fail( + // "DocumentInfoRecordBoolean was not properly set in source attachment. Expected: true, + // Got: " + // + sourceCustomProperty6); + // } - saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } - } + // if (!customProperty2Value.equals(sourceCustomProperty2)) { + // fail( + // "customProperty2 was not properly set in source attachment. Expected: " + // + customProperty2Value + // + ", Got: " + // + sourceCustomProperty2); + // } - @Test - @Order(48) - void testRenameLinkDuplicate() throws IOException { - System.out.println("Test (48): Rename link in entity fails due to duplicate error"); - List attachments = new ArrayList<>(); - - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // String editTargetResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!editTargetResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity"); + // } - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link"); - } + // // Copy attachment to target entity + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjectIds.get(1)); // Use objectId from array - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, + // objectIdsToCopy); - editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to target entity: " + copyResponse); + // } - attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - .filter(item -> !attachmentID9.equals(item.get("ID"))) // skip unwanted filename - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - attachmentID10 = attachments.get(0); - api.renameAttachment( - appUrl, entityName, facetName, createLinkEntity, attachments.get(0), "sampleRenamed"); - - String saveError = - saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - String expectedWarning = - "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; - ObjectMapper mapper = new ObjectMapper(); - assertEquals(mapper.readTree(expectedWarning), mapper.readTree(saveError)); - - String deleteEntityResponse = api.deleteEntityDraft(appUrl, entityName, createLinkEntity); - if (!deleteEntityResponse.equals("Entity Draft Deleted")) { - fail("Entity draft not deleted"); - } - } + // // Save target entity + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity"); + // } - @Test - @Order(49) - void testRenameLinkUnsupportedCharacters() throws IOException { - System.out.println( - "Test (49): Rename link in entity fails due to unsupported characters in name"); - List attachments = new ArrayList<>(); + // // Fetch target entity attachments metadata + // List> targetAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // if (targetAttachmentsMetadata.isEmpty()) { + // fail("No attachments found in target entity"); + // } - String linkName = "sample2"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link"); - } + // // Verify the copied attachment has the same secondary properties + // // Find the attachment we just copied by matching the filename + // Map copiedAttachmentMetadata = + // targetAttachmentsMetadata.stream() + // .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) + // .findFirst() + // .orElse(null); - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } + // if (copiedAttachmentMetadata == null) { + // fail("Could not find the copied attachment with file in target entity"); + // } - attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .filter(item -> "sample2".equals(item.get("filename"))) // skip unwanted filename - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - System.out.println("attachments: " + attachments); - - editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } + // Boolean copiedCustomProperty6 = + // copiedAttachmentMetadata.get("customProperty6") != null + // ? (Boolean) copiedAttachmentMetadata.get("customProperty6") + // : null; + // Integer copiedCustomProperty2 = + // copiedAttachmentMetadata.get("customProperty2") != null + // ? (Integer) copiedAttachmentMetadata.get("customProperty2") + // : null; + + // // Verify DocumentInfoRecordBoolean + // if (copiedCustomProperty6 == null || !copiedCustomProperty6) { + // fail( + // "DocumentInfoRecordBoolean as not properly copied. Expected: true, Got: " + // + copiedCustomProperty6); + // } - api.renameAttachment( - appUrl, entityName, facetName, createLinkEntity, attachments.get(0), "sampleRenamed//"); - String warning = - saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - String expectedWarning = - "{\"error\":{\"code\":\"400\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; - ObjectMapper mapper = new ObjectMapper(); - assertEquals(mapper.readTree(expectedWarning), mapper.readTree(warning)); - - String deleteEntityResponse = api.deleteEntity(appUrl, entityName, createLinkEntity); - if (!deleteEntityResponse.equals("Entity Deleted")) { - fail("Entity draft not deleted"); - } - } + // // Verify customProperty2 + // if (!customProperty2Value.equals(copiedCustomProperty2)) { + // fail( + // "customProperty2 was not properly copied. Expected: " + // + customProperty2Value + // + ", Got: " + // + copiedCustomProperty2); + // } - @Test - @Order(50) - void testEditLinkSuccess() throws IOException { - System.out.println("Test (50): Edit existing link in entity"); + // // Verify attachment content can be read from target entity + // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + // String readResponse = + // api.readAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - List attachments = new ArrayList<>(); - editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (editLinkEntity.equals("Could not create entity")) { - fail("Could not create entity"); - } - String linkName = "sample"; - String linkUrl = "https://www.example.com"; + // if (readResponse.equals("OK")) { + // testStatus = true; + // } + // if (!testStatus) { + // fail( + // "Could not verify that all secondary properties were copied from source to target + // attachment"); + // } + // } - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link"); - } + // @Test + // @Order(39) + // void testCopyAttachmentWithNotesAndSecondaryPropertiesField() throws IOException { + // System.out.println( + // "Test (39): Verify that both notes field and secondary properties are preserved during + // attachment copy"); + // Boolean testStatus = false; + + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // copyCustomSourceEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit source entity"); + // } - String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } - attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - if (attachments.isEmpty()) { - fail("Could not edit link"); - } - String linkId = attachments.get(0); - String updatedUrl = "https://editedexample.com"; - String editLinkResponse = - api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - if (!editLinkResponse.equals("Link edited successfully")) { - fail("Could not edit link"); - } - saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!saveEntityResponse.equals("Saved")) { - fail("Could not save entity"); - } - String openAttachmentResponse; - for (String attachment : attachments) { - openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, editLinkEntity, attachment); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open created link"); - } - } - } + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample2.pdf").getFile()); - @Test - @Order(51) - void testEditLinkFailureInvalidURL() throws IOException { - System.out.println("Test (51): Edit existing link with invalid url"); - Boolean testStatus = false; - List attachments = new ArrayList<>(); + // Map postData = new HashMap<>(); + // postData.put("up__ID", copyCustomSourceEntity); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } - attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - if (attachments.isEmpty()) { - fail("Could not edit link"); - } - String linkId = attachments.get(0); - String updatedUrl = "https://editedexample"; - try { - - api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - fail("Create link did not throw an error for invalid url"); - } catch (IOException e) { - String message = e.getMessage(); - int jsonStart = message.indexOf("{"); - String jsonPart = message.substring(jsonStart); - JSONObject json = new JSONObject(jsonPart); - String errorCode = json.getJSONObject("error").getString("code"); - String errorMessage = json.getJSONObject("error").getString("message"); - assertEquals("400018", errorCode); - assertTrue( - errorMessage.equals("Enter a value that is within the expected pattern.") - || errorMessage.equals("Enter a value that matches the expected pattern."), - "Unexpected error message: " + errorMessage); + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment"); + // } + + // String sourceAttachmentId = createResponse.get(1); + + // // Update attachment with notes field + // String notesValue = "This attachment has both notes and secondary properties for testing"; + // MediaType mediaType = MediaType.parse("application/json"); + // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + // RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); + + // String updateNotesResponse = + // api.updateSecondaryProperty( + // appUrl, + // entityName, + // facetName, + // copyCustomSourceEntity, + // sourceAttachmentId, + // updateNotesBody); + + // if (!updateNotesResponse.equals("Updated")) { + // fail("Could not update attachment notes field"); + // } - testStatus = true; - } - api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!testStatus) { - fail("Could not edit link with an invalid URL"); - } - } + // // Update attachment with secondary properties + // // DocumentInfoRecordBoolean : Set to true + // RequestBody bodyBoolean = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + // String updateSecondaryPropertyResponse1 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, + // bodyBoolean); + + // if (!updateSecondaryPropertyResponse1.equals("Updated")) { + // fail( + // "Could not update attachment DocumentInfoRecordBoolean (customProperty6) field. + // Response: " + // + updateSecondaryPropertyResponse1); + // } - @Test - @Order(52) - void testEditLinkFailureEmptyURL() throws IOException { - System.out.println("Test (52): Edit existing link with an empty url"); - Boolean testStatus = false; - List attachments = new ArrayList<>(); + // // customProperty2 : Set to 99999 + // Integer customProperty2Value = 99999; + // RequestBody bodyInt = + // RequestBody.create( + // MediaType.parse("application/json"), + // ByteString.encodeUtf8("{\n \"customProperty2\" : " + customProperty2Value + "\n}")); + // String updateSecondaryPropertyResponse2 = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyInt); + + // if (!updateSecondaryPropertyResponse2.equals("Updated")) { + // fail( + // "Could not update attachment customProperty2 field. Response: " + // + updateSecondaryPropertyResponse2); + // } - String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } - attachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - if (attachments.isEmpty()) { - fail("Could not edit link"); - } - String linkId = attachments.get(0); - String updatedUrl = ""; - try { - api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - fail("edit link did not throw an error for empty url"); - } catch (IOException e) { - String message = e.getMessage(); - int jsonStart = message.indexOf("{"); - String jsonPart = message.substring(jsonStart); - JSONObject json = new JSONObject(jsonPart); - String errorCode = json.getJSONObject("error").getString("code"); - String errorMessage = json.getJSONObject("error").getString("message"); - String expected = "Provide the missing value."; - assertEquals("409008", errorCode); - assertEquals(expected, errorMessage); - testStatus = true; - } - api.deleteEntityDraft(appUrl, entityName, editLinkEntity); - if (!testStatus) { - fail("Could not edit link with an empty URL"); - } - } + // // Save source entity + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity. Response: " + saveSourceResponse); + // } - @Test - @Order(53) - void testEditLinkNoSDMRoles() throws IOException { - System.out.println("Test (53): Edit link fails due to no SDM roles assigned"); + // // Fetch attachment metadata to get objectId and verify notes and secondary properties + // Map sourceAttachmentMetadata = + // api.fetchMetadata( + // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - Boolean testStatus = false; - List attachments = new ArrayList<>(); + // if (!sourceAttachmentMetadata.containsKey("objectId")) { + // fail("Source attachment metadata does not contain objectId"); + // } - String editEntityResponse = - apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - if (!editEntityResponse.equals("Entity in draft mode")) { - fail("Could not edit entity"); - } - attachments = - apiNoRoles.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - - if (attachments.isEmpty()) { - fail("Could not edit link"); - } - String linkId = attachments.get(0); - String updatedUrl = "https://www.example1.com"; - try { - apiNoRoles.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - fail("Link got edited without SDM roles in facet: \" + facetName"); - } catch (IOException e) { - String message = e.getMessage(); - int jsonStart = message.indexOf("{"); - String jsonPart = message.substring(jsonStart); - JSONObject json = new JSONObject(jsonPart); - String errorCode = json.getJSONObject("error").getString("code"); - String errorMessage = json.getJSONObject("error").getString("message"); - assertEquals("500", errorCode); - assertEquals( - "You do not have the required permissions to update attachments. Kindly contact the admin", - errorMessage); - testStatus = true; - } - apiNoRoles.deleteEntity(appUrl, entityName, createLinkEntity); - if (!testStatus) { - fail("Link got edited without SDM roles"); - } - api.deleteEntity(appUrl, entityName, editLinkEntity); - } + // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + // if (sourceObjectIds.size() < 3) { + // sourceObjectIds.add(sourceObjectId); + // } else { + // sourceObjectIds.set(2, sourceObjectId); + // } - @Test - @Order(54) - void testCopyLinkSuccessNewEntity() throws IOException { - System.out.println("Test (54): Copy link from one entity to another new entity"); - List> attachmentsMetadata = new ArrayList<>(); + // String sourceNoteValue = + // sourceAttachmentMetadata.get("note") != null + // ? sourceAttachmentMetadata.get("note").toString() + // : null; + + // if (!notesValue.equals(sourceNoteValue)) { + // fail( + // "Notes field was not properly set in source attachment. Expected: " + // + notesValue + // + ", Got: " + // + sourceNoteValue); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // Boolean sourceCustomProperty6 = + // sourceAttachmentMetadata.get("customProperty6") != null + // ? (Boolean) sourceAttachmentMetadata.get("customProperty6") + // : null; + // Integer sourceCustomProperty2 = + // sourceAttachmentMetadata.get("customProperty2") != null + // ? (Integer) sourceAttachmentMetadata.get("customProperty2") + // : null; + + // if (sourceCustomProperty6 == null || !sourceCustomProperty6) { + // fail( + // "DocumentInfoRecordBoolean was not properly set in source attachment. Expected: true, + // Got: " + // + sourceCustomProperty6); + // } - if (copyLinkSourceEntity.equals("Could not create entity") - || copyLinkTargetEntity.equals("Could not create entity")) { - fail("Could not create source or target entity"); - } + // if (!customProperty2Value.equals(sourceCustomProperty2)) { + // fail( + // "customProperty2 was not properly set in source attachment. Expected: " + // + customProperty2Value + // + ", Got: " + // + sourceCustomProperty2); + // } - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in source entity"); - } + // String editTargetResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!editTargetResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity"); + // } - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // // Copy attachment to target entity + // List objectIdsToCopy = new ArrayList<>(); + // objectIdsToCopy.add(sourceObjectIds.get(2)); // Use objectId from array - List sourceObjectIds = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - .map(item -> (String) item.get("objectId")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, + // objectIdsToCopy); - if (sourceObjectIds.isEmpty()) { - fail("Could not fetch object Id for link"); - } + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy attachment to target entity: " + copyResponse); + // } - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy link: " + copyResponse); - } + // // Save target entity + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity"); + // } - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!saveResponse.equals("Saved")) { - fail("Could not save target entity after copying link"); - } + // // Fetch target entity attachments metadata + // List> targetAttachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - attachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - Map copiedAttachment = attachmentsMetadata.get(0); - String receivedType = (String) copiedAttachment.get("type"); - String receivedUrl = (String) copiedAttachment.get("linkUrl"); - - String expectedType = "sap-icon://internet-browser"; - assertTrue( - expectedType.equalsIgnoreCase(receivedType), - "Attachment type mismatch. Expected '" - + expectedType - + "' but got '" - + receivedType - + "'."); - - assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); - - System.out.println("Attachment type and URL validated successfully."); - - String attachmentId = (String) copiedAttachment.get("ID"); - String openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open the attachment"); - } + // if (targetAttachmentsMetadata.isEmpty()) { + // fail("No attachments found in target entity"); + // } - String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - if (!deleteSourceResponse.equals("Entity Deleted") - || !deleteTargetResponse.equals("Entity Deleted")) { - fail("could not delete source or target entity"); - } - } + // // Verify the copied attachment has the same notes and secondary properties + // // Find the attachment we just copied by matching the filename + // Map copiedAttachmentMetadata = + // targetAttachmentsMetadata.stream() + // .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) + // .findFirst() + // .orElse(null); - @Test - @Order(55) - void testCopyLinkUnsuccessfulNewEntity() throws IOException { - System.out.println( - "Test (55): Copy invalid type of link from one entity to another new entity"); + // if (copiedAttachmentMetadata == null) { + // fail("Could not find the copied attachment with fil in target entity"); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // // Verify notes field was copied + // String copiedNoteValue = + // copiedAttachmentMetadata.get("note") != null + // ? copiedAttachmentMetadata.get("note").toString() + // : null; + + // if (!notesValue.equals(copiedNoteValue)) { + // fail( + // "Notes field was not properly copied. Expected: " + // + notesValue + // + ", Got: " + // + copiedNoteValue); + // } - if (copyLinkSourceEntity.equals("Could not create entity") - || copyLinkTargetEntity.equals("Could not create entity")) { - fail("Could not create source or target entity"); - } + // // Verify secondary properties were copied + // Boolean copiedCustomProperty6 = + // copiedAttachmentMetadata.get("customProperty6") != null + // ? (Boolean) copiedAttachmentMetadata.get("customProperty6") + // : null; + // Integer copiedCustomProperty2 = + // copiedAttachmentMetadata.get("customProperty2") != null + // ? (Integer) copiedAttachmentMetadata.get("customProperty2") + // : null; + + // // Verify DocumentInfoRecordBoolean + // if (copiedCustomProperty6 == null || !copiedCustomProperty6) { + // fail( + // "DocumentInfoRecordBoolean was not properly copied. Expected: true, Got: " + // + copiedCustomProperty6); + // } - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - List invalidObjectIds = Collections.singletonList("incorrectObjectId"); + // // Verify customProperty2 + // if (!customProperty2Value.equals(copiedCustomProperty2)) { + // fail( + // "customProperty2 was not properly copied. Expected: " + // + customProperty2Value + // + ", Got: " + // + copiedCustomProperty2); + // } - try { - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, invalidObjectIds); - fail("Copy attachments did not throw error for invalid ID"); - } catch (IOException e) { - System.out.println("Caught expected error: " + e.getMessage()); - } + // // Verify attachment content can be read from target entity + // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + // String readResponse = + // api.readAttachment( + // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!saveResponse.equals("Saved")) { - fail("Could not save target entity after unsuccessful copy"); - } + // if (readResponse.equals("OK")) { + // testStatus = true; + // } + // if (!testStatus) { + // fail( + // "Could not verify that notes field and all secondary properties were copied from source + // to target attachment"); + // } + // api.deleteEntity(appUrl, entityName, copyCustomSourceEntity); + // api.deleteEntity(appUrl, entityName, copyCustomTargetEntity); + // } - String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - if (!deleteSourceResponse.equals("Entity Deleted")) { - fail("Could not delete source entity"); - } - } + // @Test + // @Order(40) + // void testCopyAttachmentsSuccessExistingEntity() throws IOException { + // System.out.println("Test (40): Copy attachments from one entity to another existing entity"); + // List attachments = new ArrayList<>(); + // ClassLoader classLoader = getClass().getClassLoader(); + // List files = new ArrayList<>(); + // File file1 = new File(classLoader.getResource("sample.pdf").getFile()); + // File file2 = new File(classLoader.getResource("sample1.pdf").getFile()); + // File tempFile1 = new File(System.getProperty("java.io.tmpdir"), + // "sample_copy_existing_1.pdf"); + // Files.copy(file1.toPath(), tempFile1.toPath(), StandardCopyOption.REPLACE_EXISTING); + // File tempFile2 = new File(System.getProperty("java.io.tmpdir"), + // "sample_copy_existing_2.pdf"); + // Files.copy(file2.toPath(), tempFile2.toPath(), StandardCopyOption.REPLACE_EXISTING); + // files.add(tempFile1); + // files.add(tempFile2); + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID7); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + // String editResponse1 = + // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + // String editResponse2 = + // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // if (editResponse1.equals("Entity in draft mode") + // && editResponse2.equals("Entity in draft mode")) { + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyAttachmentSourceEntity, srvpath, postData, + // file); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment"); + // } + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + // List> attachmentsMetadata = new ArrayList<>(); + // Map fetchAttachmentMetadataResponse; + // for (String attachment : attachments) { + // try { + // fetchAttachmentMetadataResponse = + // api.fetchMetadata( + // appUrl, entityName, facetName, copyAttachmentSourceEntity, attachment); + // attachmentsMetadata.add(fetchAttachmentMetadataResponse); + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } - @Test - @Order(56) - void testCopyLinkFromNewEntityToExistingEntity() throws IOException { - System.out.println("Test (56): Copy link from a new entity to an existing target entity"); - List> attachmentsMetadata = new ArrayList<>(); - - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyLinkSourceEntity.equals("Could not create entity")) { - fail("Could not create new source entity"); - } + // sourceObjectIds.clear(); + // for (Map metadata : attachmentsMetadata) { + // if (metadata.containsKey("objectId")) { + // sourceObjectIds.add(metadata.get("objectId").toString()); + // } else { + // fail("Attachment metadata does not contain objectId"); + // } + // } - String linkName = "Sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in new source entity"); - } + // if (sourceObjectIds.size() == 2) { + // String copyResponse; + // copyResponse = + // api.copyAttachment( + // appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); + // if (copyResponse.equals("Attachments copied successfully")) { + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // if (saveEntityResponse.equals("Saved")) { + // List> fetchEntityMetadataResponse; + // fetchEntityMetadataResponse = + // api.fetchEntityMetadata(appUrl, entityName, facetName, + // copyAttachmentTargetEntity); + // targetAttachmentIds = + // fetchEntityMetadataResponse.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // String readResponse; + // if (targetAttachmentIds.size() == 4) { + // for (String targetAttachmentId : targetAttachmentIds) { + // readResponse = + // api.readAttachment( + // appUrl, + // entityName, + // facetName, + // copyAttachmentTargetEntity, + // targetAttachmentId); + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment"); + // } + // } + // } + // // api.deleteEntity(appUrl, entityName, copyAttachmentSourceEntity); + // // api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntity); + // } else { + // fail("Could not save entity after copying attachments: " + saveEntityResponse); + // } + // } else { + // fail("Could not copy attachments: " + copyResponse); + // } + // } else { + // fail("Could not fetch objects Ids for all attachments"); + // } + // } else { + // fail("Could not edit entities"); + // } + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save new source entity"); - } + // @Test + // @Order(41) + // void testCopyAttachmentsUnsuccessfulExistingEntity() throws IOException { + // System.out.println( + // "Test (41): Copy attachments from one entity to another existing entity - unsuccessful"); + // String editResponse1 = + // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + // String editResponse2 = + // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // if (editResponse1.equals("Entity in draft mode") + // && editResponse2.equals("Entity in draft mode")) { + // sourceObjectIds.add("incorrectObjectId"); + // if (sourceObjectIds.size() == 3) { + // try { + // api.copyAttachment( + // appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); + // fail("Copy attachments did not throw an error"); + // } catch (IOException e) { + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntity); + // api.deleteEntity(appUrl, entityName, copyAttachmentSourceEntity); + // } + // } else { + // fail("Could not fetch objects Ids for all attachments"); + // } + // } else { + // fail("Could not edit entities"); + // } + // } - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } + // @Test + // @Order(42) + // void testCreateLinkSuccess() throws IOException { + // System.out.println("Test (42): Create link in entity"); + // List attachments = new ArrayList<>(); + // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!createLinkEntity.equals("Could not create entity")) { + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse1 = + // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + // String createLinkResponse2 = + // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName + "1", + // linkUrl); + // if (createLinkResponse1.equals("Link created successfully") + // && createLinkResponse2.equals("Link created successfully")) { + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (saveEntityResponse.equals("Saved")) { + // attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // String openAttachmentResponse; + // for (String attachment : attachments) { + // openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, createLinkEntity, attachment); + // System.out.println("openAttachmentResponse: " + openAttachmentResponse); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open created link"); + // } + // } + // } else { + // fail("Could not save entity"); + // } + // } else { + // fail("Could not create link"); + // } + // } else { + // fail("Could not create entity"); + // } + // } - List sourceObjectIds = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - .map(item -> (String) item.get("objectId")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // @Test + // @Order(43) + // void testCreateLinkDifferentEntity() throws IOException { + // System.out.println("Test (43): Create link with same name in different entity"); + // String createLinkDifferentEntity = + // api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!createLinkDifferentEntity.equals("Could not edit entity")) { + // String linkName = "sample"; + // String linkUrl = "https://example.com"; + // String createResponse = + // api.createLink( + // appUrl, entityName, facetName, createLinkDifferentEntity, linkName, linkUrl); + // if (!createResponse.equals("Link created successfully")) { + // fail("Could not create link in different entity with same name"); + // } + // String response = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkDifferentEntity); + // if (!response.equals("Saved")) { + // fail("Could not save entity"); + // } + // response = api.deleteEntity(appUrl, entityName, createLinkDifferentEntity); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete entity"); + // } + // } else { + // fail("Could not edit entity"); + // } + // } - if (sourceObjectIds.isEmpty()) { - fail("Could not fetch objectId from new source entity"); - } + // @Test + // @Order(44) + // void testCreateLinkFailure() throws IOException { + // System.out.println("Test (44): Create link fails due to invalid URL and name"); + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!editEntityResponse.equals("Could not edit entity")) { + // String linkName = "sample"; + // String linkUrl = "example.com"; + // try { + // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + // fail("Create link did not throw an error for invalid url"); + // } catch (IOException e) { + // String message = e.getMessage(); + // int jsonStart = message.indexOf("{"); + // String jsonPart = message.substring(jsonStart); + // JSONObject json = new JSONObject(jsonPart); + // String errorCode = json.getJSONObject("error").getString("code"); + // String errorMessage = json.getJSONObject("error").getString("message"); + // assertEquals("400018", errorCode); + // assertTrue( + // errorMessage.equals("Enter a value that is within the expected pattern.") + // || errorMessage.equals("Enter a value that matches the expected pattern."), + // "Unexpected error message: " + errorMessage); + // } + // try { + // api.createLink( + // appUrl, entityName, facetName, createLinkEntity, linkName + "//", "https://" + + // linkUrl); + // fail("Create link did not throw an error for invalid name"); + // } catch (IOException e) { + // String message = e.getMessage(); + // int jsonStart = message.indexOf("{"); + // String jsonPart = message.substring(jsonStart); + // JSONObject json = new JSONObject(jsonPart); + // String errorCode = json.getJSONObject("error").getString("code"); + // String errorMessage = json.getJSONObject("error").getString("message"); + // String expected = + // "\"sample//\" contains unsupported characters (‘/’ or ‘\\’). Rename and try again."; + // assertEquals("500", errorCode); + // assertEquals( + // expected.replaceAll("\\s+", " ").trim(), errorMessage.replaceAll("\\s+", " + // ").trim()); + // } + // try { + // api.createLink(appUrl, entityName, facetName, createLinkEntity, "", ""); + // fail("Create link did not throw an error for empty name and url"); + // } catch (IOException e) { + // String message = e.getMessage(); + // int jsonStart = message.indexOf("{"); + // String jsonPart = message.substring(jsonStart); + // JSONObject json = new JSONObject(jsonPart); + // String errorCode = json.getJSONObject("error").getString("code"); + // String errorMessage = json.getJSONObject("error").getString("message"); + // String expected = "Provide the missing value."; + // assertEquals("409008", errorCode); + // assertEquals(expected, errorMessage); + // } + // try { + // api.createLink( + // appUrl, entityName, facetName, createLinkEntity, linkName, "https://" + linkUrl); + // fail("Create link did not throw an error for duplicate name"); + // } catch (IOException e) { + // String message = e.getMessage(); + // int jsonStart = message.indexOf("{"); + // String jsonPart = message.substring(jsonStart); + // JSONObject json = new JSONObject(jsonPart); + // String errorCode = json.getJSONObject("error").getString("code"); + // String errorMessage = json.getJSONObject("error").getString("message"); + // assertEquals("500", errorCode); + // assertEquals( + // "An object named \"sample\" already exists. Rename the object and try again.", + // errorMessage); + // } + // try { + // for (int i = 2; i < 5; i++) { + // api.createLink( + // appUrl, entityName, facetName, createLinkEntity, linkName + i, "https://" + + // linkUrl); + // } + // fail("More than 5 links were created in the same entity"); + // } catch (IOException e) { + // String message = e.getMessage(); + // int jsonStart = message.indexOf("{"); + // String jsonPart = message.substring(jsonStart); + // JSONObject json = new JSONObject(jsonPart); + // String errorCode = json.getJSONObject("error").getString("code"); + // String errorMessage = json.getJSONObject("error").getString("message"); + // assertEquals("500", errorCode); + // assertEquals("Cannot upload more than 4 attachments.", errorMessage); + // } + // String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!response.equals("Saved")) { + // fail("Could not save entity"); + // } + // response = api.deleteEntity(appUrl, entityName, createLinkEntity); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete entity"); + // } + // } else { + // fail("Could not edit entity"); + // } + // } - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy link from new source entity to existing target entity: " + copyResponse); - } + // @Test + // @Order(45) + // void testCreateLinkNoSDMRoles() throws IOException { + // System.out.println("Test (45): Create link fails due to no SDM roles assigned"); + // String createLinkEntityNoSDMRoles = + // apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!createLinkEntityNoSDMRoles.equals("Could not edit entity")) { + // String linkName = "sample27"; + // String linkUrl = "https://example.com"; + // try { + // apiNoRoles.createLink( + // appUrl, entityName, facetName, createLinkEntityNoSDMRoles, linkName, linkUrl); + // fail("Link got created without SDM roles"); + // } catch (IOException e) { + // String message = e.getMessage(); + // int jsonStart = message.indexOf("{"); + // String jsonPart = message.substring(jsonStart); + // JSONObject json = new JSONObject(jsonPart); + // String errorCode = json.getJSONObject("error").getString("code"); + // String errorMessage = json.getJSONObject("error").getString("message"); + // assertEquals("500", errorCode); + // assertEquals( + // "You do not have the required permissions to upload attachments. Please contact your + // administrator for access.", + // errorMessage); + // } + // String response = + // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntityNoSDMRoles); + // if (!response.equals("Saved")) { + // fail("Could not save entity"); + // } + // response = api.deleteEntity(appUrl, entityName, createLinkEntityNoSDMRoles); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete entity"); + // } + // } else { + // fail("Could not edit entity"); + // } + // } - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // @Test + // @Order(46) + // void testDeleteLink() throws IOException { + // System.out.println("Test (46): Delete link in entity"); + // List attachments = new ArrayList<>(); + // String createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!createLinkEntity.equals("Could not create entity")) { + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + // if (createLinkResponse.equals("Link created successfully")) { + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (saveEntityResponse.equals("Saved")) { + // attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // String editEntityResponse = + // api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } + // String deleteLinkResponse = + // api.deleteAttachment( + // appUrl, entityName, facetName, createLinkEntity, attachments.get(0)); + // if (!deleteLinkResponse.equals("Deleted")) { + // fail("Could not delete created link"); + // } else { + // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } + // attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // if (attachments.size() != 0) { + // fail("Link wasn't deleted"); + // } + // String response = api.deleteEntity(appUrl, entityName, createLinkEntity); + // if (!response.equals("Entity Deleted")) { + // fail("Could not delete entity"); + // } + // } + // } else { + // fail("Could not save entity"); + // } + // } else { + // fail("Could not create link"); + // } + // } else { + // fail("Could not create entity"); + // } + // } - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity after copying link"); - } + // @Test + // @Order(47) + // void testRenameLinkSuccess() throws IOException { + // System.out.println("Test (47): Rename link in entity"); + // List attachments = new ArrayList<>(); - attachmentsMetadata = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - Map copiedAttachment = attachmentsMetadata.get(0); - String receivedType = (String) copiedAttachment.get("type"); - String receivedUrl = (String) copiedAttachment.get("linkUrl"); + // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (createLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } - String expectedType = "sap-icon://internet-browser"; - assertTrue( - expectedType.equalsIgnoreCase(receivedType), - "Attachment type mismatch. Expected '" - + expectedType - + "' but got '" - + receivedType - + "'."); + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link"); + // } - assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - System.out.println("Attachment type and URL validated successfully."); + // attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - String attachmentId = (String) copiedAttachment.get("ID"); - assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - String openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open the attachment"); - } + // attachmentID9 = attachments.get(0); + // String renameLinkResponse = + // api.renameAttachment( + // appUrl, entityName, facetName, createLinkEntity, attachments.get(0), + // "sampleRenamed"); + // if (!renameLinkResponse.equals("Renamed")) fail("Could not Renamed created link"); - String deleteResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - if (!deleteResponse.equals("Entity Deleted")) { - fail("Could not delete new source entity"); - } - } + // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } + // } - @Test - @Order(57) - void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { - System.out.println( - "Test (57): Copy invalid type of link from new entity to existing target entity"); + // @Test + // @Order(48) + // void testRenameLinkDuplicate() throws IOException { + // System.out.println("Test (48): Rename link in entity fails due to duplicate error"); + // List attachments = new ArrayList<>(); + + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (copyLinkSourceEntity.equals("Could not create entity")) { - fail("Could not create new source entity"); - } + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link"); + // } - String linkName = "Sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in new source entity"); - } + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - String saveSourceResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save new source entity"); - } + // editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!editResponse.equals("Entity in draft mode")) { - fail("Could not edit target entity draft"); - } + // attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .filter(item -> !attachmentID9.equals(item.get("ID"))) // skip unwanted filename + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // attachmentID10 = attachments.get(0); + // api.renameAttachment( + // appUrl, entityName, facetName, createLinkEntity, attachments.get(0), "sampleRenamed"); + + // String saveError = + // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // String expectedWarning = + // "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"sampleRenamed\\\" already + // exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\"}}"; + // ObjectMapper mapper = new ObjectMapper(); + // assertEquals(mapper.readTree(expectedWarning), mapper.readTree(saveError)); + + // String deleteEntityResponse = api.deleteEntityDraft(appUrl, entityName, createLinkEntity); + // if (!deleteEntityResponse.equals("Entity Draft Deleted")) { + // fail("Entity draft not deleted"); + // } + // } - List invalidObjectIds = Collections.singletonList("invalidObjectId123"); + // @Test + // @Order(49) + // void testRenameLinkUnsupportedCharacters() throws IOException { + // System.out.println( + // "Test (49): Rename link in entity fails due to unsupported characters in name"); + // List attachments = new ArrayList<>(); - try { - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, invalidObjectIds); - fail("Copy did not throw error for invalid link ID"); - } catch (IOException e) { - System.out.println("Caught expected error while copying invalid link: " + e.getMessage()); - } + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - // No need to wait for upload completion as copy failed, but ensure clean state - String saveTargetResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity after unsuccessful copy"); - } + // String linkName = "sample2"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link"); + // } - String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - if (!deleteSourceResponse.equals("Entity Deleted") - || !deleteTargetResponse.equals("Entity Deleted")) { - fail("Could not delete new source entity or target entity"); - } - } + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // createLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } - @Test - @Order(58) - void testCopyLinkSuccessNewEntityDraft() throws IOException { - System.out.println("Test (58): Copy link from one entity to another new entity draft mode"); + // attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // // .filter(item -> "sample2".equals(item.get("filename"))) // skip unwanted filename + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // System.out.println("attachments: " + attachments); + + // editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } - copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // api.renameAttachment( + // appUrl, entityName, facetName, createLinkEntity, attachments.get(0), "sampleRenamed//"); + // String warning = + // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + // String expectedWarning = + // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported + // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: + // IntegrationTestEntity\"}}"; + // ObjectMapper mapper = new ObjectMapper(); + // assertEquals(mapper.readTree(expectedWarning), mapper.readTree(warning)); + + // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, createLinkEntity); + // if (!deleteEntityResponse.equals("Entity Deleted")) { + // fail("Entity draft not deleted"); + // } + // } - if (copyLinkSourceEntity.equals("Could not create entity") - || copyLinkTargetEntity.equals("Could not create entity")) { - fail("Could not create source or target entity"); - } + // @Test + // @Order(50) + // void testEditLinkSuccess() throws IOException { + // System.out.println("Test (50): Edit existing link in entity"); - String linkName = "sample"; - String linkUrl = "https://www.example.com"; - String createLinkResponse = - api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); - if (!createLinkResponse.equals("Link created successfully")) { - fail("Could not create link in source entity"); - } + // List attachments = new ArrayList<>(); + // editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (editLinkEntity.equals("Could not create entity")) { + // fail("Could not create entity"); + // } + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; - List sourceObjectIds = - api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - .map(item -> (String) item.get("objectId")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link"); + // } - if (sourceObjectIds.isEmpty()) { - fail("Could not fetch object Id for link"); - } + // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } + // attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + + // if (attachments.isEmpty()) { + // fail("Could not edit link"); + // } + // String linkId = attachments.get(0); + // String updatedUrl = "https://editedexample.com"; + // String editLinkResponse = + // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + // if (!editLinkResponse.equals("Link edited successfully")) { + // fail("Could not edit link"); + // } + // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!saveEntityResponse.equals("Saved")) { + // fail("Could not save entity"); + // } + // String openAttachmentResponse; + // for (String attachment : attachments) { + // openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, editLinkEntity, attachment); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open created link"); + // } + // } + // } - String copyResponse = - api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - if (!copyResponse.equals("Attachments copied successfully")) { - fail("Could not copy link: " + copyResponse); - } + // @Test + // @Order(51) + // void testEditLinkFailureInvalidURL() throws IOException { + // System.out.println("Test (51): Edit existing link with invalid url"); + // Boolean testStatus = false; + // List attachments = new ArrayList<>(); + + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } + // attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + + // if (attachments.isEmpty()) { + // fail("Could not edit link"); + // } + // String linkId = attachments.get(0); + // String updatedUrl = "https://editedexample"; + // try { + + // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + // fail("Create link did not throw an error for invalid url"); + // } catch (IOException e) { + // String message = e.getMessage(); + // int jsonStart = message.indexOf("{"); + // String jsonPart = message.substring(jsonStart); + // JSONObject json = new JSONObject(jsonPart); + // String errorCode = json.getJSONObject("error").getString("code"); + // String errorMessage = json.getJSONObject("error").getString("message"); + // assertEquals("400018", errorCode); + // assertTrue( + // errorMessage.equals("Enter a value that is within the expected pattern.") + // || errorMessage.equals("Enter a value that matches the expected pattern."), + // "Unexpected error message: " + errorMessage); + + // testStatus = true; + // } + // api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!testStatus) { + // fail("Could not edit link with an invalid URL"); + // } + // } - List> attachmentsMetadata = new ArrayList<>(); - attachmentsMetadata = - api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyLinkTargetEntity); - Map copiedAttachment = attachmentsMetadata.get(0); - String receivedType = (String) copiedAttachment.get("type"); - String receivedUrl = (String) copiedAttachment.get("linkUrl"); - - String expectedType = "sap-icon://internet-browser"; - assertTrue( - expectedType.equalsIgnoreCase(receivedType), - "Attachment type mismatch. Expected '" - + expectedType - + "' but got '" - + receivedType - + "'."); - - assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); - - System.out.println("Attachment type and URL validated successfully."); - - String attachmentId = (String) copiedAttachment.get("ID"); - assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); - - String openAttachmentResponse = - api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); - if (!openAttachmentResponse.equals("Attachment opened successfully")) { - fail("Could not open the attachment"); - } + // @Test + // @Order(52) + // void testEditLinkFailureEmptyURL() throws IOException { + // System.out.println("Test (52): Edit existing link with an empty url"); + // Boolean testStatus = false; + // List attachments = new ArrayList<>(); + + // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } + // attachments = + // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + + // if (attachments.isEmpty()) { + // fail("Could not edit link"); + // } + // String linkId = attachments.get(0); + // String updatedUrl = ""; + // try { + // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + // fail("edit link did not throw an error for empty url"); + // } catch (IOException e) { + // String message = e.getMessage(); + // int jsonStart = message.indexOf("{"); + // String jsonPart = message.substring(jsonStart); + // JSONObject json = new JSONObject(jsonPart); + // String errorCode = json.getJSONObject("error").getString("code"); + // String errorMessage = json.getJSONObject("error").getString("message"); + // String expected = "Provide the missing value."; + // assertEquals("409008", errorCode); + // assertEquals(expected, errorMessage); + // testStatus = true; + // } + // api.deleteEntityDraft(appUrl, entityName, editLinkEntity); + // if (!testStatus) { + // fail("Could not edit link with an empty URL"); + // } + // } - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - if (!saveResponse.equals("Saved")) { - fail("Could not save target entity after copying link"); - } - api.deleteEntityDraft(appUrl, entityName, copyLinkSourceEntity); - api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - } + // @Test + // @Order(53) + // void testEditLinkNoSDMRoles() throws IOException { + // System.out.println("Test (53): Edit link fails due to no SDM roles assigned"); - @Test - @Order(59) - void testCopyAttachmentsSuccessNewEntityDraft() throws IOException { - System.out.println( - "Test (59): Copy attachments from one entity to another new entity draft mode"); - List attachments = new ArrayList<>(); - copyAttachmentSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - copyAttachmentTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (!copyAttachmentSourceEntity.equals("Could not create entity") - && !copyAttachmentTargetEntity.equals("Could not create entity")) { - ClassLoader classLoader = getClass().getClassLoader(); - List files = new ArrayList<>(); - files.add(new File(classLoader.getResource("sample.pdf").getFile())); - files.add(new File(classLoader.getResource("sample1.pdf").getFile())); - Map postData = new HashMap<>(); - postData.put("up__ID", entityID7); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // Boolean testStatus = false; + // List attachments = new ArrayList<>(); - sourceObjectIds.clear(); + // String editEntityResponse = + // apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + // if (!editEntityResponse.equals("Entity in draft mode")) { + // fail("Could not edit entity"); + // } + // attachments = + // apiNoRoles.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + + // if (attachments.isEmpty()) { + // fail("Could not edit link"); + // } + // String linkId = attachments.get(0); + // String updatedUrl = "https://www.example1.com"; + // try { + // apiNoRoles.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + // fail("Link got edited without SDM roles in facet: \" + facetName"); + // } catch (IOException e) { + // String message = e.getMessage(); + // int jsonStart = message.indexOf("{"); + // String jsonPart = message.substring(jsonStart); + // JSONObject json = new JSONObject(jsonPart); + // String errorCode = json.getJSONObject("error").getString("code"); + // String errorMessage = json.getJSONObject("error").getString("message"); + // assertEquals("500", errorCode); + // assertEquals( + // "You do not have the required permissions to update attachments. Kindly contact the + // admin", + // errorMessage); + // testStatus = true; + // } + // apiNoRoles.deleteEntity(appUrl, entityName, createLinkEntity); + // if (!testStatus) { + // fail("Link got edited without SDM roles"); + // } + // api.deleteEntity(appUrl, entityName, editLinkEntity); + // } - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, copyAttachmentSourceEntity, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - attachments.add(createResponse.get(1)); - } else { - fail("Could not create attachment"); - } - } + // @Test + // @Order(54) + // void testCopyLinkSuccessNewEntity() throws IOException { + // System.out.println("Test (54): Copy link from one entity to another new entity"); + // List> attachmentsMetadata = new ArrayList<>(); - List> attachmentsMetadata = new ArrayList<>(); - Map fetchAttachmentMetadataResponse; - for (String attachment : attachments) { - try { - fetchAttachmentMetadataResponse = - api.fetchMetadataDraft( - appUrl, entityName, facetName, copyAttachmentSourceEntity, attachment); - attachmentsMetadata.add(fetchAttachmentMetadataResponse); - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } - for (Map metadata : attachmentsMetadata) { - if (metadata.containsKey("objectId")) { - sourceObjectIds.add(metadata.get("objectId").toString()); - } else { - fail("Attachment metadata does not contain objectId"); - } - } + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (sourceObjectIds.size() == 2) { - String copyResponse; - copyResponse = - api.copyAttachment( - appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); - if (copyResponse.equals("Attachments copied successfully")) { - String saveEntityResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - if (saveEntityResponse.equals("Saved")) { - List> fetchEntityMetadataResponse; - fetchEntityMetadataResponse = - api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); - targetAttachmentIds = - fetchEntityMetadataResponse.stream() - .map(item -> (String) item.get("ID")) - .filter(Objects::nonNull) - .collect(Collectors.toList()); - String readResponse; - for (String targetAttachmentId : targetAttachmentIds) { - readResponse = - api.readAttachment( - appUrl, - entityName, - facetName, - copyAttachmentTargetEntity, - targetAttachmentId); - if (!readResponse.equals("OK")) { - fail("Could not read copied attachment"); - } - } - } else { - fail("Could not save entity after copying attachments: " + saveEntityResponse); - } - } else { - fail("Could not copy attachments: " + copyResponse); - } - } else { - fail("Could not fetch objects Ids for all attachments"); - } - } else { - fail("Could not create entities"); - } - api.deleteEntityDraft(appUrl, entityName, copyAttachmentSourceEntity); - api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntity); - } + // if (copyLinkSourceEntity.equals("Could not create entity") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not create source or target entity"); + // } - @Test - @Order(60) - void testViewChangelogForNewlyCreatedAttachment() throws IOException { - System.out.println("Test (60): View changelog for newly created attachment"); + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in source entity"); + // } - // Create a new entity for changelog test - changelogEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - assertNotNull(changelogEntityID, "Failed to create changelog test entity"); - assertNotEquals("Could not create entity", changelogEntityID); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // Prepare a sample file to upload - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.txt").getFile()); - assertTrue(file.exists(), "Sample file should exist"); + // List sourceObjectIds = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - // Create attachment - Map postData = new HashMap<>(); - postData.put("up__ID", changelogEntityID); - postData.put("mimeType", "text/plain"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // if (sourceObjectIds.isEmpty()) { + // fail("Could not fetch object Id for link"); + // } - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, changelogEntityID, srvpath, postData, file); + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy link: " + copyResponse); + // } - assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - String status = createResponse.get(0); - changelogAttachmentID = createResponse.get(1); + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target entity after copying link"); + // } - assertEquals("Attachment created", status, "Attachment should be created successfully"); - assertNotNull(changelogAttachmentID, "Attachment ID should not be null"); - assertNotEquals("", changelogAttachmentID, "Attachment ID should not be empty"); + // attachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + // Map copiedAttachment = attachmentsMetadata.get(0); + // String receivedType = (String) copiedAttachment.get("type"); + // String receivedUrl = (String) copiedAttachment.get("linkUrl"); + + // String expectedType = "sap-icon://internet-browser"; + // assertTrue( + // expectedType.equalsIgnoreCase(receivedType), + // "Attachment type mismatch. Expected '" + // + expectedType + // + "' but got '" + // + receivedType + // + "'."); + + // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); + + // System.out.println("Attachment type and URL validated successfully."); + + // String attachmentId = (String) copiedAttachment.get("ID"); + // String openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open the attachment"); + // } - // Fetch changelog for the newly created attachment - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); + // String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + // if (!deleteSourceResponse.equals("Entity Deleted") + // || !deleteTargetResponse.equals("Entity Deleted")) { + // fail("could not delete source or target entity"); + // } + // } - assertNotNull(changelogResponse, "Changelog response should not be null"); + // @Test + // @Order(55) + // void testCopyLinkUnsuccessfulNewEntity() throws IOException { + // System.out.println( + // "Test (55): Copy invalid type of link from one entity to another new entity"); - // Verify changelog structure - assertEquals(false, changelogResponse.get("hasMoreItems"), "hasMoreItems should be false"); - assertEquals( - "sample.txt", changelogResponse.get("filename"), "Filename should match uploaded file"); - assertNotNull(changelogResponse.get("objectId"), "ObjectId should not be null"); - assertEquals(1, changelogResponse.get("numItems"), "Should have 1 changelog entry"); - - // Verify the changelog entry - @SuppressWarnings("unchecked") - List> changeLogs = - (List>) changelogResponse.get("changeLogs"); - assertEquals(1, changeLogs.size(), "Should have exactly 1 changelog entry"); - - Map logEntry = changeLogs.get(0); - assertEquals("created", logEntry.get("operation"), "Operation should be 'created'"); - assertNotNull(logEntry.get("time"), "Time should not be null"); - assertNotNull(logEntry.get("user"), "User should not be null"); - assertFalse( - logEntry.containsKey("changeDetail"), "Created operation should not have changeDetail"); - } + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - @Test - @Order(61) - void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { - System.out.println( - "Test (61): Modify note field and custom property, then verify changelog shows created + 3 updated entries"); + // if (copyLinkSourceEntity.equals("Could not create entity") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not create source or target entity"); + // } - // Update attachment with notes field (entity is already in draft mode from test 60) - String notesValue = "Test note for changelog verification"; - MediaType mediaType = MediaType.parse("application/json"); - String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // List invalidObjectIds = Collections.singletonList("incorrectObjectId"); - String updateNotesResponse = - api.updateSecondaryProperty( - appUrl, - entityName, - facetName, - changelogEntityID, - changelogAttachmentID, - updateNotesBody); - assertEquals("Updated", updateNotesResponse, "Should successfully update notes field"); - - // Update attachment with custom property - Integer customProperty2Value = 12345; - RequestBody bodyInt = - RequestBody.create( - "{\"customProperty2\": " + customProperty2Value + "}", - MediaType.parse("application/json")); - String updateCustomPropertyResponse = - api.updateSecondaryProperty( - appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID, bodyInt); - assertEquals( - "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); + // try { + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, invalidObjectIds); + // fail("Copy attachments did not throw error for invalid ID"); + // } catch (IOException e) { + // System.out.println("Caught expected error: " + e.getMessage()); + // } - // Save the entity - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target entity after unsuccessful copy"); + // } - // Edit entity again to fetch changelog - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + // String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // if (!deleteSourceResponse.equals("Entity Deleted")) { + // fail("Could not delete source entity"); + // } + // } - // Fetch changelog after modifications - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); + // @Test + // @Order(56) + // void testCopyLinkFromNewEntityToExistingEntity() throws IOException { + // System.out.println("Test (56): Copy link from a new entity to an existing target entity"); + // List> attachmentsMetadata = new ArrayList<>(); + + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyLinkSourceEntity.equals("Could not create entity")) { + // fail("Could not create new source entity"); + // } - assertNotNull(changelogResponse, "Changelog response should not be null"); + // String linkName = "Sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in new source entity"); + // } - // Verify changelog content - should have 1 created + 3 updated (note, customProperty2, and - // internal update) - assertEquals(false, changelogResponse.get("hasMoreItems"), "hasMoreItems should be false"); - assertEquals( - 4, - changelogResponse.get("numItems"), - "Should have 4 changelog entries (1 created + 3 updated)"); + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save new source entity"); + // } - @SuppressWarnings("unchecked") - List> changeLogs = - (List>) changelogResponse.get("changeLogs"); - assertEquals(4, changeLogs.size(), "Should have exactly 4 changelog entries"); + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } - // Verify first entry is 'created' - Map createdEntry = changeLogs.get(0); - assertEquals( - "created", createdEntry.get("operation"), "First entry should be 'created' operation"); - - // Verify remaining entries are 'updated' - long updatedCount = - changeLogs.stream().filter(log -> "updated".equals(log.get("operation"))).count(); - assertEquals(3, updatedCount, "Should have 3 'updated' operations"); - - // Verify that changeDetail exists in updated entries for note field - boolean hasNoteUpdate = - changeLogs.stream() - .filter(log -> "updated".equals(log.get("operation"))) - .anyMatch( - log -> { - @SuppressWarnings("unchecked") - Map changeDetail = (Map) log.get("changeDetail"); - return changeDetail != null - && "cmis:description".equals(changeDetail.get("field")); - }); - assertTrue(hasNoteUpdate, "Should have an update entry for note field (cmis:description)"); - assertTrue(hasNoteUpdate, "Should have an update entry for note field (cmis:description)"); - - // Save the entity so test 62 can edit it - String saveResponseFinal = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - assertEquals("Saved", saveResponseFinal, "Entity should be saved successfully"); - } + // List sourceObjectIds = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - @Test - @Order(62) - void testChangelogAfterRenamingAttachment() throws IOException { - System.out.println( - "Test (62): Rename attachment and verify changelog increases with rename entry"); + // if (sourceObjectIds.isEmpty()) { + // fail("Could not fetch objectId from new source entity"); + // } - // Edit entity to put it in draft mode (entity was saved at end of test 61) - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy link from new source entity to existing target entity: " + + // copyResponse); + // } - // Rename the attachment - String newFileName = "renamed_sample.txt"; - String renameResponse = - api.renameAttachment( - appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID, newFileName); - assertEquals("Renamed", renameResponse, "Should successfully rename attachment"); + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // Save entity after rename - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity after copying link"); + // } - // Edit entity again and fetch changelog - editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + // attachmentsMetadata = + // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + // Map copiedAttachment = attachmentsMetadata.get(0); + // String receivedType = (String) copiedAttachment.get("type"); + // String receivedUrl = (String) copiedAttachment.get("linkUrl"); - // Fetch changelog after rename - Map changelogAfterRename = - api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); + // String expectedType = "sap-icon://internet-browser"; + // assertTrue( + // expectedType.equalsIgnoreCase(receivedType), + // "Attachment type mismatch. Expected '" + // + expectedType + // + "' but got '" + // + receivedType + // + "'."); - assertNotNull(changelogAfterRename, "Changelog response should not be null after rename"); + // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); - // Verify changelog has increased (rename operation adds 1 entry for cmis:name change) - // Expected: 1 created + 3 initial updates + 1 rename update = 5 total - assertEquals( - 5, changelogAfterRename.get("numItems"), "Should have 5 changelog entries after rename"); + // System.out.println("Attachment type and URL validated successfully."); - @SuppressWarnings("unchecked") - List> changeLogsAfterRename = - (List>) changelogAfterRename.get("changeLogs"); - assertEquals( - 5, changeLogsAfterRename.size(), "Should have exactly 5 changelog entries after rename"); - - // Verify updated count is 4 (3 initial + 1 from rename operation) - long updatedCountAfterRename = - changeLogsAfterRename.stream() - .filter(log -> "updated".equals(log.get("operation"))) - .count(); - assertEquals(4, updatedCountAfterRename, "Should have 4 'updated' operations after rename"); - - // Verify filename change in changelog - boolean hasFilenameUpdate = - changeLogsAfterRename.stream() - .filter(log -> "updated".equals(log.get("operation"))) - .anyMatch( - log -> { - @SuppressWarnings("unchecked") - Map changeDetail = (Map) log.get("changeDetail"); - return changeDetail != null && "cmis:name".equals(changeDetail.get("field")); - }); - assertTrue(hasFilenameUpdate, "Should have an update entry for filename (cmis:name)"); - - // Cleanup - entity was saved after rename, so delete the active entity - api.deleteEntity(appUrl, entityName, changelogEntityID); - } + // String attachmentId = (String) copiedAttachment.get("ID"); + // assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); - @Test - @Order(63) - void testChangelogWithCustomPropertyEditSave() throws IOException { - System.out.println( - "Test (63): Create entity with custom property, save, edit and save again - verify changelog remains at 3 entries"); + // String openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open the attachment"); + // } - // Create a new entity - String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - assertNotNull(newEntityID, "Failed to create new entity"); - assertNotEquals("Could not create entity", newEntityID); + // String deleteResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // if (!deleteResponse.equals("Entity Deleted")) { + // fail("Could not delete new source entity"); + // } + // } - // Prepare a sample file to upload - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - assertTrue(file.exists(), "Sample file should exist"); + // @Test + // @Order(57) + // void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { + // System.out.println( + // "Test (57): Copy invalid type of link from new entity to existing target entity"); - // Create attachment - Map postData = new HashMap<>(); - postData.put("up__ID", newEntityID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (copyLinkSourceEntity.equals("Could not create entity")) { + // fail("Could not create new source entity"); + // } - List createResponse = - api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); - - assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - String status = createResponse.get(0); - String attachmentID = createResponse.get(1); - - assertEquals("Attachment created", status, "Attachment should be created successfully"); - assertNotNull(attachmentID, "Attachment ID should not be null"); - assertNotEquals("", attachmentID, "Attachment ID should not be empty"); - - // Add a custom property - Integer customPropertyValue = 99999; - RequestBody bodyInt = - RequestBody.create( - "{\"customProperty2\": " + customPropertyValue + "}", - MediaType.parse("application/json")); - String updateCustomPropertyResponse = - api.updateSecondaryProperty( - appUrl, entityName, facetName, newEntityID, attachmentID, bodyInt); - assertEquals( - "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); + // String linkName = "Sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in new source entity"); + // } - // Save the entity - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + // String saveSourceResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save new source entity"); + // } - // Edit entity to fetch initial changelog - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!editResponse.equals("Entity in draft mode")) { + // fail("Could not edit target entity draft"); + // } - // Fetch changelog after initial save - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); + // List invalidObjectIds = Collections.singletonList("invalidObjectId123"); - assertNotNull(changelogResponse, "Changelog response should not be null"); + // try { + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, invalidObjectIds); + // fail("Copy did not throw error for invalid link ID"); + // } catch (IOException e) { + // System.out.println("Caught expected error while copying invalid link: " + e.getMessage()); + // } - // Verify changelog has 3 entries: 1 created + 2 updated (cmis:secondaryObjectTypeIds + - // customProperty2) - assertEquals(3, changelogResponse.get("numItems"), "Should have 3 changelog entries initially"); + // // No need to wait for upload completion as copy failed, but ensure clean state + // String saveTargetResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity after unsuccessful copy"); + // } - @SuppressWarnings("unchecked") - List> changeLogs = - (List>) changelogResponse.get("changeLogs"); - assertEquals(3, changeLogs.size(), "Should have exactly 3 changelog entries"); + // String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + // String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + // if (!deleteSourceResponse.equals("Entity Deleted") + // || !deleteTargetResponse.equals("Entity Deleted")) { + // fail("Could not delete new source entity or target entity"); + // } + // } - // Save entity again without any modifications - saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); + // @Test + // @Order(58) + // void testCopyLinkSuccessNewEntityDraft() throws IOException { + // System.out.println("Test (58): Copy link from one entity to another new entity draft mode"); - // Edit entity again and fetch changelog - editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // Fetch changelog after second save - Map changelogAfterSecondSave = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); + // if (copyLinkSourceEntity.equals("Could not create entity") + // || copyLinkTargetEntity.equals("Could not create entity")) { + // fail("Could not create source or target entity"); + // } - assertNotNull( - changelogAfterSecondSave, "Changelog response should not be null after second save"); + // String linkName = "sample"; + // String linkUrl = "https://www.example.com"; + // String createLinkResponse = + // api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); + // if (!createLinkResponse.equals("Link created successfully")) { + // fail("Could not create link in source entity"); + // } - // Verify changelog still has only 3 entries (no new entries added) - assertEquals( - 3, - changelogAfterSecondSave.get("numItems"), - "Should still have only 3 changelog entries after edit-save without modifications"); + // List sourceObjectIds = + // api.fetchEntityMetadataDraft(appUrl, entityName, facetName, + // copyLinkSourceEntity).stream() + // .map(item -> (String) item.get("objectId")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); - @SuppressWarnings("unchecked") - List> changeLogsAfterSecondSave = - (List>) changelogAfterSecondSave.get("changeLogs"); - assertEquals( - 3, - changeLogsAfterSecondSave.size(), - "Should still have exactly 3 changelog entries after second save"); + // if (sourceObjectIds.isEmpty()) { + // fail("Could not fetch object Id for link"); + // } - // Clean up the entity - api.deleteEntity(appUrl, entityName, newEntityID); - } + // String copyResponse = + // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + // if (!copyResponse.equals("Attachments copied successfully")) { + // fail("Could not copy link: " + copyResponse); + // } - @Test - @Order(64) - void testChangelogForSavedAttachmentWithoutModification() throws IOException { - System.out.println( - "Test (64): Create entity, upload attachment, save, edit and save again - verify changelog still has only 'created' entry"); + // List> attachmentsMetadata = new ArrayList<>(); + // attachmentsMetadata = + // api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyLinkTargetEntity); + // Map copiedAttachment = attachmentsMetadata.get(0); + // String receivedType = (String) copiedAttachment.get("type"); + // String receivedUrl = (String) copiedAttachment.get("linkUrl"); + + // String expectedType = "sap-icon://internet-browser"; + // assertTrue( + // expectedType.equalsIgnoreCase(receivedType), + // "Attachment type mismatch. Expected '" + // + expectedType + // + "' but got '" + // + receivedType + // + "'."); + + // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); + + // System.out.println("Attachment type and URL validated successfully."); + + // String attachmentId = (String) copiedAttachment.get("ID"); + // assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); + + // String openAttachmentResponse = + // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); + // if (!openAttachmentResponse.equals("Attachment opened successfully")) { + // fail("Could not open the attachment"); + // } - // Create a new entity - String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - assertNotNull(newEntityID, "Failed to create new entity"); - assertNotEquals("Could not create entity", newEntityID); + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + // if (!saveResponse.equals("Saved")) { + // fail("Could not save target entity after copying link"); + // } + // api.deleteEntityDraft(appUrl, entityName, copyLinkSourceEntity); + // api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + // } - // Prepare a sample file to upload - ClassLoader classLoader = getClass().getClassLoader(); - File file = new File(classLoader.getResource("sample.pdf").getFile()); - assertTrue(file.exists(), "Sample file should exist"); + // @Test + // @Order(59) + // void testCopyAttachmentsSuccessNewEntityDraft() throws IOException { + // System.out.println( + // "Test (59): Copy attachments from one entity to another new entity draft mode"); + // List attachments = new ArrayList<>(); + // copyAttachmentSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // copyAttachmentTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (!copyAttachmentSourceEntity.equals("Could not create entity") + // && !copyAttachmentTargetEntity.equals("Could not create entity")) { + // ClassLoader classLoader = getClass().getClassLoader(); + // List files = new ArrayList<>(); + // files.add(new File(classLoader.getResource("sample.pdf").getFile())); + // files.add(new File(classLoader.getResource("sample1.pdf").getFile())); + // Map postData = new HashMap<>(); + // postData.put("up__ID", entityID7); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); + + // sourceObjectIds.clear(); + + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, copyAttachmentSourceEntity, srvpath, postData, + // file); + // if (createResponse.get(0).equals("Attachment created")) { + // attachments.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment"); + // } + // } - // Create attachment - Map postData = new HashMap<>(); - postData.put("up__ID", newEntityID); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // List> attachmentsMetadata = new ArrayList<>(); + // Map fetchAttachmentMetadataResponse; + // for (String attachment : attachments) { + // try { + // fetchAttachmentMetadataResponse = + // api.fetchMetadataDraft( + // appUrl, entityName, facetName, copyAttachmentSourceEntity, attachment); + // attachmentsMetadata.add(fetchAttachmentMetadataResponse); + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } + // for (Map metadata : attachmentsMetadata) { + // if (metadata.containsKey("objectId")) { + // sourceObjectIds.add(metadata.get("objectId").toString()); + // } else { + // fail("Attachment metadata does not contain objectId"); + // } + // } - List createResponse = - api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); + // if (sourceObjectIds.size() == 2) { + // String copyResponse; + // copyResponse = + // api.copyAttachment( + // appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); + // if (copyResponse.equals("Attachments copied successfully")) { + // String saveEntityResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + // if (saveEntityResponse.equals("Saved")) { + // List> fetchEntityMetadataResponse; + // fetchEntityMetadataResponse = + // api.fetchEntityMetadata(appUrl, entityName, facetName, + // copyAttachmentTargetEntity); + // targetAttachmentIds = + // fetchEntityMetadataResponse.stream() + // .map(item -> (String) item.get("ID")) + // .filter(Objects::nonNull) + // .collect(Collectors.toList()); + // String readResponse; + // for (String targetAttachmentId : targetAttachmentIds) { + // readResponse = + // api.readAttachment( + // appUrl, + // entityName, + // facetName, + // copyAttachmentTargetEntity, + // targetAttachmentId); + // if (!readResponse.equals("OK")) { + // fail("Could not read copied attachment"); + // } + // } + // } else { + // fail("Could not save entity after copying attachments: " + saveEntityResponse); + // } + // } else { + // fail("Could not copy attachments: " + copyResponse); + // } + // } else { + // fail("Could not fetch objects Ids for all attachments"); + // } + // } else { + // fail("Could not create entities"); + // } + // api.deleteEntityDraft(appUrl, entityName, copyAttachmentSourceEntity); + // api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntity); + // } - assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - String status = createResponse.get(0); - String newAttachmentID = createResponse.get(1); + // @Test + // @Order(60) + // void testViewChangelogForNewlyCreatedAttachment() throws IOException { + // System.out.println("Test (60): View changelog for newly created attachment"); - assertEquals("Attachment created", status, "Attachment should be created successfully"); - assertNotNull(newAttachmentID, "Attachment ID should not be null"); - assertNotEquals("", newAttachmentID, "Attachment ID should not be empty"); + // // Create a new entity for changelog test + // changelogEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // assertNotNull(changelogEntityID, "Failed to create changelog test entity"); + // assertNotEquals("Could not create entity", changelogEntityID); - // Save the entity immediately without any modifications - String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + // // Prepare a sample file to upload + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.txt").getFile()); + // assertTrue(file.exists(), "Sample file should exist"); - // Edit entity again without making any changes to the attachment - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + // // Create attachment + // Map postData = new HashMap<>(); + // postData.put("up__ID", changelogEntityID); + // postData.put("mimeType", "text/plain"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // Save entity again without modifying the attachment - saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, changelogEntityID, srvpath, postData, file); + + // assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + // String status = createResponse.get(0); + // changelogAttachmentID = createResponse.get(1); + + // assertEquals("Attachment created", status, "Attachment should be created successfully"); + // assertNotNull(changelogAttachmentID, "Attachment ID should not be null"); + // assertNotEquals("", changelogAttachmentID, "Attachment ID should not be empty"); + + // // Fetch changelog for the newly created attachment + // Map changelogResponse = + // api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, + // changelogAttachmentID); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + + // // Verify changelog structure + // assertEquals(false, changelogResponse.get("hasMoreItems"), "hasMoreItems should be false"); + // assertEquals( + // "sample.txt", changelogResponse.get("filename"), "Filename should match uploaded file"); + // assertNotNull(changelogResponse.get("objectId"), "ObjectId should not be null"); + // assertEquals(1, changelogResponse.get("numItems"), "Should have 1 changelog entry"); + + // // Verify the changelog entry + // @SuppressWarnings("unchecked") + // List> changeLogs = + // (List>) changelogResponse.get("changeLogs"); + // assertEquals(1, changeLogs.size(), "Should have exactly 1 changelog entry"); + + // Map logEntry = changeLogs.get(0); + // assertEquals("created", logEntry.get("operation"), "Operation should be 'created'"); + // assertNotNull(logEntry.get("time"), "Time should not be null"); + // assertNotNull(logEntry.get("user"), "User should not be null"); + // assertFalse( + // logEntry.containsKey("changeDetail"), "Created operation should not have changeDetail"); + // } - // Edit entity to fetch changelog - editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + // @Test + // @Order(61) + // void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { + // System.out.println( + // "Test (61): Modify note field and custom property, then verify changelog shows created + + // 3 updated entries"); + + // // Update attachment with notes field (entity is already in draft mode from test 60) + // String notesValue = "Test note for changelog verification"; + // MediaType mediaType = MediaType.parse("application/json"); + // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + // RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); + + // String updateNotesResponse = + // api.updateSecondaryProperty( + // appUrl, + // entityName, + // facetName, + // changelogEntityID, + // changelogAttachmentID, + // updateNotesBody); + // assertEquals("Updated", updateNotesResponse, "Should successfully update notes field"); + + // // Update attachment with custom property + // Integer customProperty2Value = 12345; + // RequestBody bodyInt = + // RequestBody.create( + // "{\"customProperty2\": " + customProperty2Value + "}", + // MediaType.parse("application/json")); + // String updateCustomPropertyResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID, bodyInt); + // assertEquals( + // "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); + + // // Save the entity + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + // assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + + // // Edit entity again to fetch changelog + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + + // // Fetch changelog after modifications + // Map changelogResponse = + // api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, + // changelogAttachmentID); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + + // // Verify changelog content - should have 1 created + 3 updated (note, customProperty2, and + // // internal update) + // assertEquals(false, changelogResponse.get("hasMoreItems"), "hasMoreItems should be false"); + // assertEquals( + // 4, + // changelogResponse.get("numItems"), + // "Should have 4 changelog entries (1 created + 3 updated)"); + + // @SuppressWarnings("unchecked") + // List> changeLogs = + // (List>) changelogResponse.get("changeLogs"); + // assertEquals(4, changeLogs.size(), "Should have exactly 4 changelog entries"); + + // // Verify first entry is 'created' + // Map createdEntry = changeLogs.get(0); + // assertEquals( + // "created", createdEntry.get("operation"), "First entry should be 'created' operation"); + + // // Verify remaining entries are 'updated' + // long updatedCount = + // changeLogs.stream().filter(log -> "updated".equals(log.get("operation"))).count(); + // assertEquals(3, updatedCount, "Should have 3 'updated' operations"); + + // // Verify that changeDetail exists in updated entries for note field + // boolean hasNoteUpdate = + // changeLogs.stream() + // .filter(log -> "updated".equals(log.get("operation"))) + // .anyMatch( + // log -> { + // @SuppressWarnings("unchecked") + // Map changeDetail = (Map) + // log.get("changeDetail"); + // return changeDetail != null + // && "cmis:description".equals(changeDetail.get("field")); + // }); + // assertTrue(hasNoteUpdate, "Should have an update entry for note field (cmis:description)"); + // assertTrue(hasNoteUpdate, "Should have an update entry for note field (cmis:description)"); + + // // Save the entity so test 62 can edit it + // String saveResponseFinal = api.saveEntityDraft(appUrl, entityName, srvpath, + // changelogEntityID); + // assertEquals("Saved", saveResponseFinal, "Entity should be saved successfully"); + // } - // Fetch changelog for the attachment - Map changelogResponse = - api.fetchChangelog(appUrl, entityName, facetName, newEntityID, newAttachmentID); + // @Test + // @Order(62) + // void testChangelogAfterRenamingAttachment() throws IOException { + // System.out.println( + // "Test (62): Rename attachment and verify changelog increases with rename entry"); + + // // Edit entity to put it in draft mode (entity was saved at end of test 61) + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + + // // Rename the attachment + // String newFileName = "renamed_sample.txt"; + // String renameResponse = + // api.renameAttachment( + // appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID, + // newFileName); + // assertEquals("Renamed", renameResponse, "Should successfully rename attachment"); + + // // Save entity after rename + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + // assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); + + // // Edit entity again and fetch changelog + // editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + + // // Fetch changelog after rename + // Map changelogAfterRename = + // api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, + // changelogAttachmentID); + + // assertNotNull(changelogAfterRename, "Changelog response should not be null after rename"); + + // // Verify changelog has increased (rename operation adds 1 entry for cmis:name change) + // // Expected: 1 created + 3 initial updates + 1 rename update = 5 total + // assertEquals( + // 5, changelogAfterRename.get("numItems"), "Should have 5 changelog entries after rename"); + + // @SuppressWarnings("unchecked") + // List> changeLogsAfterRename = + // (List>) changelogAfterRename.get("changeLogs"); + // assertEquals( + // 5, changeLogsAfterRename.size(), "Should have exactly 5 changelog entries after rename"); + + // // Verify updated count is 4 (3 initial + 1 from rename operation) + // long updatedCountAfterRename = + // changeLogsAfterRename.stream() + // .filter(log -> "updated".equals(log.get("operation"))) + // .count(); + // assertEquals(4, updatedCountAfterRename, "Should have 4 'updated' operations after rename"); + + // // Verify filename change in changelog + // boolean hasFilenameUpdate = + // changeLogsAfterRename.stream() + // .filter(log -> "updated".equals(log.get("operation"))) + // .anyMatch( + // log -> { + // @SuppressWarnings("unchecked") + // Map changeDetail = (Map) + // log.get("changeDetail"); + // return changeDetail != null && "cmis:name".equals(changeDetail.get("field")); + // }); + // assertTrue(hasFilenameUpdate, "Should have an update entry for filename (cmis:name)"); + + // // Cleanup - entity was saved after rename, so delete the active entity + // api.deleteEntity(appUrl, entityName, changelogEntityID); + // } - assertNotNull(changelogResponse, "Changelog response should not be null"); + // @Test + // @Order(63) + // void testChangelogWithCustomPropertyEditSave() throws IOException { + // System.out.println( + // "Test (63): Create entity with custom property, save, edit and save again - verify + // changelog remains at 3 entries"); - // Verify changelog content - should only have 'created' entry even after edit and save - assertEquals(false, changelogResponse.get("hasMoreItems"), "hasMoreItems should be false"); - assertEquals( - "sample.pdf", changelogResponse.get("filename"), "Filename should match uploaded file"); - assertNotNull(changelogResponse.get("objectId"), "ObjectId should not be null"); - assertEquals(1, changelogResponse.get("numItems"), "Should have only 1 changelog entry"); - - // Verify the changelog entry - @SuppressWarnings("unchecked") - List> changeLogs = - (List>) changelogResponse.get("changeLogs"); - assertEquals(1, changeLogs.size(), "Should have exactly 1 changelog entry"); - - Map logEntry = changeLogs.get(0); - assertEquals("created", logEntry.get("operation"), "Operation should be 'created'"); - assertNotNull(logEntry.get("time"), "Time should not be null"); - assertNotNull(logEntry.get("user"), "User should not be null"); - assertFalse( - logEntry.containsKey("changeDetail"), "Created operation should not have changeDetail"); - - // Clean up the new entity - api.deleteEntity(appUrl, entityName, newEntityID); - } + // // Create a new entity + // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // assertNotNull(newEntityID, "Failed to create new entity"); + // assertNotEquals("Could not create entity", newEntityID); - @Test - @Order(65) - void testMoveAttachmentsWithSourceFacet() throws IOException { - System.out.println( - "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); + // // Prepare a sample file to upload + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // assertTrue(file.exists(), "Sample file should exist"); - // Create source entity and add attachments - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // // Create attachment + // Map postData = new HashMap<>(); + // postData.put("up__ID", newEntityID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // Prepare sample files - ClassLoader classLoader = getClass().getClassLoader(); - List files = new ArrayList<>(); - files.add(new File(classLoader.getResource("sample.pdf").getFile())); - files.add(new File(classLoader.getResource("sample.txt").getFile())); + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, + // file); + + // assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + // String status = createResponse.get(0); + // String attachmentID = createResponse.get(1); + + // assertEquals("Attachment created", status, "Attachment should be created successfully"); + // assertNotNull(attachmentID, "Attachment ID should not be null"); + // assertNotEquals("", attachmentID, "Attachment ID should not be empty"); + + // // Add a custom property + // Integer customPropertyValue = 99999; + // RequestBody bodyInt = + // RequestBody.create( + // "{\"customProperty2\": " + customPropertyValue + "}", + // MediaType.parse("application/json")); + // String updateCustomPropertyResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, newEntityID, attachmentID, bodyInt); + // assertEquals( + // "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); + + // // Save the entity + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + + // // Edit entity to fetch initial changelog + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + + // // Fetch changelog after initial save + // Map changelogResponse = + // api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + + // // Verify changelog has 3 entries: 1 created + 2 updated (cmis:secondaryObjectTypeIds + + // // customProperty2) + // assertEquals(3, changelogResponse.get("numItems"), "Should have 3 changelog entries + // initially"); + + // @SuppressWarnings("unchecked") + // List> changeLogs = + // (List>) changelogResponse.get("changeLogs"); + // assertEquals(3, changeLogs.size(), "Should have exactly 3 changelog entries"); + + // // Save entity again without any modifications + // saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); + + // // Edit entity again and fetch changelog + // editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + + // // Fetch changelog after second save + // Map changelogAfterSecondSave = + // api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); + + // assertNotNull( + // changelogAfterSecondSave, "Changelog response should not be null after second save"); + + // // Verify changelog still has only 3 entries (no new entries added) + // assertEquals( + // 3, + // changelogAfterSecondSave.get("numItems"), + // "Should still have only 3 changelog entries after edit-save without modifications"); + + // @SuppressWarnings("unchecked") + // List> changeLogsAfterSecondSave = + // (List>) changelogAfterSecondSave.get("changeLogs"); + // assertEquals( + // 3, + // changeLogsAfterSecondSave.size(), + // "Should still have exactly 3 changelog entries after second save"); + + // // Clean up the entity + // api.deleteEntity(appUrl, entityName, newEntityID); + // } - Map postData = new HashMap<>(); - postData.put("up__ID", moveSourceEntity); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // @Test + // @Order(64) + // void testChangelogForSavedAttachmentWithoutModification() throws IOException { + // System.out.println( + // "Test (64): Create entity, upload attachment, save, edit and save again - verify + // changelog still has only 'created' entry"); - // Create attachments in source entity - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment in source entity"); - } - } + // // Create a new entity + // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // assertNotNull(newEntityID, "Failed to create new entity"); + // assertNotEquals("Could not create entity", newEntityID); - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // Prepare a sample file to upload + // ClassLoader classLoader = getClass().getClassLoader(); + // File file = new File(classLoader.getResource("sample.pdf").getFile()); + // assertTrue(file.exists(), "Sample file should exist"); - // Fetch object IDs from source entity - moveObjectIds.clear(); - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - // Get source folder ID - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } else { - fail("Attachment metadata does not contain objectId"); - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // // Create attachment + // Map postData = new HashMap<>(); + // postData.put("up__ID", newEntityID); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // List createResponse = + // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, + // file); + + // assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + // String status = createResponse.get(0); + // String newAttachmentID = createResponse.get(1); + + // assertEquals("Attachment created", status, "Attachment should be created successfully"); + // assertNotNull(newAttachmentID, "Attachment ID should not be null"); + // assertNotEquals("", newAttachmentID, "Attachment ID should not be empty"); + + // // Save the entity immediately without any modifications + // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // assertEquals("Saved", saveResponse, "Entity should be saved successfully"); + + // // Edit entity again without making any changes to the attachment + // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + + // // Save entity again without modifying the attachment + // saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + // assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); + + // // Edit entity to fetch changelog + // editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); + + // // Fetch changelog for the attachment + // Map changelogResponse = + // api.fetchChangelog(appUrl, entityName, facetName, newEntityID, newAttachmentID); + + // assertNotNull(changelogResponse, "Changelog response should not be null"); + + // // Verify changelog content - should only have 'created' entry even after edit and save + // assertEquals(false, changelogResponse.get("hasMoreItems"), "hasMoreItems should be false"); + // assertEquals( + // "sample.pdf", changelogResponse.get("filename"), "Filename should match uploaded file"); + // assertNotNull(changelogResponse.get("objectId"), "ObjectId should not be null"); + // assertEquals(1, changelogResponse.get("numItems"), "Should have only 1 changelog entry"); + + // // Verify the changelog entry + // @SuppressWarnings("unchecked") + // List> changeLogs = + // (List>) changelogResponse.get("changeLogs"); + // assertEquals(1, changeLogs.size(), "Should have exactly 1 changelog entry"); + + // Map logEntry = changeLogs.get(0); + // assertEquals("created", logEntry.get("operation"), "Operation should be 'created'"); + // assertNotNull(logEntry.get("time"), "Time should not be null"); + // assertNotNull(logEntry.get("user"), "User should not be null"); + // assertFalse( + // logEntry.containsKey("changeDetail"), "Created operation should not have changeDetail"); + + // // Clean up the new entity + // api.deleteEntity(appUrl, entityName, newEntityID); + // } - // Create target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // @Test + // @Order(65) + // void testMoveAttachmentsWithSourceFacet() throws IOException { + // System.out.println( + // "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); - // Save target before move - String saveTargetBeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity: " + saveTargetBeforeMoveResponse); - } + // // Create source entity and add attachments + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - // Move attachments from source to target with sourceFacet - String sourceFacet = serviceName + "." + entityName + "." + facetName; - String targetFacet = serviceName + "." + entityName + "." + facetName; - api.moveAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // All attachments moved to target entity in SDM & UI - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - assertEquals( - sourceAttachmentIds.size(), - targetMetadataAfterMove.size(), - "Target entity should have all attachments after move"); - - // Verify attachments can be read from target entity - for (Map metadata : targetMetadataAfterMove) { - String targetAttachmentId = (String) metadata.get("ID"); - String readResponse = - api.readAttachment(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); - if (!readResponse.equals("OK")) { - fail("Could not read moved attachment from target entity"); - } - } + // // Prepare sample files + // ClassLoader classLoader = getClass().getClassLoader(); + // List files = new ArrayList<>(); + // files.add(new File(classLoader.getResource("sample.pdf").getFile())); + // files.add(new File(classLoader.getResource("sample.txt").getFile())); - // All attachments removed from source entity in SDM & UI - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - assertEquals( - 0, sourceMetadataAfterMove.size(), "Source entity should have 0 attachments after move"); + // Map postData = new HashMap<>(); + // postData.put("up__ID", moveSourceEntity); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // // Create attachments in source entity + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in source entity"); + // } + // } - @Test - @Order(66) - public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { - System.out.println( - "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); + // // Save source entity + // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - // Create source entity and add attachments - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // // Fetch object IDs from source entity + // moveObjectIds.clear(); + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // // Get source folder ID + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } else { + // fail("Attachment metadata does not contain objectId"); + // } + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } - // Prepare sample files - ClassLoader classLoader = getClass().getClassLoader(); - List files = new ArrayList<>(); - files.add(new File(classLoader.getResource("sample.pdf").getFile())); - files.add(new File(classLoader.getResource("sample.txt").getFile())); + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - Map postData = new HashMap<>(); - postData.put("up__ID", moveSourceEntity); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // // Create target entity + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - // Create attachments in source entity - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment in source entity"); - } - } + // // Save target before move + // String saveTargetBeforeMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity: " + saveTargetBeforeMoveResponse); + // } - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // Move attachments from source to target with sourceFacet + // String sourceFacet = serviceName + "." + entityName + "." + facetName; + // String targetFacet = serviceName + "." + entityName + "." + facetName; + // api.moveAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // All attachments moved to target entity in SDM & UI + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // assertEquals( + // sourceAttachmentIds.size(), + // targetMetadataAfterMove.size(), + // "Target entity should have all attachments after move"); + + // // Verify attachments can be read from target entity + // for (Map metadata : targetMetadataAfterMove) { + // String targetAttachmentId = (String) metadata.get("ID"); + // String readResponse = + // api.readAttachment(appUrl, entityName, facetName, moveTargetEntity, + // targetAttachmentId); + // if (!readResponse.equals("OK")) { + // fail("Could not read moved attachment from target entity"); + // } + // } - // Fetch object IDs from source entity - moveObjectIds.clear(); - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } catch (Exception e) { - fail("Could not fetch metadata for attachment: " + attachmentId); - } - } + // // All attachments removed from source entity in SDM & UI + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // assertEquals( + // 0, sourceMetadataAfterMove.size(), "Source entity should have 0 attachments after move"); - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch all objectIds from source entity"); - } + // // Clean up - delete both entities + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } - // Create target entity and add attachment - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // @Test + // @Order(66) + // public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { + // System.out.println( + // "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); - Map targetPostData = new HashMap<>(); - targetPostData.put("up__ID", moveTargetEntity); - targetPostData.put("mimeType", "application/pdf"); - targetPostData.put("createdAt", new Date().toString()); - targetPostData.put("createdBy", "test@test.com"); - targetPostData.put("modifiedBy", "test@test.com"); + // // Create source entity and add attachments + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); - List targetCreateResponse = - api.createAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - srvpath, - targetPostData, - duplicateFile); + // // Prepare sample files + // ClassLoader classLoader = getClass().getClassLoader(); + // List files = new ArrayList<>(); + // files.add(new File(classLoader.getResource("sample.pdf").getFile())); + // files.add(new File(classLoader.getResource("sample.txt").getFile())); - if (!targetCreateResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment on target entity"); - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", moveSourceEntity); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // Save target entity to persist the attachment - String saveTargetBeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse); - } + // // Create attachments in source entity + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in source entity"); + // } + // } - // Fetch target metadata before move (target entity is now saved with 1 attachment) - List> targetMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - int targetCountBeforeMove = targetMetadataBeforeMove.size(); - - // Move attachments from source to target with sourceFacet - String sourceFacet = serviceName + "." + entityName + "." + facetName; - String targetFacet = serviceName + "." + entityName + "." + facetName; - api.moveAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - // Verify target has duplicate skipped, other attachments moved - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - - // Expected: original attachments + non-duplicate moved attachments - int expectedTargetCount = targetCountBeforeMove + (sourceAttachmentIds.size() - 1); - assertEquals( - expectedTargetCount, - targetMetadataAfterMove.size(), - "Target should have duplicate skipped, other attachments moved"); - - // Verify source entity has only the duplicate attachment remaining - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // Calculate expected source count: number of duplicates that couldn't be moved - int expectedSourceCount = - sourceAttachmentIds.size() - (targetMetadataAfterMove.size() - targetCountBeforeMove); - assertEquals( - expectedSourceCount, - sourceMetadataAfterMove.size(), - "Source should have duplicate attachment remaining"); + // // Save source entity + // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // // Fetch object IDs from source entity + // moveObjectIds.clear(); + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } catch (Exception e) { + // fail("Could not fetch metadata for attachment: " + attachmentId); + // } + // } - @Test - @Order(67) - public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { - System.out.println( - "Test (67): Move attachments with notes and secondary properties with sourceFacet"); + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch all objectIds from source entity"); + // } - // Create source entity and add attachments - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // // Create target entity and add attachment + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - // Prepare sample files - ClassLoader classLoader = getClass().getClassLoader(); - List files = new ArrayList<>(); - files.add(new File(classLoader.getResource("sample.pdf").getFile())); - files.add(new File(classLoader.getResource("sample.txt").getFile())); + // Map targetPostData = new HashMap<>(); + // targetPostData.put("up__ID", moveTargetEntity); + // targetPostData.put("mimeType", "application/pdf"); + // targetPostData.put("createdAt", new Date().toString()); + // targetPostData.put("createdBy", "test@test.com"); + // targetPostData.put("modifiedBy", "test@test.com"); - Map postData = new HashMap<>(); - postData.put("up__ID", moveSourceEntity); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); + // List targetCreateResponse = + // api.createAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity, + // srvpath, + // targetPostData, + // duplicateFile); + + // if (!targetCreateResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment on target entity"); + // } - // Create attachments in source entity - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment in source entity"); - } - } + // // Save target entity to persist the attachment + // String saveTargetBeforeMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse); + // } - // Add notes to attachments - String notesValue = "Test note for verification"; - MediaType mediaType = MediaType.parse("application/json"); - String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); - - for (String attachmentId : sourceAttachmentIds) { - String updateNotesResponse = - api.updateSecondaryProperty( - appUrl, entityName, facetName, moveSourceEntity, attachmentId, updateNotesBody); - if (!updateNotesResponse.equals("Updated")) { - fail("Could not update notes for attachment: " + attachmentId); - } - } + // // Fetch target metadata before move (target entity is now saved with 1 attachment) + // List> targetMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // int targetCountBeforeMove = targetMetadataBeforeMove.size(); + + // // Move attachments from source to target with sourceFacet + // String sourceFacet = serviceName + "." + entityName + "." + facetName; + // String targetFacet = serviceName + "." + entityName + "." + facetName; + // api.moveAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // // Verify target has duplicate skipped, other attachments moved + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + + // // Expected: original attachments + non-duplicate moved attachments + // int expectedTargetCount = targetCountBeforeMove + (sourceAttachmentIds.size() - 1); + // assertEquals( + // expectedTargetCount, + // targetMetadataAfterMove.size(), + // "Target should have duplicate skipped, other attachments moved"); + + // // Verify source entity has only the duplicate attachment remaining + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // // Calculate expected source count: number of duplicates that couldn't be moved + // int expectedSourceCount = + // sourceAttachmentIds.size() - (targetMetadataAfterMove.size() - targetCountBeforeMove); + // assertEquals( + // expectedSourceCount, + // sourceMetadataAfterMove.size(), + // "Source should have duplicate attachment remaining"); + + // // Clean up - delete both entities + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } - // Add custom property to attachments - Integer customProperty2Value = 54321; - RequestBody bodyInt = - RequestBody.create( - "{\"customProperty2\": " + customProperty2Value + "}", - MediaType.parse("application/json")); - - for (String attachmentId : sourceAttachmentIds) { - String updateCustomPropertyResponse = - api.updateSecondaryProperty( - appUrl, entityName, facetName, moveSourceEntity, attachmentId, bodyInt); - if (!updateCustomPropertyResponse.equals("Updated")) { - fail("Could not update custom property for attachment: " + attachmentId); - } - } + // @Test + // @Order(67) + // public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { + // System.out.println( + // "Test (67): Move attachments with notes and secondary properties with sourceFacet"); - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // Create source entity and add attachments + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - // Fetch object IDs from source entity - moveObjectIds.clear(); - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } catch (Exception e) { - fail("Could not fetch metadata for attachment: " + attachmentId); - } - } + // // Prepare sample files + // ClassLoader classLoader = getClass().getClassLoader(); + // List files = new ArrayList<>(); + // files.add(new File(classLoader.getResource("sample.pdf").getFile())); + // files.add(new File(classLoader.getResource("sample.txt").getFile())); - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch all objectIds from source entity"); - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", moveSourceEntity); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // Create target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // // Create attachments in source entity + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in source entity"); + // } + // } - // Save target before move - String saveTargetBeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse); - } + // // Add notes to attachments + // String notesValue = "Test note for verification"; + // MediaType mediaType = MediaType.parse("application/json"); + // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + // RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); + + // for (String attachmentId : sourceAttachmentIds) { + // String updateNotesResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, moveSourceEntity, attachmentId, updateNotesBody); + // if (!updateNotesResponse.equals("Updated")) { + // fail("Could not update notes for attachment: " + attachmentId); + // } + // } - // Move attachments from source to target with sourceFacet - String sourceFacet = serviceName + "." + entityName + "." + facetName; - String targetFacet = serviceName + "." + entityName + "." + facetName; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // // Add custom property to attachments + // Integer customProperty2Value = 54321; + // RequestBody bodyInt = + // RequestBody.create( + // "{\"customProperty2\": " + customProperty2Value + "}", + // MediaType.parse("application/json")); + + // for (String attachmentId : sourceAttachmentIds) { + // String updateCustomPropertyResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, moveSourceEntity, attachmentId, bodyInt); + // if (!updateCustomPropertyResponse.equals("Updated")) { + // fail("Could not update custom property for attachment: " + attachmentId); + // } + // } - // Verify all attachments moved to target - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - assertEquals( - sourceAttachmentIds.size(), - targetMetadataAfterMove.size(), - "Target entity should have all attachments after move"); - - // Verify notes and secondary properties are preserved - for (Map metadata : targetMetadataAfterMove) { - String targetAttachmentId = (String) metadata.get("ID"); - assertNotNull(targetAttachmentId, "Target attachment ID should not be null"); - - Map detailedMetadata = - api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); - - // Verify notes are preserved - if (detailedMetadata.containsKey("note")) { - assertEquals( - notesValue, - detailedMetadata.get("note"), - "Notes should be preserved after move for attachment: " + targetAttachmentId); - } else { - fail("Notes property missing after move for attachment: " + targetAttachmentId); - } + // // Save source entity + // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - // Verify custom property is preserved - if (detailedMetadata.containsKey("customProperty2")) { - assertEquals( - customProperty2Value, - detailedMetadata.get("customProperty2"), - "Custom property should be preserved after move for attachment: " + targetAttachmentId); - } else { - fail("Custom property missing after move for attachment: " + targetAttachmentId); - } - } + // // Fetch object IDs from source entity + // moveObjectIds.clear(); + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } catch (Exception e) { + // fail("Could not fetch metadata for attachment: " + attachmentId); + // } + // } - // Verify source entity has no attachments (all moved with sourceFacet) - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - assertEquals(0, sourceMetadataAfterMove.size(), "Source entity has no attachments after move"); + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch all objectIds from source entity"); + // } - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // // Create target entity + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - @Test - @Order(68) - public void testMoveAttachmentsWithoutSourceFacet() throws Exception { - System.out.println( - "Test (68): Move valid attachments from Source Entity to Target Entity without sourceFacet"); + // // Save target before move + // String saveTargetBeforeMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse); + // } - // Create source entity and add attachments - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // // Move attachments from source to target with sourceFacet + // String sourceFacet = serviceName + "." + entityName + "." + facetName; + // String targetFacet = serviceName + "." + entityName + "." + facetName; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - // Prepare sample files - ClassLoader classLoader = getClass().getClassLoader(); - List files = new ArrayList<>(); - files.add(new File(classLoader.getResource("sample.pdf").getFile())); - files.add(new File(classLoader.getResource("sample.txt").getFile())); + // // Verify all attachments moved to target + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // assertEquals( + // sourceAttachmentIds.size(), + // targetMetadataAfterMove.size(), + // "Target entity should have all attachments after move"); + + // // Verify notes and secondary properties are preserved + // for (Map metadata : targetMetadataAfterMove) { + // String targetAttachmentId = (String) metadata.get("ID"); + // assertNotNull(targetAttachmentId, "Target attachment ID should not be null"); + + // Map detailedMetadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); + + // // Verify notes are preserved + // if (detailedMetadata.containsKey("note")) { + // assertEquals( + // notesValue, + // detailedMetadata.get("note"), + // "Notes should be preserved after move for attachment: " + targetAttachmentId); + // } else { + // fail("Notes property missing after move for attachment: " + targetAttachmentId); + // } - Map postData = new HashMap<>(); - postData.put("up__ID", moveSourceEntity); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // // Verify custom property is preserved + // if (detailedMetadata.containsKey("customProperty2")) { + // assertEquals( + // customProperty2Value, + // detailedMetadata.get("customProperty2"), + // "Custom property should be preserved after move for attachment: " + + // targetAttachmentId); + // } else { + // fail("Custom property missing after move for attachment: " + targetAttachmentId); + // } + // } - // Create attachments in source entity - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment in source entity"); - } - } + // // Verify source entity has no attachments (all moved with sourceFacet) + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // assertEquals(0, sourceMetadataAfterMove.size(), "Source entity has no attachments after + // move"); - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // Clean up - delete both entities + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } - // Fetch object IDs from source entity - moveObjectIds.clear(); - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - // Get source folder ID from first attachment - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } else { - fail("Attachment metadata does not contain objectId"); - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // @Test + // @Order(68) + // public void testMoveAttachmentsWithoutSourceFacet() throws Exception { + // System.out.println( + // "Test (68): Move valid attachments from Source Entity to Target Entity without + // sourceFacet"); - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // // Create source entity and add attachments + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - // Create target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // // Prepare sample files + // ClassLoader classLoader = getClass().getClassLoader(); + // List files = new ArrayList<>(); + // files.add(new File(classLoader.getResource("sample.pdf").getFile())); + // files.add(new File(classLoader.getResource("sample.txt").getFile())); - // Save target before move - String saveTargetBeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity before move"); - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", moveSourceEntity); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // Move attachments without sourceFacet (pass null for sourceFacet parameter) - String targetFacet = serviceName + "." + entityName + "." + facetName; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - null); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // // Create attachments in source entity + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in source entity"); + // } + // } - // Verify attachments are in target entity - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - assertEquals( - moveObjectIds.size(), - targetMetadataAfterMove.size(), - "Target entity should have all moved attachments"); - - // Verify attachments can be read from target entity - for (Map metadata : targetMetadataAfterMove) { - String targetAttachmentId = (String) metadata.get("ID"); - String readResponse = - api.readAttachment(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); - if (!readResponse.equals("OK")) { - fail("Could not read moved attachment from target entity"); - } - } + // // Save source entity + // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - // Expected Behavior: Attachments remain in source entity UI (without sourceFacet) - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - assertEquals( - moveObjectIds.size(), - sourceMetadataAfterMove.size(), - "Source entity should still have attachments in UI when sourceFacet is not specified"); + // // Fetch object IDs from source entity + // moveObjectIds.clear(); + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // // Get source folder ID from first attachment + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } else { + // fail("Attachment metadata does not contain objectId"); + // } + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } - // Verify the same objectIds are still visible in source - for (Map metadata : sourceMetadataAfterMove) { - String objectId = (String) metadata.get("objectId"); - assertTrue( - moveObjectIds.contains(objectId), - "Source entity should still show attachment with objectId: " + objectId); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // // Create target entity + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - @Test - @Order(69) - public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { - System.out.println( - "Test (69): Move attachments into existing Target Entity when duplicate exists without sourceFacet"); + // // Save target before move + // String saveTargetBeforeMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity before move"); + // } - // Create source entity and add attachments - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // // Move attachments without sourceFacet (pass null for sourceFacet parameter) + // String targetFacet = serviceName + "." + entityName + "." + facetName; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // null); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - // Prepare sample files - ClassLoader classLoader = getClass().getClassLoader(); - List files = new ArrayList<>(); - files.add(new File(classLoader.getResource("sample.pdf").getFile())); - files.add(new File(classLoader.getResource("sample.txt").getFile())); + // // Verify attachments are in target entity + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // assertEquals( + // moveObjectIds.size(), + // targetMetadataAfterMove.size(), + // "Target entity should have all moved attachments"); + + // // Verify attachments can be read from target entity + // for (Map metadata : targetMetadataAfterMove) { + // String targetAttachmentId = (String) metadata.get("ID"); + // String readResponse = + // api.readAttachment(appUrl, entityName, facetName, moveTargetEntity, + // targetAttachmentId); + // if (!readResponse.equals("OK")) { + // fail("Could not read moved attachment from target entity"); + // } + // } - Map postData = new HashMap<>(); - postData.put("up__ID", moveSourceEntity); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // // Expected Behavior: Attachments remain in source entity UI (without sourceFacet) + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // assertEquals( + // moveObjectIds.size(), + // sourceMetadataAfterMove.size(), + // "Source entity should still have attachments in UI when sourceFacet is not specified"); + + // // Verify the same objectIds are still visible in source + // for (Map metadata : sourceMetadataAfterMove) { + // String objectId = (String) metadata.get("objectId"); + // assertTrue( + // moveObjectIds.contains(objectId), + // "Source entity should still show attachment with objectId: " + objectId); + // } - // Create attachments in source entity - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment in source entity"); - } - } + // // Clean up - delete both entities + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // @Test + // @Order(69) + // public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { + // System.out.println( + // "Test (69): Move attachments into existing Target Entity when duplicate exists without + // sourceFacet"); - // Fetch object IDs from source entity - moveObjectIds.clear(); - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - // Get source folder ID from first attachment - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } else { - fail("Attachment metadata does not contain objectId"); - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // // Create source entity and add attachments + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // // Prepare sample files + // ClassLoader classLoader = getClass().getClassLoader(); + // List files = new ArrayList<>(); + // files.add(new File(classLoader.getResource("sample.pdf").getFile())); + // files.add(new File(classLoader.getResource("sample.txt").getFile())); - // Create target entity and add duplicate attachment (sample.pdf) - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", moveSourceEntity); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // Add the same first file (sample.pdf) to target entity to create duplicate - Map targetPostData = new HashMap<>(); - targetPostData.put("up__ID", moveTargetEntity); - targetPostData.put("mimeType", "application/pdf"); - targetPostData.put("createdAt", new Date().toString()); - targetPostData.put("createdBy", "test@test.com"); - targetPostData.put("modifiedBy", "test@test.com"); + // // Create attachments in source entity + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in source entity"); + // } + // } - List createTargetResponse = - api.createAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - srvpath, - targetPostData, - files.get(0)); // Add same file (sample.pdf) - if (!createTargetResponse.get(0).equals("Attachment created")) { - fail("Could not create duplicate attachment in target entity"); - } + // // Save source entity + // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - // Save target entity before move - String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity: " + saveTargetResponse); - } + // // Fetch object IDs from source entity + // moveObjectIds.clear(); + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // // Get source folder ID from first attachment + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } else { + // fail("Attachment metadata does not contain objectId"); + // } + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } - // Get initial target metadata count - List> targetMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - int initialTargetCount = targetMetadataBeforeMove.size(); + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - // Step 3: Move attachments without sourceFacet (duplicate should be skipped) - String targetFacet = serviceName + "." + entityName + "." + facetName; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - null); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // // Create target entity and add duplicate attachment (sample.pdf) + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - // Expected Behavior - Verify duplicate was skipped, other attachments moved - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // // Add the same first file (sample.pdf) to target entity to create duplicate + // Map targetPostData = new HashMap<>(); + // targetPostData.put("up__ID", moveTargetEntity); + // targetPostData.put("mimeType", "application/pdf"); + // targetPostData.put("createdAt", new Date().toString()); + // targetPostData.put("createdBy", "test@test.com"); + // targetPostData.put("modifiedBy", "test@test.com"); - int nonDuplicateCount = moveObjectIds.size() - 1; - int expectedTargetCount = initialTargetCount + nonDuplicateCount; + // List createTargetResponse = + // api.createAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity, + // srvpath, + // targetPostData, + // files.get(0)); // Add same file (sample.pdf) + // if (!createTargetResponse.get(0).equals("Attachment created")) { + // fail("Could not create duplicate attachment in target entity"); + // } - assertEquals( - expectedTargetCount, - targetMetadataAfterMove.size(), - "Target entity should have initial attachments plus non-duplicate moved attachments"); - - // Verify at least one non-duplicate attachment was moved - assertTrue( - targetMetadataAfterMove.size() > initialTargetCount, - "Target should have more attachments after move (non-duplicates added)"); - - // Verify all attachments still remain in source entity UI (without sourceFacet) - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - assertEquals( - moveObjectIds.size(), - sourceMetadataAfterMove.size(), - "Source entity should still have all attachments in UI when sourceFacet is not specified"); - - // Verify all original objectIds are still visible in source - List sourceObjectIds = new ArrayList<>(); - for (Map metadata : sourceMetadataAfterMove) { - sourceObjectIds.add((String) metadata.get("objectId")); - } - for (String objectId : moveObjectIds) { - assertTrue( - sourceObjectIds.contains(objectId), - "Source entity should still show attachment with objectId: " + objectId); - } + // // Save target entity before move + // String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity: " + saveTargetResponse); + // } - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // // Get initial target metadata count + // List> targetMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // int initialTargetCount = targetMetadataBeforeMove.size(); + + // // Step 3: Move attachments without sourceFacet (duplicate should be skipped) + // String targetFacet = serviceName + "." + entityName + "." + facetName; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // null); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - @Test - @Order(70) - public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() - throws Exception { - System.out.println( - "Test (70): Move attachments with notes and secondary properties without sourceFacet"); + // // Expected Behavior - Verify duplicate was skipped, other attachments moved + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + + // int nonDuplicateCount = moveObjectIds.size() - 1; + // int expectedTargetCount = initialTargetCount + nonDuplicateCount; + + // assertEquals( + // expectedTargetCount, + // targetMetadataAfterMove.size(), + // "Target entity should have initial attachments plus non-duplicate moved attachments"); + + // // Verify at least one non-duplicate attachment was moved + // assertTrue( + // targetMetadataAfterMove.size() > initialTargetCount, + // "Target should have more attachments after move (non-duplicates added)"); + + // // Verify all attachments still remain in source entity UI (without sourceFacet) + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // assertEquals( + // moveObjectIds.size(), + // sourceMetadataAfterMove.size(), + // "Source entity should still have all attachments in UI when sourceFacet is not + // specified"); + + // // Verify all original objectIds are still visible in source + // List sourceObjectIds = new ArrayList<>(); + // for (Map metadata : sourceMetadataAfterMove) { + // sourceObjectIds.add((String) metadata.get("objectId")); + // } + // for (String objectId : moveObjectIds) { + // assertTrue( + // sourceObjectIds.contains(objectId), + // "Source entity should still show attachment with objectId: " + objectId); + // } - // Create source entity and add attachments - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // // Clean up - delete both entities + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } - // Prepare sample files - ClassLoader classLoader = getClass().getClassLoader(); - List files = new ArrayList<>(); - files.add(new File(classLoader.getResource("sample.pdf").getFile())); - files.add(new File(classLoader.getResource("sample.txt").getFile())); + // @Test + // @Order(70) + // public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() + // throws Exception { + // System.out.println( + // "Test (70): Move attachments with notes and secondary properties without sourceFacet"); - Map postData = new HashMap<>(); - postData.put("up__ID", moveSourceEntity); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // // Create source entity and add attachments + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - // Create attachments in source entity - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment in source entity"); - } - } + // // Prepare sample files + // ClassLoader classLoader = getClass().getClassLoader(); + // List files = new ArrayList<>(); + // files.add(new File(classLoader.getResource("sample.pdf").getFile())); + // files.add(new File(classLoader.getResource("sample.txt").getFile())); - // Add notes to attachments - String notesValue = "Test note for migration verification"; - MediaType mediaType = MediaType.parse("application/json"); - String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); - - for (String attachmentId : sourceAttachmentIds) { - String updateNotesResponse = - api.updateSecondaryProperty( - appUrl, entityName, facetName, moveSourceEntity, attachmentId, updateNotesBody); - if (!updateNotesResponse.equals("Updated")) { - fail("Could not update notes for attachment: " + attachmentId); - } - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", moveSourceEntity); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // Add custom property to attachments - Integer customProperty2Value = 54321; - RequestBody bodyInt = - RequestBody.create( - "{\"customProperty2\": " + customProperty2Value + "}", - MediaType.parse("application/json")); - - for (String attachmentId : sourceAttachmentIds) { - String updateCustomPropertyResponse = - api.updateSecondaryProperty( - appUrl, entityName, facetName, moveSourceEntity, attachmentId, bodyInt); - if (!updateCustomPropertyResponse.equals("Updated")) { - fail("Could not update custom property for attachment: " + attachmentId); - } - } + // // Create attachments in source entity + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in source entity"); + // } + // } - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // Add notes to attachments + // String notesValue = "Test note for migration verification"; + // MediaType mediaType = MediaType.parse("application/json"); + // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + // RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); + + // for (String attachmentId : sourceAttachmentIds) { + // String updateNotesResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, moveSourceEntity, attachmentId, updateNotesBody); + // if (!updateNotesResponse.equals("Updated")) { + // fail("Could not update notes for attachment: " + attachmentId); + // } + // } - // Fetch object IDs from source entity - moveObjectIds.clear(); - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } catch (Exception e) { - fail("Could not fetch metadata for attachment: " + attachmentId); - } - } + // // Add custom property to attachments + // Integer customProperty2Value = 54321; + // RequestBody bodyInt = + // RequestBody.create( + // "{\"customProperty2\": " + customProperty2Value + "}", + // MediaType.parse("application/json")); + + // for (String attachmentId : sourceAttachmentIds) { + // String updateCustomPropertyResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, moveSourceEntity, attachmentId, bodyInt); + // if (!updateCustomPropertyResponse.equals("Updated")) { + // fail("Could not update custom property for attachment: " + attachmentId); + // } + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch all objectIds from source entity"); - } + // // Save source entity + // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - // Get source attachment count before move - List> sourceMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + // // Fetch object IDs from source entity + // moveObjectIds.clear(); + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } catch (Exception e) { + // fail("Could not fetch metadata for attachment: " + attachmentId); + // } + // } - // Create target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch all objectIds from source entity"); + // } - // Save target before move - String saveTargetBeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity before move"); - } + // // Get source attachment count before move + // List> sourceMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - // Get target attachment count before move - List> targetMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - int targetCountBeforeMove = targetMetadataBeforeMove.size(); + // // Create target entity + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - // Move attachments from source to target WITHOUT sourceFacet - String targetFacet = serviceName + "." + entityName + "." + facetName; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - null); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // // Save target before move + // String saveTargetBeforeMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity before move"); + // } - // Verify expected number of attachments moved to target - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - int expectedTargetCount = targetCountBeforeMove + sourceAttachmentIds.size(); - assertEquals( - expectedTargetCount, - targetMetadataAfterMove.size(), - "Target entity should have " + expectedTargetCount + " attachments after move"); - - // Verify notes and secondary properties are preserved - for (Map metadata : targetMetadataAfterMove) { - String targetAttachmentId = (String) metadata.get("ID"); - assertNotNull(targetAttachmentId, "Target attachment ID should not be null"); - - Map detailedMetadata = - api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); - - // Verify notes are preserved - if (detailedMetadata.containsKey("note")) { - assertEquals( - notesValue, - detailedMetadata.get("note"), - "Notes should be preserved after move for attachment: " + targetAttachmentId); - } else { - fail("Notes property missing after move for attachment: " + targetAttachmentId); - } + // // Get target attachment count before move + // List> targetMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // int targetCountBeforeMove = targetMetadataBeforeMove.size(); + + // // Move attachments from source to target WITHOUT sourceFacet + // String targetFacet = serviceName + "." + entityName + "." + facetName; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // null); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - // Verify custom property is preserved - if (detailedMetadata.containsKey("customProperty2")) { - assertEquals( - customProperty2Value, - detailedMetadata.get("customProperty2"), - "Custom property should be preserved after move for attachment: " + targetAttachmentId); - } else { - fail("Custom property missing after move for attachment: " + targetAttachmentId); - } - } + // // Verify expected number of attachments moved to target + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // int expectedTargetCount = targetCountBeforeMove + sourceAttachmentIds.size(); + // assertEquals( + // expectedTargetCount, + // targetMetadataAfterMove.size(), + // "Target entity should have " + expectedTargetCount + " attachments after move"); + + // // Verify notes and secondary properties are preserved + // for (Map metadata : targetMetadataAfterMove) { + // String targetAttachmentId = (String) metadata.get("ID"); + // assertNotNull(targetAttachmentId, "Target attachment ID should not be null"); + + // Map detailedMetadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); + + // // Verify notes are preserved + // if (detailedMetadata.containsKey("note")) { + // assertEquals( + // notesValue, + // detailedMetadata.get("note"), + // "Notes should be preserved after move for attachment: " + targetAttachmentId); + // } else { + // fail("Notes property missing after move for attachment: " + targetAttachmentId); + // } - // Verify source entity still has all attachments (without sourceFacet) - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - assertEquals( - sourceCountBeforeMove, - sourceMetadataAfterMove.size(), - "Source entity should still have " - + sourceCountBeforeMove - + " attachments (without sourceFacet)"); - - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // // Verify custom property is preserved + // if (detailedMetadata.containsKey("customProperty2")) { + // assertEquals( + // customProperty2Value, + // detailedMetadata.get("customProperty2"), + // "Custom property should be preserved after move for attachment: " + + // targetAttachmentId); + // } else { + // fail("Custom property missing after move for attachment: " + targetAttachmentId); + // } + // } - @Test - @Order(71) - public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { - System.out.println( - "Test (71): Move attachments with invalid or undefined secondary properties"); + // // Verify source entity still has all attachments (without sourceFacet) + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // assertEquals( + // sourceCountBeforeMove, + // sourceMetadataAfterMove.size(), + // "Source entity should still have " + // + sourceCountBeforeMove + // + " attachments (without sourceFacet)"); + + // // Clean up - delete both entities + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } - // Create source entity and add attachments - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // @Test + // @Order(71) + // public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { + // System.out.println( + // "Test (71): Move attachments with invalid or undefined secondary properties"); - // Prepare sample files - ClassLoader classLoader = getClass().getClassLoader(); - List files = new ArrayList<>(); - files.add(new File(classLoader.getResource("sample.pdf").getFile())); - files.add(new File(classLoader.getResource("sample.txt").getFile())); - files.add(new File(classLoader.getResource("WDIRSCodeList.csv").getFile())); + // // Create source entity and add attachments + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - Map postData = new HashMap<>(); - postData.put("up__ID", moveSourceEntity); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // // Prepare sample files + // ClassLoader classLoader = getClass().getClassLoader(); + // List files = new ArrayList<>(); + // files.add(new File(classLoader.getResource("sample.pdf").getFile())); + // files.add(new File(classLoader.getResource("sample.txt").getFile())); + // files.add(new File(classLoader.getResource("WDIRSCodeList.csv").getFile())); - // Create attachments in source entity - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment in source entity"); - } - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", moveSourceEntity); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // Add valid secondary properties to first attachment (customProperty2) - String validAttachmentId = sourceAttachmentIds.get(0); - Integer validCustomProperty2Value = 12345; - RequestBody validPropertyBody = - RequestBody.create( - "{\"customProperty2\": " + validCustomProperty2Value + "}", - MediaType.parse("application/json")); - - String validPropertyResponse = - api.updateSecondaryProperty( - appUrl, entityName, facetName, moveSourceEntity, validAttachmentId, validPropertyBody); - if (!validPropertyResponse.equals("Updated")) { - fail("Could not update valid property for attachment: " + validAttachmentId); - } + // // Create attachments in source entity + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in source entity"); + // } + // } - // add invalid secondary properties to second attachment (non-existent property) - String invalidAttachmentId = sourceAttachmentIds.get(1); - RequestBody invalidPropertyBody = - RequestBody.create( - "{\"nonExistentProperty\": \"invalid\"}", MediaType.parse("application/json")); - - api.updateSecondaryProperty( - appUrl, entityName, facetName, moveSourceEntity, invalidAttachmentId, invalidPropertyBody); - - // add undefined properties to third attachment - String undefinedAttachmentId = sourceAttachmentIds.get(2); - RequestBody undefinedPropertyBody = - RequestBody.create( - "{\"undefinedField\": \"test\", \"anotherUndefined\": 999}", - MediaType.parse("application/json")); - - api.updateSecondaryProperty( - appUrl, - entityName, - facetName, - moveSourceEntity, - undefinedAttachmentId, - undefinedPropertyBody); - - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // Add valid secondary properties to first attachment (customProperty2) + // String validAttachmentId = sourceAttachmentIds.get(0); + // Integer validCustomProperty2Value = 12345; + // RequestBody validPropertyBody = + // RequestBody.create( + // "{\"customProperty2\": " + validCustomProperty2Value + "}", + // MediaType.parse("application/json")); + + // String validPropertyResponse = + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, moveSourceEntity, validAttachmentId, + // validPropertyBody); + // if (!validPropertyResponse.equals("Updated")) { + // fail("Could not update valid property for attachment: " + validAttachmentId); + // } - // Fetch object IDs from source entity - moveObjectIds.clear(); - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } catch (Exception e) { - fail("Could not fetch metadata for attachment: " + attachmentId); - } - } + // // add invalid secondary properties to second attachment (non-existent property) + // String invalidAttachmentId = sourceAttachmentIds.get(1); + // RequestBody invalidPropertyBody = + // RequestBody.create( + // "{\"nonExistentProperty\": \"invalid\"}", MediaType.parse("application/json")); + + // api.updateSecondaryProperty( + // appUrl, entityName, facetName, moveSourceEntity, invalidAttachmentId, + // invalidPropertyBody); + + // // add undefined properties to third attachment + // String undefinedAttachmentId = sourceAttachmentIds.get(2); + // RequestBody undefinedPropertyBody = + // RequestBody.create( + // "{\"undefinedField\": \"test\", \"anotherUndefined\": 999}", + // MediaType.parse("application/json")); + + // api.updateSecondaryProperty( + // appUrl, + // entityName, + // facetName, + // moveSourceEntity, + // undefinedAttachmentId, + // undefinedPropertyBody); + + // // Save source entity + // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch all objectIds from source entity"); - } + // // Fetch object IDs from source entity + // moveObjectIds.clear(); + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } catch (Exception e) { + // fail("Could not fetch metadata for attachment: " + attachmentId); + // } + // } - // Get source attachment count before move - List> sourceMetadataBeforeMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch all objectIds from source entity"); + // } - // Create target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // // Get source attachment count before move + // List> sourceMetadataBeforeMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - // Save target before move - String saveTargetBeforeMoveResponse68 = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse68.equals("Saved")) { - fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse68); - } + // // Create target entity + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - // Move attachments from source to target with sourceFacet - String sourceFacet = serviceName + "." + entityName + "." + facetName; - String targetFacet = serviceName + "." + entityName + "." + facetName; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // // Save target before move + // String saveTargetBeforeMoveResponse68 = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse68.equals("Saved")) { + // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse68); + // } - // Verify attachments moved to target - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // // Move attachments from source to target with sourceFacet + // String sourceFacet = serviceName + "." + entityName + "." + facetName; + // String targetFacet = serviceName + "." + entityName + "." + facetName; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - assertTrue( - targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); - assertEquals( - sourceCountBeforeMove, - targetMetadataAfterMove.size(), - "All attachments should move (invalid properties are ignored)"); - - // Verify only allowed properties are populated in target - for (Map metadata : targetMetadataAfterMove) { - String targetAttachmentId = (String) metadata.get("ID"); - assertNotNull(targetAttachmentId, "Target attachment ID should not be null"); - - // Fetch detailed metadata to verify properties - Map detailedMetadata = - api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); - - // Check if this is the attachment with valid customProperty2 - if (detailedMetadata.containsKey("customProperty2") - && detailedMetadata.get("customProperty2") != null) { - assertEquals( - validCustomProperty2Value, - detailedMetadata.get("customProperty2"), - "Valid customProperty2 should be preserved"); - } - } + // // Verify attachments moved to target + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + + // assertTrue( + // targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); + // assertEquals( + // sourceCountBeforeMove, + // targetMetadataAfterMove.size(), + // "All attachments should move (invalid properties are ignored)"); + + // // Verify only allowed properties are populated in target + // for (Map metadata : targetMetadataAfterMove) { + // String targetAttachmentId = (String) metadata.get("ID"); + // assertNotNull(targetAttachmentId, "Target attachment ID should not be null"); + + // // Fetch detailed metadata to verify properties + // Map detailedMetadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); + + // // Check if this is the attachment with valid customProperty2 + // if (detailedMetadata.containsKey("customProperty2") + // && detailedMetadata.get("customProperty2") != null) { + // assertEquals( + // validCustomProperty2Value, + // detailedMetadata.get("customProperty2"), + // "Valid customProperty2 should be preserved"); + // } + // } - // Verify source entity has no attachments - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - assertEquals( - 0, - sourceMetadataAfterMove.size(), - "Source entity should have no attachments after move with sourceFacet"); + // // Verify source entity has no attachments + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // assertEquals( + // 0, + // sourceMetadataAfterMove.size(), + // "Source entity should have no attachments after move with sourceFacet"); + + // // Clean up - delete both entities + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // @Test + // @Order(72) + // public void testMoveAttachmentsFromSourceEntityInDraftMode() throws Exception { + // System.out.println( + // "Test (72): Move attachments from Source Entity when Source Entity is in draft mode"); - @Test - @Order(72) - public void testMoveAttachmentsFromSourceEntityInDraftMode() throws Exception { - System.out.println( - "Test (72): Move attachments from Source Entity when Source Entity is in draft mode"); + // // Create source entity and keep it in draft mode + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - // Create source entity and keep it in draft mode - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // // Prepare sample files + // ClassLoader classLoader = getClass().getClassLoader(); + // List files = new ArrayList<>(); + // files.add(new File(classLoader.getResource("sample.pdf").getFile())); + // files.add(new File(classLoader.getResource("sample.txt").getFile())); + // files.add(new File(classLoader.getResource("WDIRSCodeList.csv").getFile())); - // Prepare sample files - ClassLoader classLoader = getClass().getClassLoader(); - List files = new ArrayList<>(); - files.add(new File(classLoader.getResource("sample.pdf").getFile())); - files.add(new File(classLoader.getResource("sample.txt").getFile())); - files.add(new File(classLoader.getResource("WDIRSCodeList.csv").getFile())); + // Map postData = new HashMap<>(); + // postData.put("up__ID", moveSourceEntity); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - Map postData = new HashMap<>(); - postData.put("up__ID", moveSourceEntity); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // // Create attachments in source entity + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in source entity"); + // } + // } - // Create attachments in source entity - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment in source entity"); - } - } + // // Verify attachments are added to source entity + // int sourceCountBeforeMove = sourceAttachmentIds.size(); + // assertTrue(sourceCountBeforeMove > 0, "Source entity should have attachments before move"); + // assertEquals( + // files.size(), sourceCountBeforeMove, "Source should have " + files.size() + " + // attachments"); + + // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - // Verify attachments are added to source entity - int sourceCountBeforeMove = sourceAttachmentIds.size(); - assertTrue(sourceCountBeforeMove > 0, "Source entity should have attachments before move"); - assertEquals( - files.size(), sourceCountBeforeMove, "Source should have " + files.size() + " attachments"); + // // Fetch object IDs from source entity + // moveObjectIds.clear(); + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // // Get source folder ID from first attachment + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - // Fetch object IDs from source entity - moveObjectIds.clear(); - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - // Get source folder ID from first attachment - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // String editSourceResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // moveSourceEntity); + // if (!editSourceResponse.equals("Entity in draft mode")) { + // fail("Could not edit source entity back to draft mode"); + // } - assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // // Create target entity + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - String editSourceResponse = api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!editSourceResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity back to draft mode"); - } + // // Save target before move + // String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveTargetEntity); + // if (!saveTargetResponse.equals("Saved")) { + // fail("Could not save target entity: " + saveTargetResponse); + // } - // Create target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // // Move attachments from draft source to target using sourceFacet + // String targetFacet = serviceName + "." + entityName + "." + facetName; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // null); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - // Save target before move - String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetResponse.equals("Saved")) { - fail("Could not save target entity: " + saveTargetResponse); - } + // // Verify attachments moved to target + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // assertTrue( + // targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); + // assertEquals( + // sourceCountBeforeMove, + // targetMetadataAfterMove.size(), + // "Target should have " + sourceCountBeforeMove + " attachments after move"); + + // // Verify all expected attachments are in target + // Set targetFileNames = + // targetMetadataAfterMove.stream() + // .map(m -> (String) m.get("fileName")) + // .collect(java.util.stream.Collectors.toSet()); + + // for (File file : files) { + // assertTrue( + // targetFileNames.contains(file.getName()), + // "Target should contain attachment: " + file.getName()); + // } - // Move attachments from draft source to target using sourceFacet - String targetFacet = serviceName + "." + entityName + "." + facetName; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - null); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // // Now save the source entity + // String saveSourceAfterMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceAfterMoveResponse.equals("Saved")) { + // fail("Could not save source entity after move: " + saveSourceAfterMoveResponse); + // } - // Verify attachments moved to target - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - assertTrue( - targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); - assertEquals( - sourceCountBeforeMove, - targetMetadataAfterMove.size(), - "Target should have " + sourceCountBeforeMove + " attachments after move"); + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // assertEquals( + // sourceCountBeforeMove, + // sourceMetadataAfterMove.size(), + // "Source entity in draft mode retains attachments after move (copy behavior)"); + + // Set sourceFileNamesAfterMove = + // sourceMetadataAfterMove.stream() + // .map(m -> (String) m.get("fileName")) + // .collect(java.util.stream.Collectors.toSet()); + + // for (File file : files) { + // assertTrue( + // sourceFileNamesAfterMove.contains(file.getName()), + // "Source (draft) should still contain attachment: " + file.getName()); + // } - // Verify all expected attachments are in target - Set targetFileNames = - targetMetadataAfterMove.stream() - .map(m -> (String) m.get("fileName")) - .collect(java.util.stream.Collectors.toSet()); + // // Clean up - delete both entities + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } - for (File file : files) { - assertTrue( - targetFileNames.contains(file.getName()), - "Target should contain attachment: " + file.getName()); - } + // @Test + // @Order(73) + // public void testEditAttachmentFileNameAndMoveToTarget() throws Exception { + // System.out.println( + // "Test (73): Edit attachment file name in Source Entity and move it to Target Entity"); - // Now save the source entity - String saveSourceAfterMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceAfterMoveResponse.equals("Saved")) { - fail("Could not save source entity after move: " + saveSourceAfterMoveResponse); - } + // // Create source entity and add attachment + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - assertEquals( - sourceCountBeforeMove, - sourceMetadataAfterMove.size(), - "Source entity in draft mode retains attachments after move (copy behavior)"); + // // Add attachment with original name (sample.txt) + // ClassLoader classLoader = getClass().getClassLoader(); + // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - Set sourceFileNamesAfterMove = - sourceMetadataAfterMove.stream() - .map(m -> (String) m.get("fileName")) - .collect(java.util.stream.Collectors.toSet()); + // Map postData = new HashMap<>(); + // postData.put("up__ID", moveSourceEntity); + // postData.put("mimeType", "text/plain"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - for (File file : files) { - assertTrue( - sourceFileNamesAfterMove.contains(file.getName()), - "Source (draft) should still contain attachment: " + file.getName()); - } + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, originalFile); + // if (!createResponse.get(0).equals("Attachment created")) { + // fail("Could not create attachment in source entity"); + // } - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // String attachmentId = createResponse.get(1); + // assertNotNull(attachmentId, "Attachment ID should not be null"); - @Test - @Order(73) - public void testEditAttachmentFileNameAndMoveToTarget() throws Exception { - System.out.println( - "Test (73): Edit attachment file name in Source Entity and move it to Target Entity"); + // // Save source entity + // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - // Create source entity and add attachment - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // // Verify original filename + // List> metadataBeforeRename = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // assertEquals(1, metadataBeforeRename.size(), "Source should have 1 attachment"); + // assertEquals( + // "sample.txt", + // metadataBeforeRename.get(0).get("fileName"), + // "Original filename should be sample.txt"); + + // // Edit source entity back to draft mode + // String editSourceResponse = api.editEntityDraft(appUrl, entityName, srvpath, + // moveSourceEntity); + // if (!editSourceResponse.equals("Entity in draft mode")) { + // fail("Could not edit source entity to draft mode"); + // } - // Add attachment with original name (sample.txt) - ClassLoader classLoader = getClass().getClassLoader(); - File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // // Rename the attachment to testEdited.txt + // String newFileName = "testEdited.txt"; + // String renameResponse = + // api.renameAttachment( + // appUrl, entityName, facetName, moveSourceEntity, attachmentId, newFileName); + // assertEquals("Renamed", renameResponse, "Attachment should be renamed successfully"); + + // // Save source entity after rename + // saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity after rename: " + saveSourceResponse); + // } - Map postData = new HashMap<>(); - postData.put("up__ID", moveSourceEntity); - postData.put("mimeType", "text/plain"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // // Verify renamed filename in source + // List> metadataAfterRename = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // assertEquals(1, metadataAfterRename.size(), "Source should still have 1 attachment"); + // assertEquals( + // newFileName, + // metadataAfterRename.get(0).get("fileName"), + // "Filename should be updated to " + newFileName); + + // // Get objectId and folderId for move operation + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + // String objectId = metadata.get("objectId").toString(); + // moveSourceFolderId = metadata.get("folderId").toString(); + // assertNotNull(objectId, "Object ID should not be null"); + // assertNotNull(moveSourceFolderId, "Folder ID should not be null"); + + // moveObjectIds.clear(); + // moveObjectIds.add(objectId); + + // // Create target entity + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity"); + // } - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, originalFile); - if (!createResponse.get(0).equals("Attachment created")) { - fail("Could not create attachment in source entity"); - } + // // Save target before move + // String saveTargetBeforeMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTargetBeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity before move"); + // } - String attachmentId = createResponse.get(1); - assertNotNull(attachmentId, "Attachment ID should not be null"); + // // Move attachment from source to target with sourceFacet + // String sourceFacet = serviceName + "." + entityName + "." + facetName; + // String targetFacet = serviceName + "." + entityName + "." + facetName; + // Map moveResult = + // api.moveAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // fail("Move operation returned null result"); + // } - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // Verify attachment moved to target with renamed filename + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // assertEquals(1, targetMetadataAfterMove.size(), "Target should have 1 attachment after + // move"); + // assertEquals( + // newFileName, + // targetMetadataAfterMove.get(0).get("fileName"), + // "Target should have attachment with renamed filename: " + newFileName); + + // // Verify attachment removed from source + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // assertEquals( + // 0, + // sourceMetadataAfterMove.size(), + // "Source entity should have no attachments after move with sourceFacet"); + + // // Clean up - delete both entities + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } - // Verify original filename - List> metadataBeforeRename = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - assertEquals(1, metadataBeforeRename.size(), "Source should have 1 attachment"); - assertEquals( - "sample.txt", - metadataBeforeRename.get(0).get("fileName"), - "Original filename should be sample.txt"); - - // Edit source entity back to draft mode - String editSourceResponse = api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!editSourceResponse.equals("Entity in draft mode")) { - fail("Could not edit source entity to draft mode"); - } + // @Test + // @Order(74) + // public void testChainMoveAttachmentsFromSourceToTarget1ToTarget2() throws Exception { + // System.out.println( + // "Test (74): Move attachments from Source Entity to Target Entity 1 and then to Target + // Entity 2"); - // Rename the attachment to testEdited.txt - String newFileName = "testEdited.txt"; - String renameResponse = - api.renameAttachment( - appUrl, entityName, facetName, moveSourceEntity, attachmentId, newFileName); - assertEquals("Renamed", renameResponse, "Attachment should be renamed successfully"); - - // Save source entity after rename - saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity after rename: " + saveSourceResponse); - } + // // Create source entity and add attachments + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - // Verify renamed filename in source - List> metadataAfterRename = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - assertEquals(1, metadataAfterRename.size(), "Source should still have 1 attachment"); - assertEquals( - newFileName, - metadataAfterRename.get(0).get("fileName"), - "Filename should be updated to " + newFileName); - - // Get objectId and folderId for move operation - Map metadata = - api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - String objectId = metadata.get("objectId").toString(); - moveSourceFolderId = metadata.get("folderId").toString(); - assertNotNull(objectId, "Object ID should not be null"); - assertNotNull(moveSourceFolderId, "Folder ID should not be null"); - - moveObjectIds.clear(); - moveObjectIds.add(objectId); - - // Create target entity - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity"); - } + // // Prepare sample files + // ClassLoader classLoader = getClass().getClassLoader(); + // List files = new ArrayList<>(); + // files.add(new File(classLoader.getResource("sample.pdf").getFile())); + // files.add(new File(classLoader.getResource("sample.txt").getFile())); - // Save target before move - String saveTargetBeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTargetBeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity before move"); - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", moveSourceEntity); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // Move attachment from source to target with sourceFacet - String sourceFacet = serviceName + "." + entityName + "." + facetName; - String targetFacet = serviceName + "." + entityName + "." + facetName; - Map moveResult = - api.moveAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - fail("Move operation returned null result"); - } + // // Create attachments in source entity + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in source entity"); + // } + // } - // Verify attachment moved to target with renamed filename - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - assertEquals(1, targetMetadataAfterMove.size(), "Target should have 1 attachment after move"); - assertEquals( - newFileName, - targetMetadataAfterMove.get(0).get("fileName"), - "Target should have attachment with renamed filename: " + newFileName); + // // Save source entity + // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - // Verify attachment removed from source - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - assertEquals( - 0, - sourceMetadataAfterMove.size(), - "Source entity should have no attachments after move with sourceFacet"); + // // Get count of attachments in source + // int sourceCountInitial = sourceAttachmentIds.size(); + // assertTrue(sourceCountInitial > 0, "Source should have attachments"); - // Clean up - delete both entities - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // // Fetch object IDs from source entity + // moveObjectIds.clear(); + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // // Get source folder ID from first attachment + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } - @Test - @Order(74) - public void testChainMoveAttachmentsFromSourceToTarget1ToTarget2() throws Exception { - System.out.println( - "Test (74): Move attachments from Source Entity to Target Entity 1 and then to Target Entity 2"); + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - // Create source entity and add attachments - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); - } + // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - // Prepare sample files - ClassLoader classLoader = getClass().getClassLoader(); - List files = new ArrayList<>(); - files.add(new File(classLoader.getResource("sample.pdf").getFile())); - files.add(new File(classLoader.getResource("sample.txt").getFile())); + // // Create Target Entity 1 + // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity 1"); + // } - Map postData = new HashMap<>(); - postData.put("up__ID", moveSourceEntity); - postData.put("mimeType", "application/pdf"); - postData.put("createdAt", new Date().toString()); - postData.put("createdBy", "test@test.com"); - postData.put("modifiedBy", "test@test.com"); + // // Save target1 before move + // String saveTarget1BeforeMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + // if (!saveTarget1BeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity 1 before move"); + // } - // Create attachments in source entity - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment in source entity"); - } - } + // // Move attachments from source to Target Entity 1 with sourceFacet + // String sourceFacet = serviceName + "." + entityName + "." + facetName; + // String targetFacet = serviceName + "." + entityName + "." + facetName; + // Map moveResult1 = + // api.moveAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult1 == null) { + // fail("Move operation from source to target 1 returned null result"); + // } - // Save source entity - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } + // // Verify attachments moved to Target Entity 1 + // List> target1MetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // assertTrue( + // target1MetadataAfterMove.size() > 0, "Target entity 1 should have attachments after + // move"); + // assertEquals( + // sourceCountInitial, + // target1MetadataAfterMove.size(), + // "Target 1 should have " + sourceCountInitial + " attachments"); + + // // Verify all expected files are in Target Entity 1 + // Set target1FileNames = + // target1MetadataAfterMove.stream() + // .map(m -> (String) m.get("fileName")) + // .collect(java.util.stream.Collectors.toSet()); + + // for (File file : files) { + // assertTrue( + // target1FileNames.contains(file.getName()), + // "Target 1 should contain attachment: " + file.getName()); + // } - // Get count of attachments in source - int sourceCountInitial = sourceAttachmentIds.size(); - assertTrue(sourceCountInitial > 0, "Source should have attachments"); - - // Fetch object IDs from source entity - moveObjectIds.clear(); - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - // Get source folder ID from first attachment - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } - } + // // Verify attachments removed from source + // List> sourceMetadataAfterFirstMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // assertEquals( + // 0, + // sourceMetadataAfterFirstMove.size(), + // "Source entity should have no attachments after move to target 1"); + + // // Create Target Entity 2 + // String moveTargetEntity2 = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity2.equals("Could not create entity")) { + // fail("Could not create target entity 2"); + // } - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); - } + // // Save target2 before move + // String saveTarget2BeforeMoveResponse = + // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity2); + // if (!saveTarget2BeforeMoveResponse.equals("Saved")) { + // fail("Could not save target entity 2 before move"); + // } - assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // // Get new object IDs and folder ID from Target Entity 1 for second move + // List target1AttachmentIds = new ArrayList<>(); + // for (Map metadata : target1MetadataAfterMove) { + // String attachmentId = metadata.get("ID").toString(); + // target1AttachmentIds.add(attachmentId); + // } - // Create Target Entity 1 - moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity 1"); - } + // moveObjectIds.clear(); + // String target1FolderId = null; + // for (String attachmentId : target1AttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // // Get folder ID from first attachment + // if (target1FolderId == null && metadata.containsKey("folderId")) { + // target1FolderId = metadata.get("folderId").toString(); + // } + // } + // } catch (IOException e) { + // fail("Could not fetch attachment metadata from target 1: " + e.getMessage()); + // } + // } - // Save target1 before move - String saveTarget1BeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - if (!saveTarget1BeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity 1 before move"); - } + // assertNotNull(target1FolderId, "Target 1 folder ID should not be null"); + + // // Move attachments from Target Entity 1 to Target Entity 2 with sourceFacet + // Map moveResult2 = + // api.moveAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity2, + // target1FolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult2 == null) { + // fail("Move operation from target 1 to target 2 returned null result"); + // } - // Move attachments from source to Target Entity 1 with sourceFacet - String sourceFacet = serviceName + "." + entityName + "." + facetName; - String targetFacet = serviceName + "." + entityName + "." + facetName; - Map moveResult1 = - api.moveAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult1 == null) { - fail("Move operation from source to target 1 returned null result"); - } + // // Verify attachments moved to Target Entity 2 + // List> target2MetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity2); + // assertTrue( + // target2MetadataAfterMove.size() > 0, "Target entity 2 should have attachments after + // move"); + // assertEquals( + // sourceCountInitial, + // target2MetadataAfterMove.size(), + // "Target 2 should have " + sourceCountInitial + " attachments"); + + // // Verify all expected files are in Target Entity 2 + // Set target2FileNames = + // target2MetadataAfterMove.stream() + // .map(m -> (String) m.get("fileName")) + // .collect(java.util.stream.Collectors.toSet()); + + // for (File file : files) { + // assertTrue( + // target2FileNames.contains(file.getName()), + // "Target 2 should contain attachment: " + file.getName()); + // } - // Verify attachments moved to Target Entity 1 - List> target1MetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - assertTrue( - target1MetadataAfterMove.size() > 0, "Target entity 1 should have attachments after move"); - assertEquals( - sourceCountInitial, - target1MetadataAfterMove.size(), - "Target 1 should have " + sourceCountInitial + " attachments"); + // // Verify attachments removed from Target Entity 1 + // List> target1MetadataAfterSecondMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // assertEquals( + // 0, + // target1MetadataAfterSecondMove.size(), + // "Target entity 1 should have no attachments after move to target 2"); + + // // Clean up - delete all three entities + // api.deleteEntity(appUrl, entityName, moveTargetEntity2); + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } - // Verify all expected files are in Target Entity 1 - Set target1FileNames = - target1MetadataAfterMove.stream() - .map(m -> (String) m.get("fileName")) - .collect(java.util.stream.Collectors.toSet()); + // @Test + // @Order(75) + // public void testMoveAttachmentsWithoutSDMRole() throws Exception { + // System.out.println("Test (75): Move attachments when user does not have SDM Role"); + + // // Create source entity with SDM role and add attachments + // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveSourceEntity.equals("Could not create entity")) { + // fail("Could not create source entity"); + // } - for (File file : files) { - assertTrue( - target1FileNames.contains(file.getName()), - "Target 1 should contain attachment: " + file.getName()); - } + // // Prepare sample files + // ClassLoader classLoader = getClass().getClassLoader(); + // List files = new ArrayList<>(); + // files.add(new File(classLoader.getResource("sample.pdf").getFile())); + // files.add(new File(classLoader.getResource("sample.txt").getFile())); - // Verify attachments removed from source - List> sourceMetadataAfterFirstMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - assertEquals( - 0, - sourceMetadataAfterFirstMove.size(), - "Source entity should have no attachments after move to target 1"); - - // Create Target Entity 2 - String moveTargetEntity2 = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity2.equals("Could not create entity")) { - fail("Could not create target entity 2"); - } + // Map postData = new HashMap<>(); + // postData.put("up__ID", moveSourceEntity); + // postData.put("mimeType", "application/pdf"); + // postData.put("createdAt", new Date().toString()); + // postData.put("createdBy", "test@test.com"); + // postData.put("modifiedBy", "test@test.com"); - // Save target2 before move - String saveTarget2BeforeMoveResponse = - api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity2); - if (!saveTarget2BeforeMoveResponse.equals("Saved")) { - fail("Could not save target entity 2 before move"); - } + // // Create attachments in source entity with SDM role + // List sourceAttachmentIds = new ArrayList<>(); + // for (File file : files) { + // List createResponse = + // api.createAttachment( + // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + // if (createResponse.get(0).equals("Attachment created")) { + // sourceAttachmentIds.add(createResponse.get(1)); + // } else { + // fail("Could not create attachment in source entity"); + // } + // } - // Get new object IDs and folder ID from Target Entity 1 for second move - List target1AttachmentIds = new ArrayList<>(); - for (Map metadata : target1MetadataAfterMove) { - String attachmentId = metadata.get("ID").toString(); - target1AttachmentIds.add(attachmentId); - } + // // Save source entity with SDM role + // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, + // moveSourceEntity); + // if (!saveSourceResponse.equals("Saved")) { + // fail("Could not save source entity: " + saveSourceResponse); + // } - moveObjectIds.clear(); - String target1FolderId = null; - for (String attachmentId : target1AttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - // Get folder ID from first attachment - if (target1FolderId == null && metadata.containsKey("folderId")) { - target1FolderId = metadata.get("folderId").toString(); - } - } - } catch (IOException e) { - fail("Could not fetch attachment metadata from target 1: " + e.getMessage()); - } - } + // // Get count of attachments in source + // int sourceCountInitial = sourceAttachmentIds.size(); + // assertTrue(sourceCountInitial > 0, "Source should have attachments"); - assertNotNull(target1FolderId, "Target 1 folder ID should not be null"); + // // Fetch object IDs from source entity + // moveObjectIds.clear(); + // for (String attachmentId : sourceAttachmentIds) { + // try { + // Map metadata = + // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + // if (metadata.containsKey("objectId")) { + // moveObjectIds.add(metadata.get("objectId").toString()); + // // Get source folder ID from first attachment + // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + // moveSourceFolderId = metadata.get("folderId").toString(); + // } + // } + // } catch (IOException e) { + // fail("Could not fetch attachment metadata: " + e.getMessage()); + // } + // } - // Move attachments from Target Entity 1 to Target Entity 2 with sourceFacet - Map moveResult2 = - api.moveAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity2, - target1FolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult2 == null) { - fail("Move operation from target 1 to target 2 returned null result"); - } + // if (moveObjectIds.size() != sourceAttachmentIds.size()) { + // fail("Could not fetch object IDs for all attachments"); + // } - // Verify attachments moved to Target Entity 2 - List> target2MetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity2); - assertTrue( - target2MetadataAfterMove.size() > 0, "Target entity 2 should have attachments after move"); - assertEquals( - sourceCountInitial, - target2MetadataAfterMove.size(), - "Target 2 should have " + sourceCountInitial + " attachments"); + // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - // Verify all expected files are in Target Entity 2 - Set target2FileNames = - target2MetadataAfterMove.stream() - .map(m -> (String) m.get("fileName")) - .collect(java.util.stream.Collectors.toSet()); + // // Create target entity with no SDM role + // moveTargetEntity = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + // if (moveTargetEntity.equals("Could not create entity")) { + // fail("Could not create target entity with no SDM role"); + // } - for (File file : files) { - assertTrue( - target2FileNames.contains(file.getName()), - "Target 2 should contain attachment: " + file.getName()); - } + // // Try to move attachments from source to target using user without SDM role + // String sourceFacet = serviceName + "." + entityName + "." + facetName; + // String targetFacet = serviceName + "." + entityName + "." + facetName; + // Map moveResult = null; + // boolean moveOperationFailed = false; + // String errorMessage = null; + + // try { + // moveResult = + // apiNoRoles.moveAttachment( + // appUrl, + // entityName, + // facetName, + // moveTargetEntity, + // moveSourceFolderId, + // moveObjectIds, + // targetFacet, + // sourceFacet); + + // if (moveResult == null) { + // moveOperationFailed = true; + // errorMessage = "Move operation returned null"; + // } else if (moveResult.containsKey("error")) { + // moveOperationFailed = true; + // errorMessage = moveResult.get("error").toString(); + // } + // } catch (Exception e) { + // moveOperationFailed = true; + // errorMessage = e.getMessage(); + // } - // Verify attachments removed from Target Entity 1 - List> target1MetadataAfterSecondMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - assertEquals( - 0, - target1MetadataAfterSecondMove.size(), - "Target entity 1 should have no attachments after move to target 2"); - - // Clean up - delete all three entities - api.deleteEntity(appUrl, entityName, moveTargetEntity2); - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); - } + // // Verify move operation failed + // assertTrue(moveOperationFailed, "Move operation should fail when user does not have SDM + // role"); + // assertNotNull(errorMessage, "Error message should be present when move operation fails"); + // System.out.println("Move operation failed as expected. Error: " + errorMessage); + + // // Verify attachments are still in source entity (not moved) + // List> sourceMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // assertEquals( + // sourceCountInitial, + // sourceMetadataAfterMove.size(), + // "Source should still have all attachments after failed move"); + + // // Verify target entity has no attachments + // List> targetMetadataAfterMove = + // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + // assertEquals( + // 0, targetMetadataAfterMove.size(), "Target should have no attachments after failed + // move"); + + // // Clean up - delete both entities using SDM role + // api.deleteEntity(appUrl, entityName, moveTargetEntity); + // api.deleteEntity(appUrl, entityName, moveSourceEntity); + // } @Test - @Order(75) - public void testMoveAttachmentsWithoutSDMRole() throws Exception { - System.out.println("Test (75): Move attachments when user does not have SDM Role"); - - // Create source entity with SDM role and add attachments - moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveSourceEntity.equals("Could not create entity")) { - fail("Could not create source entity"); + @Order(76) + void testDownloadMultipleAttachments() throws IOException { + System.out.println( + "Test (76): Create entity, upload 3 attachments (pdf, txt, exe), and download all"); + boolean testStatus = false; + + // Step 1: Create entity + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create entity"); + return; } + String downloadTestEntityID = response; - // Prepare sample files ClassLoader classLoader = getClass().getClassLoader(); - List files = new ArrayList<>(); - files.add(new File(classLoader.getResource("sample.pdf").getFile())); - files.add(new File(classLoader.getResource("sample.txt").getFile())); + // Step 2: Upload pdf, txt, exe in one draft session Map postData = new HashMap<>(); - postData.put("up__ID", moveSourceEntity); - postData.put("mimeType", "application/pdf"); + postData.put("up__ID", downloadTestEntityID); postData.put("createdAt", new Date().toString()); postData.put("createdBy", "test@test.com"); postData.put("modifiedBy", "test@test.com"); - // Create attachments in source entity with SDM role - List sourceAttachmentIds = new ArrayList<>(); - for (File file : files) { - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - if (createResponse.get(0).equals("Attachment created")) { - sourceAttachmentIds.add(createResponse.get(1)); - } else { - fail("Could not create attachment in source entity"); - } - } - - // Save source entity with SDM role - String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - if (!saveSourceResponse.equals("Saved")) { - fail("Could not save source entity: " + saveSourceResponse); - } - - // Get count of attachments in source - int sourceCountInitial = sourceAttachmentIds.size(); - assertTrue(sourceCountInitial > 0, "Source should have attachments"); - - // Fetch object IDs from source entity - moveObjectIds.clear(); - for (String attachmentId : sourceAttachmentIds) { - try { - Map metadata = - api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - if (metadata.containsKey("objectId")) { - moveObjectIds.add(metadata.get("objectId").toString()); - // Get source folder ID from first attachment - if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - moveSourceFolderId = metadata.get("folderId").toString(); - } - } - } catch (IOException e) { - fail("Could not fetch attachment metadata: " + e.getMessage()); - } + // Upload pdf + postData.put("mimeType", "application/pdf"); + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List createResponse1 = + api.createAttachment( + appUrl, entityName, facetName, downloadTestEntityID, srvpath, postData, pdfFile); + if (!createResponse1.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); + fail("Could not upload sample.pdf"); + return; } + String downloadAttachmentID1 = createResponse1.get(1); - if (moveObjectIds.size() != sourceAttachmentIds.size()) { - fail("Could not fetch object IDs for all attachments"); + // Upload txt + postData.put("mimeType", "application/txt"); + File txtFile = new File(classLoader.getResource("sample.txt").getFile()); + List createResponse2 = + api.createAttachment( + appUrl, entityName, facetName, downloadTestEntityID, srvpath, postData, txtFile); + if (!createResponse2.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); + fail("Could not upload sample.txt"); + return; } + String downloadAttachmentID2 = createResponse2.get(1); - assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - - // Create target entity with no SDM role - moveTargetEntity = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (moveTargetEntity.equals("Could not create entity")) { - fail("Could not create target entity with no SDM role"); + // Upload exe + postData.put("mimeType", "application/exe"); + File exeFile = new File(classLoader.getResource("sample.exe").getFile()); + List createResponse3 = + api.createAttachment( + appUrl, entityName, facetName, downloadTestEntityID, srvpath, postData, exeFile); + if (!createResponse3.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); + fail("Could not upload sample.exe"); + return; } + String downloadAttachmentID3 = createResponse3.get(1); - // Try to move attachments from source to target using user without SDM role - String sourceFacet = serviceName + "." + entityName + "." + facetName; - String targetFacet = serviceName + "." + entityName + "." + facetName; - Map moveResult = null; - boolean moveOperationFailed = false; - String errorMessage = null; - - try { - moveResult = - apiNoRoles.moveAttachment( - appUrl, - entityName, - facetName, - moveTargetEntity, - moveSourceFolderId, - moveObjectIds, - targetFacet, - sourceFacet); - - if (moveResult == null) { - moveOperationFailed = true; - errorMessage = "Move operation returned null"; - } else if (moveResult.containsKey("error")) { - moveOperationFailed = true; - errorMessage = moveResult.get("error").toString(); - } - } catch (Exception e) { - moveOperationFailed = true; - errorMessage = e.getMessage(); + // Step 3: Save entity draft + response = api.saveEntityDraft(appUrl, entityName, srvpath, downloadTestEntityID); + if (!response.equals("Saved")) { + api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); + fail("Could not save entity draft: " + response); + return; } - // Verify move operation failed - assertTrue(moveOperationFailed, "Move operation should fail when user does not have SDM role"); - assertNotNull(errorMessage, "Error message should be present when move operation fails"); - System.out.println("Move operation failed as expected. Error: " + errorMessage); - - // Verify attachments are still in source entity (not moved) - List> sourceMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // Step 4: Select first attachment - Download button should be enabled + // Verify download works with a single attachment selection + String singleDownloadResult = + api.downloadSelectedAttachments( + appUrl, entityName, facetName, downloadTestEntityID, List.of(downloadAttachmentID1)); + JSONArray singleResultArray = new JSONArray(singleDownloadResult); + assertEquals(1, singleResultArray.length(), "Expected 1 result in download response"); + JSONObject singleResult = singleResultArray.getJSONObject(0); assertEquals( - sourceCountInitial, - sourceMetadataAfterMove.size(), - "Source should still have all attachments after failed move"); + "success", + singleResult.getString("status"), + "Download button should be enabled: single attachment download should succeed"); + assertTrue(singleResult.has("content"), "Downloaded attachment should have a content field"); + + // Step 5: Select all 3 and click download + String multiDownloadResult = + api.downloadSelectedAttachments( + appUrl, + entityName, + facetName, + downloadTestEntityID, + List.of(downloadAttachmentID1, downloadAttachmentID2, downloadAttachmentID3)); + JSONArray multiResultArray = new JSONArray(multiDownloadResult); + assertEquals(3, multiResultArray.length(), "Expected 3 results in download response"); + for (int i = 0; i < multiResultArray.length(); i++) { + JSONObject result = multiResultArray.getJSONObject(i); + assertEquals( + "success", + result.getString("status"), + "Attachment " + (i + 1) + " should download successfully"); + assertTrue( + result.has("content"), + "Attachment " + (i + 1) + " should have a content field in the response"); + } + testStatus = true; - // Verify target entity has no attachments - List> targetMetadataAfterMove = - api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - assertEquals( - 0, targetMetadataAfterMove.size(), "Target should have no attachments after failed move"); + // Clean up + api.deleteEntity(appUrl, entityName, downloadTestEntityID); - // Clean up - delete both entities using SDM role - api.deleteEntity(appUrl, entityName, moveTargetEntity); - api.deleteEntity(appUrl, entityName, moveSourceEntity); + if (!testStatus) { + fail("Multiple attachment download test failed"); + } } // @Test - // @Order(76) + // @Order(77) // void testUploadAttachmentExceedingMaximumFileSize() throws IOException { // System.out.println( // "Test (76) : Upload attachment exceeding maximum file size in references facet"); From bdd4eef8a1bc7b6c38e537eefb99260015d6f5df Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Thu, 28 May 2026 11:14:50 +0530 Subject: [PATCH 2/8] Test for draft entity --- .../cds/sdm/IntegrationTest_SingleFacet.java | 279 +++++++++++++++++- 1 file changed, 278 insertions(+), 1 deletion(-) diff --git a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java index e46839470..9c95d42c1 100644 --- a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java +++ b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java @@ -6627,8 +6627,285 @@ void testDownloadMultipleAttachments() throws IOException { } } + @Test + @Order(77) + void testDownloadButtonDisabledWhenLinkSelected() throws IOException { + System.out.println( + "Test (77): Download button enabled for pdf only; disabled when link is also selected"); + + // Step 1: Create entity (already in draft mode) + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create entity"); + return; + } + String testEntityID = response; + + ClassLoader classLoader = getClass().getClassLoader(); + + // Step 2: Upload one pdf attachment (entity is already in draft mode) + Map postData = new HashMap<>(); + postData.put("up__ID", testEntityID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, testEntityID, srvpath, postData, pdfFile); + if (!createResponse.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not upload sample.pdf"); + return; + } + String pdfAttachmentID = createResponse.get(1); + + // Step 3: Create a link attachment (entity still in draft mode) + String linkResponse = + api.createLink( + appUrl, entityName, facetName, testEntityID, "TestLink", "https://www.example.com"); + if (!linkResponse.equals("Link created successfully")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not create link attachment"); + return; + } + + // Save entity draft + response = api.saveEntityDraft(appUrl, entityName, srvpath, testEntityID); + if (!response.equals("Saved")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not save entity draft: " + response); + return; + } + + // Fetch metadata to find the link attachment ID (mimeType = "application/internet-shortcut") + List> allAttachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, testEntityID); + String linkAttachmentID = + allAttachments.stream() + .filter( + a -> "application/internet-shortcut".equalsIgnoreCase((String) a.get("mimeType"))) + .map(a -> (String) a.get("ID")) + .findFirst() + .orElse(null); + if (linkAttachmentID == null) { + api.deleteEntity(appUrl, entityName, testEntityID); + fail("Could not find link attachment in entity metadata"); + return; + } + + // Step 4: Select only the pdf → Download button should be enabled (succeeds) + String pdfOnlyResult = + api.downloadSelectedAttachments( + appUrl, entityName, facetName, testEntityID, List.of(pdfAttachmentID)); + JSONArray pdfOnlyArray = new JSONArray(pdfOnlyResult); + assertEquals(1, pdfOnlyArray.length(), "Expected 1 result when only pdf is selected"); + assertEquals( + "success", + pdfOnlyArray.getJSONObject(0).getString("status"), + "Download button should be enabled: pdf-only download should succeed"); + + // Step 5: Select both pdf and link → Download button should be disabled + // (link attachment returns error status, disabling the download) + String mixedResult = + api.downloadSelectedAttachments( + appUrl, + entityName, + facetName, + testEntityID, + List.of(pdfAttachmentID, linkAttachmentID)); + JSONArray mixedArray = new JSONArray(mixedResult); + assertEquals(2, mixedArray.length(), "Expected 2 results when pdf and link are selected"); + + // Find the result for the link attachment and assert it has error status + JSONObject linkResult = null; + for (int i = 0; i < mixedArray.length(); i++) { + JSONObject item = mixedArray.getJSONObject(i); + if (linkAttachmentID.equals(item.getString("id"))) { + linkResult = item; + break; + } + } + assertNotNull(linkResult, "Result for link attachment should be present"); + assertEquals( + "error", + linkResult.getString("status"), + "Download button should be disabled: link attachment download should return error"); + assertEquals( + "Download is not supported for link attachments", + linkResult.getString("message"), + "Error message for link attachment download should match"); + + // Clean up + api.deleteEntity(appUrl, entityName, testEntityID); + } + + @Test + @Order(78) + void testDownloadMultipleAttachmentsInDraftState() throws IOException { + System.out.println( + "Test (78): Create entity in draft state, upload 3 attachments (pdf, txt, exe), and" + + " download before saving"); + boolean testStatus = false; + + // Step 1: Create entity draft (do NOT save) + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create entity"); + return; + } + String draftEntityID = response; + + ClassLoader classLoader = getClass().getClassLoader(); + + // Step 2: Upload pdf, txt, exe while entity remains in draft state + Map postData = new HashMap<>(); + postData.put("up__ID", draftEntityID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + // Upload pdf + postData.put("mimeType", "application/pdf"); + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List createResponse1 = + api.createAttachment( + appUrl, entityName, facetName, draftEntityID, srvpath, postData, pdfFile); + if (!createResponse1.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, draftEntityID); + fail("Could not upload sample.pdf"); + return; + } + String draftAttachmentID1 = createResponse1.get(1); + + // Upload txt + postData.put("mimeType", "application/txt"); + File txtFile = new File(classLoader.getResource("sample.txt").getFile()); + List createResponse2 = + api.createAttachment( + appUrl, entityName, facetName, draftEntityID, srvpath, postData, txtFile); + if (!createResponse2.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, draftEntityID); + fail("Could not upload sample.txt"); + return; + } + String draftAttachmentID2 = createResponse2.get(1); + + // Upload exe + postData.put("mimeType", "application/exe"); + File exeFile = new File(classLoader.getResource("sample.exe").getFile()); + List createResponse3 = + api.createAttachment( + appUrl, entityName, facetName, draftEntityID, srvpath, postData, exeFile); + if (!createResponse3.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, draftEntityID); + fail("Could not upload sample.exe"); + return; + } + String draftAttachmentID3 = createResponse3.get(1); + + OkHttpClient client = + new OkHttpClient.Builder() + .connectTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .writeTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .readTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .build(); + ObjectMapper objectMapper = new ObjectMapper(); + + // Helper: build the draft download URL for a given first attachment ID + // IsActiveEntity=false targets the draft entity + java.util.function.BiFunction, String> callDraftDownload = + (firstId, ids) -> { + String url = + "https://" + + appUrl + + "/odata/v4/" + + srvpath + + "/" + + entityName + + "(ID=" + + draftEntityID + + ",IsActiveEntity=false)" + + "/" + + facetName + + "(up__ID=" + + draftEntityID + + ",ID=" + + firstId + + ",IsActiveEntity=false)" + + "/" + + srvpath + + ".downloadSelectedAttachments"; + + String idsParam = String.join(",", ids); + String jsonPayload = "{\"ids\": \"" + idsParam + "\"}"; + RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonPayload); + Request req = + new Request.Builder() + .url(url) + .post(body) + .addHeader("Authorization", "Bearer " + token) + .build(); + try (Response resp = client.newCall(req).execute()) { + if (!resp.isSuccessful()) { + throw new RuntimeException( + "Draft download failed: " + resp.code() + " - " + resp.body().string()); + } + String respBody = resp.body().string(); + Map responseMap = objectMapper.readValue(respBody, Map.class); + if (responseMap.containsKey("value")) { + return responseMap.get("value").toString(); + } + return respBody; + } catch (IOException e) { + throw new RuntimeException("Draft download error: " + e.getMessage(), e); + } + }; + + // Step 3: Select first attachment — Download button should be enabled even in draft state + String singleDownloadResult = + callDraftDownload.apply(draftAttachmentID1, List.of(draftAttachmentID1)); + JSONArray singleResultArray = new JSONArray(singleDownloadResult); + assertEquals(1, singleResultArray.length(), "Expected 1 result in download response"); + JSONObject singleResult = singleResultArray.getJSONObject(0); + assertEquals( + "success", + singleResult.getString("status"), + "Download button should be enabled in draft state: single attachment download should" + + " succeed"); + assertTrue(singleResult.has("content"), "Downloaded attachment should have a content field"); + + // Step 4: Select all 3 and download while entity is still in draft state + String multiDownloadResult = + callDraftDownload.apply( + draftAttachmentID1, + List.of(draftAttachmentID1, draftAttachmentID2, draftAttachmentID3)); + JSONArray multiResultArray = new JSONArray(multiDownloadResult); + assertEquals(3, multiResultArray.length(), "Expected 3 results in download response"); + for (int i = 0; i < multiResultArray.length(); i++) { + JSONObject result = multiResultArray.getJSONObject(i); + assertEquals( + "success", + result.getString("status"), + "Attachment " + (i + 1) + " should download successfully in draft state"); + assertTrue( + result.has("content"), + "Attachment " + (i + 1) + " should have a content field in the response"); + } + testStatus = true; + + // Clean up — entity was never saved, so delete the draft + api.deleteEntityDraft(appUrl, entityName, draftEntityID); + + if (!testStatus) { + fail("Multiple attachment download in draft state test failed"); + } + } + // @Test - // @Order(77) + // @Order(79) // void testUploadAttachmentExceedingMaximumFileSize() throws IOException { // System.out.println( // "Test (76) : Upload attachment exceeding maximum file size in references facet"); From 5ae6fdc716f20abe2c6c58bd0c19f97ee9bb218f Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Thu, 28 May 2026 12:07:23 +0530 Subject: [PATCH 3/8] Test case for unsaved entity --- .../cds/sdm/IntegrationTest_SingleFacet.java | 175 +++++++++++++++++- 1 file changed, 174 insertions(+), 1 deletion(-) diff --git a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java index 9c95d42c1..02b701864 100644 --- a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java +++ b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java @@ -6904,8 +6904,181 @@ void testDownloadMultipleAttachmentsInDraftState() throws IOException { } } + @Test + @Order(79) + void testDownloadButtonWithPdfAndLinkInDraftState() throws IOException { + System.out.println( + "Test (79): Upload pdf and link, save entity, edit entity (draft state)," + + " download button enabled for pdf only, disabled when link also selected"); + + // Step 1: Create entity draft + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create entity"); + return; + } + String testEntityID = response; + + ClassLoader classLoader = getClass().getClassLoader(); + + // Step 2: Upload one pdf attachment (entity in draft state) + Map postData = new HashMap<>(); + postData.put("up__ID", testEntityID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, testEntityID, srvpath, postData, pdfFile); + if (!createResponse.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not upload sample.pdf"); + return; + } + // Capture pdf attachment ID directly from upload response (draft state, reliable) + String pdfAttachmentID = createResponse.get(1); + + // Step 3: Create a link attachment (entity still in draft state) + String linkResponse = + api.createLink( + appUrl, entityName, facetName, testEntityID, "TestLink", "https://www.example.com"); + if (!linkResponse.equals("Link created successfully")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not create link attachment"); + return; + } + + // Fetch link attachment ID from draft metadata while still in draft state + List> draftAttachments = + api.fetchEntityMetadataDraft(appUrl, entityName, facetName, testEntityID); + String linkAttachmentID = + draftAttachments.stream() + .filter( + a -> "application/internet-shortcut".equalsIgnoreCase((String) a.get("mimeType"))) + .map(a -> (String) a.get("ID")) + .findFirst() + .orElse(null); + if (linkAttachmentID == null) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not find link attachment in draft entity metadata"); + return; + } + + // Step 4: Save entity + response = api.saveEntityDraft(appUrl, entityName, srvpath, testEntityID); + if (!response.equals("Saved")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not save entity draft: " + response); + return; + } + + // Step 5: Edit entity — puts it back into draft state + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, testEntityID); + if (!editResponse.equals("Entity in draft mode")) { + api.deleteEntity(appUrl, entityName, testEntityID); + fail("Could not put entity into edit/draft mode: " + editResponse); + return; + } + + // Inline draft download helper (IsActiveEntity=false) + OkHttpClient client = + new OkHttpClient.Builder() + .connectTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .writeTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .readTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .build(); + ObjectMapper objectMapper = new ObjectMapper(); + + java.util.function.BiFunction, String> callDraftDownload = + (firstId, ids) -> { + String url = + "https://" + + appUrl + + "/odata/v4/" + + srvpath + + "/" + + entityName + + "(ID=" + + testEntityID + + ",IsActiveEntity=false)" + + "/" + + facetName + + "(up__ID=" + + testEntityID + + ",ID=" + + firstId + + ",IsActiveEntity=false)" + + "/" + + srvpath + + ".downloadSelectedAttachments"; + String idsParam = String.join(",", ids); + String jsonPayload = "{\"ids\": \"" + idsParam + "\"}"; + RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonPayload); + Request req = + new Request.Builder() + .url(url) + .post(body) + .addHeader("Authorization", "Bearer " + token) + .build(); + try (Response resp = client.newCall(req).execute()) { + if (!resp.isSuccessful()) { + throw new RuntimeException( + "Draft download failed: " + resp.code() + " - " + resp.body().string()); + } + String respBody = resp.body().string(); + Map responseMap = objectMapper.readValue(respBody, Map.class); + if (responseMap.containsKey("value")) { + return responseMap.get("value").toString(); + } + return respBody; + } catch (IOException e) { + throw new RuntimeException("Draft download error: " + e.getMessage(), e); + } + }; + + // Step 7: Select only pdf — Download button should be enabled (succeeds) + String pdfOnlyResult = callDraftDownload.apply(pdfAttachmentID, List.of(pdfAttachmentID)); + JSONArray pdfOnlyArray = new JSONArray(pdfOnlyResult); + assertEquals(1, pdfOnlyArray.length(), "Expected 1 result when only pdf is selected"); + assertEquals( + "success", + pdfOnlyArray.getJSONObject(0).getString("status"), + "Download button should be enabled in draft state: pdf-only download should succeed"); + + // Step 8: Select pdf + link — Download button should be disabled + // (link attachment returns error status, disabling the download) + String mixedResult = + callDraftDownload.apply(pdfAttachmentID, List.of(pdfAttachmentID, linkAttachmentID)); + JSONArray mixedArray = new JSONArray(mixedResult); + assertEquals(2, mixedArray.length(), "Expected 2 results when pdf and link are selected"); + + JSONObject linkResult = null; + for (int i = 0; i < mixedArray.length(); i++) { + JSONObject item = mixedArray.getJSONObject(i); + if (linkAttachmentID.equals(item.getString("id"))) { + linkResult = item; + break; + } + } + assertNotNull(linkResult, "Result for link attachment should be present"); + assertEquals( + "error", + linkResult.getString("status"), + "Download button should be disabled in draft state: link attachment should return error"); + assertEquals( + "Download is not supported for link attachments", + linkResult.getString("message"), + "Error message for link attachment download should match"); + + // Clean up + api.deleteEntity(appUrl, entityName, testEntityID); + } + // @Test - // @Order(79) + // @Order(80) // void testUploadAttachmentExceedingMaximumFileSize() throws IOException { // System.out.println( // "Test (76) : Upload attachment exceeding maximum file size in references facet"); From 66dd1bbf36499f65bfdad6ba227cfb60c3dbf04f Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Thu, 28 May 2026 17:14:40 +0530 Subject: [PATCH 4/8] Adding tests in chapters --- ...ntegrationTest_Chapters_MultipleFacet.java | 615 ++++++++++++++++++ 1 file changed, 615 insertions(+) diff --git a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_Chapters_MultipleFacet.java b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_Chapters_MultipleFacet.java index 30b629cb7..74eeed95b 100644 --- a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_Chapters_MultipleFacet.java +++ b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_Chapters_MultipleFacet.java @@ -14,6 +14,7 @@ import java.util.stream.Collectors; import okhttp3.*; import okio.ByteString; +import org.json.JSONArray; import org.json.JSONObject; import org.junit.jupiter.api.*; @@ -6363,4 +6364,618 @@ void testMoveAttachmentsWithoutSDMRole() throws IOException { api.deleteEntity(appUrl, bookEntityName, sourceBookID); api.deleteEntity(appUrl, bookEntityName, targetBookID); } + + @Test + @Order(76) + void testDownloadMultipleAttachmentsInDraftState() throws IOException { + System.out.println( + "Test (76): Create book+chapter, upload pdf/txt/exe per facet in draft state, download" + + " before saving"); + + String draftBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (draftBookID.equals("Could not create entity")) { + fail("Could not create book"); + return; + } + String draftChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, draftBookID); + if (draftChapterID.equals("Could not create entity")) { + api.deleteEntityDraft(appUrl, bookEntityName, draftBookID); + fail("Could not create chapter"); + return; + } + + ClassLoader classLoader = getClass().getClassLoader(); + Map> facetAttachmentIds = new HashMap<>(); + for (String facetName : facet) { + List ids = new ArrayList<>(); + Map postData = new HashMap<>(); + postData.put("up__ID", draftChapterID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + postData.put("mimeType", "application/pdf"); + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List r1 = + api.createAttachment( + appUrl, chapterEntityName, facetName, draftChapterID, srvpath, postData, pdfFile); + if (!r1.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, bookEntityName, draftBookID); + fail("Could not upload sample.pdf for facet " + facetName); + return; + } + ids.add(r1.get(1)); + + postData.put("mimeType", "application/txt"); + File txtFile = new File(classLoader.getResource("sample.txt").getFile()); + List r2 = + api.createAttachment( + appUrl, chapterEntityName, facetName, draftChapterID, srvpath, postData, txtFile); + if (!r2.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, bookEntityName, draftBookID); + fail("Could not upload sample.txt for facet " + facetName); + return; + } + ids.add(r2.get(1)); + + postData.put("mimeType", "application/exe"); + File exeFile = new File(classLoader.getResource("sample.exe").getFile()); + List r3 = + api.createAttachment( + appUrl, chapterEntityName, facetName, draftChapterID, srvpath, postData, exeFile); + if (!r3.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, bookEntityName, draftBookID); + fail("Could not upload sample.exe for facet " + facetName); + return; + } + ids.add(r3.get(1)); + facetAttachmentIds.put(facetName, ids); + } + + OkHttpClient client = + new OkHttpClient.Builder() + .connectTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .writeTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .readTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .build(); + ObjectMapper objectMapper = new ObjectMapper(); + + for (String facetName : facet) { + List ids = facetAttachmentIds.get(facetName); + String baseUrl = + "https://" + + appUrl + + "/odata/v4/" + + srvpath + + "/" + + chapterEntityName + + "(ID=" + + draftChapterID + + ",IsActiveEntity=false)" + + "/" + + facetName + + "(up__ID=" + + draftChapterID + + ",ID=" + + ids.get(0) + + ",IsActiveEntity=false)" + + "/" + + srvpath + + ".downloadSelectedAttachments"; + + RequestBody singleBody = + RequestBody.create( + MediaType.parse("application/json"), "{\"ids\": \"" + ids.get(0) + "\"}"); + Request singleReq = + new Request.Builder() + .url(baseUrl) + .post(singleBody) + .addHeader("Authorization", "Bearer " + token) + .build(); + try (Response resp = client.newCall(singleReq).execute()) { + if (!resp.isSuccessful()) { + api.deleteEntityDraft(appUrl, bookEntityName, draftBookID); + fail( + "Single draft download failed for facet " + + facetName + + ": " + + resp.code() + + " - " + + resp.body().string()); + return; + } + Map responseMap = objectMapper.readValue(resp.body().string(), Map.class); + String result = + responseMap.containsKey("value") + ? responseMap.get("value").toString() + : responseMap.toString(); + JSONArray singleArray = new JSONArray(result); + assertEquals(1, singleArray.length(), "Expected 1 result for facet " + facetName); + assertEquals( + "success", + singleArray.getJSONObject(0).getString("status"), + "Download button should be enabled in draft state for facet " + facetName); + assertTrue( + singleArray.getJSONObject(0).has("content"), + "Attachment should have content field for facet " + facetName); + } + + String multiIdsParam = String.join(",", ids); + RequestBody multiBody = + RequestBody.create( + MediaType.parse("application/json"), "{\"ids\": \"" + multiIdsParam + "\"}"); + Request multiReq = + new Request.Builder() + .url(baseUrl) + .post(multiBody) + .addHeader("Authorization", "Bearer " + token) + .build(); + try (Response resp = client.newCall(multiReq).execute()) { + if (!resp.isSuccessful()) { + api.deleteEntityDraft(appUrl, bookEntityName, draftBookID); + fail( + "Multi draft download failed for facet " + + facetName + + ": " + + resp.code() + + " - " + + resp.body().string()); + return; + } + Map responseMap = objectMapper.readValue(resp.body().string(), Map.class); + String result = + responseMap.containsKey("value") + ? responseMap.get("value").toString() + : responseMap.toString(); + JSONArray multiArray = new JSONArray(result); + assertEquals(3, multiArray.length(), "Expected 3 results for facet " + facetName); + for (int j = 0; j < multiArray.length(); j++) { + assertEquals( + "success", + multiArray.getJSONObject(j).getString("status"), + "Attachment " + (j + 1) + " should download successfully for facet " + facetName); + assertTrue( + multiArray.getJSONObject(j).has("content"), + "Attachment " + (j + 1) + " should have content field for facet " + facetName); + } + } + } + + api.deleteEntityDraft(appUrl, bookEntityName, draftBookID); + } + + @Test + @Order(77) + void testDownloadButtonDisabledWithLinkInDraftState() throws IOException { + System.out.println( + "Test (77): Upload pdf and link per facet to chapter, save book, edit (draft state)," + + " pdf download enabled, pdf+link download disabled"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + return; + } + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not create chapter"); + return; + } + + ClassLoader classLoader = getClass().getClassLoader(); + Map facetPdfId = new HashMap<>(); + Map facetLinkId = new HashMap<>(); + for (String facetName : facet) { + Map postData = new HashMap<>(); + postData.put("up__ID", testChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List pdfResponse = + api.createAttachment( + appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, pdfFile); + if (!pdfResponse.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not upload pdf for facet " + facetName); + return; + } + facetPdfId.put(facetName, pdfResponse.get(1)); + + String linkResp = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "TestLink", + "https://www.example.com"); + if (!linkResp.equals("Link created successfully")) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not create link for facet " + facetName); + return; + } + + List> draftMeta = + api.fetchEntityMetadataDraft(appUrl, chapterEntityName, facetName, testChapterID); + String linkId = + draftMeta.stream() + .filter( + a -> "application/internet-shortcut".equalsIgnoreCase((String) a.get("mimeType"))) + .map(a -> (String) a.get("ID")) + .findFirst() + .orElse(null); + if (linkId == null) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not find link attachment in draft metadata for facet " + facetName); + return; + } + facetLinkId.put(facetName, linkId); + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not save book: " + saveResponse); + return; + } + + String editResponse = api.editEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!editResponse.equals("Entity in draft mode")) { + api.deleteEntity(appUrl, bookEntityName, testBookID); + fail("Could not put book into edit/draft mode: " + editResponse); + return; + } + + OkHttpClient client = + new OkHttpClient.Builder() + .connectTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .writeTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .readTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .build(); + ObjectMapper objectMapper = new ObjectMapper(); + + for (String facetName : facet) { + String pdfId = facetPdfId.get(facetName); + String linkId = facetLinkId.get(facetName); + String baseUrl = + "https://" + + appUrl + + "/odata/v4/" + + srvpath + + "/" + + chapterEntityName + + "(ID=" + + testChapterID + + ",IsActiveEntity=false)" + + "/" + + facetName + + "(up__ID=" + + testChapterID + + ",ID=" + + pdfId + + ",IsActiveEntity=false)" + + "/" + + srvpath + + ".downloadSelectedAttachments"; + + RequestBody pdfBody = + RequestBody.create(MediaType.parse("application/json"), "{\"ids\": \"" + pdfId + "\"}"); + Request pdfReq = + new Request.Builder() + .url(baseUrl) + .post(pdfBody) + .addHeader("Authorization", "Bearer " + token) + .build(); + try (Response resp = client.newCall(pdfReq).execute()) { + if (!resp.isSuccessful()) { + api.deleteEntity(appUrl, bookEntityName, testBookID); + fail( + "PDF-only draft download failed for facet " + + facetName + + ": " + + resp.code() + + " - " + + resp.body().string()); + return; + } + Map responseMap = objectMapper.readValue(resp.body().string(), Map.class); + String result = + responseMap.containsKey("value") + ? responseMap.get("value").toString() + : responseMap.toString(); + JSONArray pdfArray = new JSONArray(result); + assertEquals(1, pdfArray.length(), "Expected 1 result for pdf-only for facet " + facetName); + assertEquals( + "success", + pdfArray.getJSONObject(0).getString("status"), + "Download button should be enabled for pdf in draft state, facet " + facetName); + } + + String mixedIdsParam = pdfId + "," + linkId; + RequestBody mixedBody = + RequestBody.create( + MediaType.parse("application/json"), "{\"ids\": \"" + mixedIdsParam + "\"}"); + Request mixedReq = + new Request.Builder() + .url(baseUrl) + .post(mixedBody) + .addHeader("Authorization", "Bearer " + token) + .build(); + try (Response resp = client.newCall(mixedReq).execute()) { + if (!resp.isSuccessful()) { + api.deleteEntity(appUrl, bookEntityName, testBookID); + fail( + "Mixed draft download failed for facet " + + facetName + + ": " + + resp.code() + + " - " + + resp.body().string()); + return; + } + Map responseMap = objectMapper.readValue(resp.body().string(), Map.class); + String result = + responseMap.containsKey("value") + ? responseMap.get("value").toString() + : responseMap.toString(); + JSONArray mixedArray = new JSONArray(result); + assertEquals( + 2, mixedArray.length(), "Expected 2 results for pdf+link for facet " + facetName); + JSONObject linkResult = null; + for (int j = 0; j < mixedArray.length(); j++) { + JSONObject item = mixedArray.getJSONObject(j); + if (linkId.equals(item.getString("id"))) { + linkResult = item; + break; + } + } + assertNotNull(linkResult, "Link result should be present for facet " + facetName); + assertEquals( + "error", + linkResult.getString("status"), + "Download button should be disabled: link should return error for facet " + facetName); + assertEquals( + "Download is not supported for link attachments", + linkResult.getString("message"), + "Error message should match for facet " + facetName); + } + } + + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(78) + void testDownloadMultipleAttachmentsInActiveState() throws IOException { + System.out.println( + "Test (78): Create book+chapter, upload pdf/txt/exe per facet, save, download in active" + + " state"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + return; + } + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not create chapter"); + return; + } + + ClassLoader classLoader = getClass().getClassLoader(); + Map> facetAttachmentIds = new HashMap<>(); + for (String facetName : facet) { + List ids = new ArrayList<>(); + Map postData = new HashMap<>(); + postData.put("up__ID", testChapterID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + postData.put("mimeType", "application/pdf"); + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List r1 = + api.createAttachment( + appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, pdfFile); + if (!r1.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not upload sample.pdf for facet " + facetName); + return; + } + ids.add(r1.get(1)); + + postData.put("mimeType", "application/txt"); + File txtFile = new File(classLoader.getResource("sample.txt").getFile()); + List r2 = + api.createAttachment( + appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, txtFile); + if (!r2.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not upload sample.txt for facet " + facetName); + return; + } + ids.add(r2.get(1)); + + postData.put("mimeType", "application/exe"); + File exeFile = new File(classLoader.getResource("sample.exe").getFile()); + List r3 = + api.createAttachment( + appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, exeFile); + if (!r3.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not upload sample.exe for facet " + facetName); + return; + } + ids.add(r3.get(1)); + facetAttachmentIds.put(facetName, ids); + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not save book: " + saveResponse); + return; + } + + for (String facetName : facet) { + List ids = facetAttachmentIds.get(facetName); + + String singleResult = + api.downloadSelectedAttachments( + appUrl, chapterEntityName, facetName, testChapterID, List.of(ids.get(0))); + JSONArray singleArray = new JSONArray(singleResult); + assertEquals(1, singleArray.length(), "Expected 1 result for facet " + facetName); + assertEquals( + "success", + singleArray.getJSONObject(0).getString("status"), + "Download button should be enabled: single download should succeed for facet " + + facetName); + assertTrue( + singleArray.getJSONObject(0).has("content"), + "Downloaded attachment should have a content field for facet " + facetName); + + String multiResult = + api.downloadSelectedAttachments(appUrl, chapterEntityName, facetName, testChapterID, ids); + JSONArray multiArray = new JSONArray(multiResult); + assertEquals(3, multiArray.length(), "Expected 3 results for facet " + facetName); + for (int j = 0; j < multiArray.length(); j++) { + assertEquals( + "success", + multiArray.getJSONObject(j).getString("status"), + "Attachment " + (j + 1) + " should download successfully for facet " + facetName); + assertTrue( + multiArray.getJSONObject(j).has("content"), + "Attachment " + (j + 1) + " should have content field for facet " + facetName); + } + } + + api.deleteEntity(appUrl, bookEntityName, testBookID); + } + + @Test + @Order(79) + void testDownloadButtonDisabledWithLinkInActiveState() throws IOException { + System.out.println( + "Test (79): Upload pdf and link per facet to chapter, save book, pdf download enabled," + + " pdf+link download disabled in active state"); + + String testBookID = api.createEntityDraft(appUrl, bookEntityName, entityName2, srvpath); + if (testBookID.equals("Could not create entity")) { + fail("Could not create book"); + return; + } + String testChapterID = + api.createEntityDraft(appUrl, chapterEntityName, entityName2, srvpath, testBookID); + if (testChapterID.equals("Could not create entity")) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not create chapter"); + return; + } + + ClassLoader classLoader = getClass().getClassLoader(); + Map facetPdfId = new HashMap<>(); + for (String facetName : facet) { + Map postData = new HashMap<>(); + postData.put("up__ID", testChapterID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List pdfResponse = + api.createAttachment( + appUrl, chapterEntityName, facetName, testChapterID, srvpath, postData, pdfFile); + if (!pdfResponse.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not upload pdf for facet " + facetName); + return; + } + facetPdfId.put(facetName, pdfResponse.get(1)); + + String linkResp = + api.createLink( + appUrl, + chapterEntityName, + facetName, + testChapterID, + "TestLink", + "https://www.example.com"); + if (!linkResp.equals("Link created successfully")) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not create link for facet " + facetName); + return; + } + } + + String saveResponse = api.saveEntityDraft(appUrl, bookEntityName, srvpath, testBookID); + if (!saveResponse.equals("Saved")) { + api.deleteEntityDraft(appUrl, bookEntityName, testBookID); + fail("Could not save book: " + saveResponse); + return; + } + + for (String facetName : facet) { + String pdfId = facetPdfId.get(facetName); + + List> activeMetadata = + api.fetchEntityMetadata(appUrl, chapterEntityName, facetName, testChapterID); + String linkId = + activeMetadata.stream() + .filter( + a -> "application/internet-shortcut".equalsIgnoreCase((String) a.get("mimeType"))) + .map(a -> (String) a.get("ID")) + .findFirst() + .orElse(null); + if (linkId == null) { + api.deleteEntity(appUrl, bookEntityName, testBookID); + fail("Could not find link attachment in active metadata for facet " + facetName); + return; + } + + String pdfOnlyResult = + api.downloadSelectedAttachments( + appUrl, chapterEntityName, facetName, testChapterID, List.of(pdfId)); + JSONArray pdfArray = new JSONArray(pdfOnlyResult); + assertEquals(1, pdfArray.length(), "Expected 1 result for pdf-only for facet " + facetName); + assertEquals( + "success", + pdfArray.getJSONObject(0).getString("status"), + "Download button should be enabled: pdf download should succeed for facet " + facetName); + + String mixedResult = + api.downloadSelectedAttachments( + appUrl, chapterEntityName, facetName, testChapterID, List.of(pdfId, linkId)); + JSONArray mixedArray = new JSONArray(mixedResult); + assertEquals( + 2, mixedArray.length(), "Expected 2 results for pdf+link for facet " + facetName); + JSONObject linkResult = null; + for (int j = 0; j < mixedArray.length(); j++) { + JSONObject item = mixedArray.getJSONObject(j); + if (linkId.equals(item.getString("id"))) { + linkResult = item; + break; + } + } + assertNotNull(linkResult, "Link result should be present for facet " + facetName); + assertEquals( + "error", + linkResult.getString("status"), + "Download button should be disabled: link should return error for facet " + facetName); + assertEquals( + "Download is not supported for link attachments", + linkResult.getString("message"), + "Error message should match for facet " + facetName); + } + + api.deleteEntity(appUrl, bookEntityName, testBookID); + } } From 96287896674b044f4338bf9ccfd9912d09c244b9 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Thu, 28 May 2026 19:18:13 +0530 Subject: [PATCH 5/8] Changes to fix spotless --- .../cds/sdm/IntegrationTest_SingleFacet.java | 11314 ++++++++-------- 1 file changed, 5299 insertions(+), 6015 deletions(-) diff --git a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java index 02b701864..824b0d134 100644 --- a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java +++ b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java @@ -9,9 +9,11 @@ import java.nio.charset.StandardCharsets; import java.nio.file.Files; import java.nio.file.StandardCopyOption; +import java.time.LocalDateTime; import java.util.*; +import java.util.stream.Collectors; import okhttp3.*; -import org.json.JSONArray; +import okio.ByteString; import org.json.JSONObject; import org.junit.jupiter.api.*; @@ -538,6547 +540,5829 @@ void testCreateAttachmentWithRestrictedCharacterInFilename() throws IOException } } - // @Test - // @Order(10) - // void testDraftUpdateWithFileUploadDeleteAndCreate() throws IOException { - // System.out.println("Test (10): Upload attachments, delete one and create entity"); + @Test + @Order(10) + void testDraftUpdateWithFileUploadDeleteAndCreate() throws IOException { + System.out.println("Test (10): Upload attachments, delete one and create entity"); - // boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (response != "Could not create entity") { - - // entityID5 = response; - // ClassLoader classLoader = getClass().getClassLoader(); - - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Map postData1 = new HashMap<>(); - // postData1.put("up__ID", entityID5); - // postData1.put("mimeType", "application/pdf"); - // postData1.put("createdAt", new Date().toString()); - // postData1.put("createdBy", "test@test.com"); - // postData1.put("modifiedBy", "test@test.com"); - - // List createResponse1 = - // api.createAttachment(appUrl, entityName, facetName, entityID5, srvpath, postData1, - // file); - // if (createResponse1.get(0).equals("Attachment created")) { - // attachmentID7 = createResponse1.get(1); - // } + boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response != "Could not create entity") { - // file = new File(classLoader.getResource("sample.txt").getFile()); - // Map postData2 = new HashMap<>(); - // postData2.put("up__ID", entityID5); - // postData2.put("mimeType", "application/txt"); - // postData2.put("createdAt", new Date().toString()); - // postData2.put("createdBy", "test@test.com"); - // postData2.put("modifiedBy", "test@test.com"); - - // List createResponse2 = - // api.createAttachment(appUrl, entityName, facetName, entityID5, srvpath, postData2, - // file); - // if (createResponse2.get(0).equals("Attachment created")) { - // attachmentID8 = createResponse2.get(1); - // } - // response = api.deleteAttachment(appUrl, entityName, facetName, entityID5, attachmentID8); - // if (response.equals("Deleted")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); + entityID5 = response; + ClassLoader classLoader = getClass().getClassLoader(); - // if (response.equals("Saved")) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("Failed to create entity after deleting one attachment"); - // } - // } + File file = new File(classLoader.getResource("sample.pdf").getFile()); + Map postData1 = new HashMap<>(); + postData1.put("up__ID", entityID5); + postData1.put("mimeType", "application/pdf"); + postData1.put("createdAt", new Date().toString()); + postData1.put("createdBy", "test@test.com"); + postData1.put("modifiedBy", "test@test.com"); + + List createResponse1 = + api.createAttachment(appUrl, entityName, facetName, entityID5, srvpath, postData1, file); + if (createResponse1.get(0).equals("Attachment created")) { + attachmentID7 = createResponse1.get(1); + } - // @Test - // @Order(11) - // void testUpdateEntityDraft() throws IOException { - // System.out.println("Test (11): Update entity in draft"); - // boolean testStatus = false; - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new - // File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); + file = new File(classLoader.getResource("sample.txt").getFile()); + Map postData2 = new HashMap<>(); + postData2.put("up__ID", entityID5); + postData2.put("mimeType", "application/txt"); + postData2.put("createdAt", new Date().toString()); + postData2.put("createdBy", "test@test.com"); + postData2.put("modifiedBy", "test@test.com"); + + List createResponse2 = + api.createAttachment(appUrl, entityName, facetName, entityID5, srvpath, postData2, file); + if (createResponse2.get(0).equals("Attachment created")) { + attachmentID8 = createResponse2.get(1); + } + response = api.deleteAttachment(appUrl, entityName, facetName, entityID5, attachmentID8); + if (response.equals("Deleted")) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); - // File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); - // Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + if (response.equals("Saved")) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("Failed to create entity after deleting one attachment"); + } + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID5); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + @Test + @Order(11) + void testUpdateEntityDraft() throws IOException { + System.out.println("Test (11): Update entity in draft"); + boolean testStatus = false; + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(Objects.requireNonNull(classLoader.getResource("sample.pdf")).getFile()); - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID5); - // if (response.equals("Entity in draft mode")) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, entityID5, srvpath, postData, tempFile); - // String check = createResponse.get(0); - // if (check.equals("Attachment created")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); - // if (response.equals("Saved")) { - // testStatus = true; - // } - // } - // } - // if (!testStatus) { - // fail("update entity draft with uploading attachment failed"); - // } - // api.deleteEntity(appUrl, entityName, entityID5); - // } + File tempFile = new File(System.getProperty("java.io.tmpdir"), "sample3.pdf"); + Files.copy(file.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); - // @Test - // @Order(12) - // void testRenameSingleAttachment() { - // System.out.println("Test (12) : Rename single attachment"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // String name = "sample123"; - // if (response == "Entity in draft mode") { - // response = api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, - // name); - // if (response.equals("Renamed")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Saved")) { - // testStatus = true; - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } - // if (!testStatus) { - // fail("Attachment was not renamed"); - // } - // } + Map postData = new HashMap<>(); + postData.put("up__ID", entityID5); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // @Test - // @Order(13) - // void testRenameAttachmentWithUnsupportedCharacter() { - // System.out.println("Test (13) : Rename single attachment with unsupported characters"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // String name = "invalid/name"; - // if (response == "Entity in draft mode") { - // response = api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, - // name); - // if (response.equals("Renamed")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"invalid/name\\\" contains unsupported - // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\"}}"; - // if (response.equals(expected)) { - // api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, - // "sample123"); - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if ("Saved".equals(response)) testStatus = true; - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } - // if (!testStatus) { - // fail("Attachment was renamed with unsupported characters"); - // } - // } + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID5); + if (response.equals("Entity in draft mode")) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, entityID5, srvpath, postData, tempFile); + String check = createResponse.get(0); + if (check.equals("Attachment created")) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID5); + if (response.equals("Saved")) { + testStatus = true; + } + } + } + if (!testStatus) { + fail("update entity draft with uploading attachment failed"); + } + api.deleteEntity(appUrl, entityName, entityID5); + } - // @Test - // @Order(14) - // void testRenameMultipleAttachments() { - // System.out.println("Test (14) : Rename multiple attachments"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // String name1 = "sample1234"; - // String name2 = "sample12345"; - // if (response == "Entity in draft mode") { - // String response1 = - // api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID2, name1); - // String response2 = - // api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID3, name2); - // if (response1.equals("Renamed") && response2.equals("Renamed")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Saved")) { - // testStatus = true; - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } - // if (!testStatus) { - // fail("Attachment was not renamed"); - // } - // } + @Test + @Order(12) + void testRenameSingleAttachment() { + System.out.println("Test (12) : Rename single attachment"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + String name = "sample123"; + if (response == "Entity in draft mode") { + response = api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, name); + if (response.equals("Renamed")) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (response.equals("Saved")) { + testStatus = true; + } + } else { + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } + if (!testStatus) { + fail("Attachment was not renamed"); + } + } - // @Test - // @Order(15) - // void testRenameSingleAttachmentDuplicate() { - // System.out.println("Test (15) : Rename single attachment duplicate"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // String name = "sample123"; - // String name2 = "sample123456"; - // if (response == "Entity in draft mode") { - // response = api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID3, - // name); - // if (response.equals("Renamed")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"sample123\\\" already - // exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\"}}"; - // if (response.equals(expected)) { - // response = - // api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID3, - // name2); - // if (response.equals("Renamed")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response.equals("Saved")) { - // testStatus = true; - // } - // } - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } - // if (!testStatus) { - // fail("Attachment was renamed"); - // } - // } + @Test + @Order(13) + void testRenameAttachmentWithUnsupportedCharacter() { + System.out.println("Test (13) : Rename single attachment with unsupported characters"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + String name = "invalid/name"; + if (response == "Entity in draft mode") { + response = api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, name); + if (response.equals("Renamed")) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + "{\"error\":{\"code\":\"400\",\"message\":\"\\\"invalid/name\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; + if (response.equals(expected)) { + api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, "sample123"); + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if ("Saved".equals(response)) testStatus = true; + } + } else { + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } + if (!testStatus) { + fail("Attachment was renamed with unsupported characters"); + } + } - // @Test - // @Order(16) - // void testRenameMultipleAttachmentsWithOneUnsupportedCharacter() { - // System.out.println( - // "Test (16) : Rename multiple attachments where one name has unsupported characters"); - // Boolean testStatus = false; - - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - - // if (response.equals("Entity in draft mode")) { - // String validName1 = "valid_attachment1.pdf"; - // String invalidName2 = "invalid/attachment2.pdf"; - - // String renameResponse1 = - // api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, - // validName1); - // String renameResponse2 = - // api.renameAttachment( - // appUrl, entityName, facetName, entityID, attachmentID2, invalidName2); - - // if (renameResponse1.equals("Renamed") && renameResponse2.equals("Renamed")) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"invalid/attachment2.pdf\\\" contains - // unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\"}}"; - // if (response.equals(expected)) { - // api.renameAttachment( - // appUrl, entityName, facetName, entityID, attachmentID2, "sample1234"); - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if ("Saved".equals(response)) testStatus = true; - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } + @Test + @Order(14) + void testRenameMultipleAttachments() { + System.out.println("Test (14) : Rename multiple attachments"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + String name1 = "sample1234"; + String name2 = "sample12345"; + if (response == "Entity in draft mode") { + String response1 = + api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID2, name1); + String response2 = + api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID3, name2); + if (response1.equals("Renamed") && response2.equals("Renamed")) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (response.equals("Saved")) { + testStatus = true; + } + } else { + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } + if (!testStatus) { + fail("Attachment was not renamed"); + } + } - // if (!testStatus) { - // fail("Multiple renames should have failed due to one unsupported characters"); - // } - // } + @Test + @Order(15) + void testRenameSingleAttachmentDuplicate() { + System.out.println("Test (15) : Rename single attachment duplicate"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + String name = "sample123"; + String name2 = "sample123456"; + if (response == "Entity in draft mode") { + response = api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID3, name); + if (response.equals("Renamed")) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"sample123\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; + if (response.equals(expected)) { + response = + api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID3, name2); + if (response.equals("Renamed")) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (response.equals("Saved")) { + testStatus = true; + } + } + } + } else { + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } + if (!testStatus) { + fail("Attachment was renamed"); + } + } - // @Test - // @Order(17) - // void testRenameSingleAttachmentWithoutSDMRole() throws IOException { - // System.out.println("Test (17) : Rename attachments where user don't have SDM Roles"); - // boolean testStatus = false; - // String apiResponse = apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, entityID); - // String name = "sample123"; // Renaming the attachment - // if (apiResponse == "Entity in draft mode") { - // apiResponse = - // apiNoRoles.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, - // name); - // if (apiResponse.equals("Renamed")) { - // apiResponse = apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // String expected = - // "[{\"code\":\"\",\"message\":\"Could not update the following files. \\n" - // + // - // "\\n" - // + // - // "\\t\\u2022 valid_attachment1.pdf\\n" - // + // - // "\\n" - // + // - // "You do not have the required permissions to update attachments. Kindly contact - // the admin\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - // if (apiResponse.equals(expected)) { - // testStatus = true; - // } - // } else { - // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // } - // } - // if (!testStatus) { - // fail("Attachment got renamed without SDM roles."); - // } - // } + @Test + @Order(16) + void testRenameMultipleAttachmentsWithOneUnsupportedCharacter() { + System.out.println( + "Test (16) : Rename multiple attachments where one name has unsupported characters"); + Boolean testStatus = false; - // @Test - // @Order(18) - // void testRenameToValidateNames() throws IOException { - // System.out.println("Test (18) : Rename attachments to validate names"); - // boolean testStatus = false, successCount = true; - // String generatedID = ""; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // entityID3 = response; - // String[] filetoUpload = {"sample.pdf", "sample.txt", "sample.exe", "sample2.pdf"}; - // String[] names = {"Restricted/Character", " ", "duplicateName.pdf", - // "duplicateName.pdf"}; - - // ClassLoader classLoader = getClass().getClassLoader(); - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID3); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // for (int i = 0; i < filetoUpload.length; i++) { - // File file = new File(classLoader.getResource(filetoUpload[i]).getFile()); - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData, - // file); - // generatedID = createResponse.get(1); - // response = - // api.renameAttachment(appUrl, entityName, facetName, entityID3, generatedID, - // names[i]); - // successCount &= "Renamed".equals(response); - // } - // if (successCount) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // String expected = - // "{\"error\":{\"code\":\"400\",\"message\":\"The object name cannot be empty or - // consist entirely of space characters. Enter a value.\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"Restricted/Character\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An object named \\\"duplicateName.pdf\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; - // if (response.equals(expected)) { - // response = api.deleteEntityDraft(appUrl, entityName, entityID3); - // if (response.equals("Entity Draft Deleted")) testStatus = true; - // } - // } - // if (!testStatus) fail("Could not create entity"); - // } else { - // fail("Could not create entity"); - // return; - // } - // } + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // @Test - // @Order(19) - // void testDeleteSingleAttachment() throws IOException { - // System.out.println("Test (19) : Delete single attachment"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response == "Entity in draft mode") { - // response = api.deleteAttachment(appUrl, entityName, facetName, entityID, attachmentID1); - // if (response == "Deleted") { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response == "Saved") { - // response = api.readAttachment(appUrl, entityName, facetName, entityID, attachmentID1); - // if (response.equals("Could not read Attachment")) { - // testStatus = true; - // } - // } - // } - // } - // if (!testStatus) { - // fail("Could not read Attachment"); - // } - // } + if (response.equals("Entity in draft mode")) { + String validName1 = "valid_attachment1.pdf"; + String invalidName2 = "invalid/attachment2.pdf"; - // @Test - // @Order(20) - // void testDeleteMultipleAttachments() throws IOException { - // System.out.println("Test (20) : Delete multiple attachments"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response == "Entity in draft mode") { - // String response1 = - // api.deleteAttachment(appUrl, entityName, facetName, entityID, attachmentID2); - // String response2 = - // api.deleteAttachment(appUrl, entityName, facetName, entityID, attachmentID3); - // if (response1 == "Deleted" && response2 == "Deleted") { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); - // if (response == "Saved") { - // response1 = api.readAttachment(appUrl, entityName, facetName, entityID, attachmentID2); - // response2 = api.readAttachment(appUrl, entityName, facetName, entityID, attachmentID3); - // if (response1.equals("Could not read Attachment") - // && response2.equals("Could not read Attachment")) { - // testStatus = true; - // } - // } - // } - // } - // if (!testStatus) { - // fail("Could not delete attachment"); - // } - // } + String renameResponse1 = + api.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, validName1); + String renameResponse2 = + api.renameAttachment( + appUrl, entityName, facetName, entityID, attachmentID2, invalidName2); - // @Test - // @Order(21) - // void testUploadBlockedMimeType() throws IOException { - // System.out.println("Test (21): Upload blocked mimeType .rtf"); - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // entityID2 = response; - - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new - // File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID2); - // postData.put("mimeType", "application/rtf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, entityID2, srvpath, postData, - // file); - // String actualResponse = createResponse.get(0); - // String expectedJson = - // "{\"error\":{\"code\":\"500\",\"message\":\"This file type is not allowed in this - // repository. Contact your administrator for assistance.\"}}"; - - // if (expectedJson.equals(actualResponse)) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - // if ("Saved".equals(response)) { - // testStatus = true; - // } - // } else { - // api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); - // } - // } - // if (!testStatus) { - // fail("Attachment got uploaded with blocked .rtf MIME type"); - // } - // } + if (renameResponse1.equals("Renamed") && renameResponse2.equals("Renamed")) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + "{\"error\":{\"code\":\"400\",\"message\":\"\\\"invalid/attachment2.pdf\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; + if (response.equals(expected)) { + api.renameAttachment( + appUrl, entityName, facetName, entityID, attachmentID2, "sample1234"); + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if ("Saved".equals(response)) testStatus = true; + } + } else { + api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } - // @Test - // @Order(22) - // void testDeleteEntity() { - // System.out.println("Test (22) : Delete entity"); - // Boolean testStatus = false; - // String response = api.deleteEntity(appUrl, entityName, entityID); - // String response2 = api.deleteEntity(appUrl, entityName, entityID2); - // if (response == "Entity Deleted" && response2 == "Entity Deleted") { - // testStatus = true; - // } - // if (!testStatus) { - // fail("Could not delete entity"); - // } - // } + if (!testStatus) { + fail("Multiple renames should have failed due to one unsupported characters"); + } + } - // @Test - // @Order(23) - // void testUpdateValidSecondaryProperty_beforeEntityIsSaved_singleAttachment() throws IOException - // { - // System.out.println("Test (23): Rename & Update secondary property before entity is saved"); - // System.out.println("Creating entity"); - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (response != "Could not create entity") { - // entityID3 = response; - // System.out.println("Entity created"); - // System.out.println("Creating attachment"); - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID3); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData, - // file); - // String check = createResponse.get(0); - // if (check.equals("Attachment created")) { - // attachmentID1 = createResponse.get(1); - // System.out.println("Attachment created"); - // String name1 = "sample1234.pdf"; - // String secondaryPropertyString = "sample12345"; - // Integer secondaryPropertyInt = 1234; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // System.out.println("Renaming and updating secondary properties for attachment"); - // String response1 = - // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - // // Update secondary properties for String - // String dropdownValue1 = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - // // Update secondary properties for DateTime - // RequestBody bodyDateTime = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // // Update secondary properties for Boolean - // RequestBody bodyBoolean = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - // if (response1 == "Renamed" - // && updateSecondaryPropertyResponse1 == "Updated" - // && updateSecondaryPropertyResponse2 == "Updated" - // && updateSecondaryPropertyResponse3 == "Updated" - // && updateSecondaryPropertyResponse4 == "Updated") { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // if (response.equals("Saved")) { - // System.out.println("Entity saved"); - // testStatus = true; - // System.out.println("Renamed & updated Secondary properties for attachment"); - // } - // } - // } - // } - // if (!testStatus) { - // fail("Could not update secondary property before entity is saved"); - // } - // } + @Test + @Order(17) + void testRenameSingleAttachmentWithoutSDMRole() throws IOException { + System.out.println("Test (17) : Rename attachments where user don't have SDM Roles"); + boolean testStatus = false; + String apiResponse = apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, entityID); + String name = "sample123"; // Renaming the attachment + if (apiResponse == "Entity in draft mode") { + apiResponse = + apiNoRoles.renameAttachment(appUrl, entityName, facetName, entityID, attachmentID1, name); + if (apiResponse.equals("Renamed")) { + apiResponse = apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); + String expected = + "[{\"code\":\"\",\"message\":\"Could not update the following files. \\n" + + // + "\\n" + + // + "\\t\\u2022 valid_attachment1.pdf\\n" + + // + "\\n" + + // + "You do not have the required permissions to update attachments. Kindly contact the admin\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + if (apiResponse.equals(expected)) { + testStatus = true; + } + } else { + apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, entityID); + } + } + if (!testStatus) { + fail("Attachment got renamed without SDM roles."); + } + } - // @Test - // @Order(24) - // void testUpdateValidSecondaryProperty_afterEntityIsSaved_singleAttachment() { - // System.out.println("Test (24): Rename & Update secondary property after entity is saved"); - // System.out.println("Editing entity"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - // if (response == "Entity in draft mode") { - // String name1 = "sample.pdf"; - // String secondaryPropertyString = "sample"; - // Integer secondaryPropertyInt = 12; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // System.out.println("Renaming and updating secondary properties for attachment"); - // String response1 = - // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - // // Update secondary properties for String - // String dropdownValue1 = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - // // Update secondary properties for DateTime - // RequestBody bodyDateTime = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // // Update secondary properties for Boolean - // RequestBody bodyBoolean = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - // if (response1 == "Renamed" - // && updateSecondaryPropertyResponse1 == "Updated" - // && updateSecondaryPropertyResponse2 == "Updated" - // && updateSecondaryPropertyResponse3 == "Updated" - // && updateSecondaryPropertyResponse4 == "Updated") { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // if (response.equals("Saved")) { - // System.out.println("Entity saved"); - // testStatus = true; - // System.out.println("Renamed & updated Secondary properties for attachment"); - // } - // } - // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - // if (deleteEntityResponse != "Entity Deleted") { - // fail("Could not delete entity"); - // } - // } - // if (!testStatus) { - // fail("Could not update secondary property after entity is saved"); - // } - // } + @Test + @Order(18) + void testRenameToValidateNames() throws IOException { + System.out.println("Test (18) : Rename attachments to validate names"); + boolean testStatus = false, successCount = true; + String generatedID = ""; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + entityID3 = response; + String[] filetoUpload = {"sample.pdf", "sample.txt", "sample.exe", "sample2.pdf"}; + String[] names = {"Restricted/Character", " ", "duplicateName.pdf", "duplicateName.pdf"}; - // @Test - // @Order(25) - // void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_singleAttachment() - // throws IOException { - // System.out.println( - // "Test (25): Rename & Update invalid secondary property before entity is saved"); - // System.out.println("Creating entity"); - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!"Could not create entity".equals(response)) { - // entityID3 = response; - // System.out.println("Entity created"); - // System.out.println("Creating attachment"); - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID3); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData, - // file); - // String check = createResponse.get(0); - // if ("Attachment created".equals(check)) { - // attachmentID1 = createResponse.get(1); - // System.out.println("Attachment created"); - // String name1 = "sample1234.pdf"; - - // // Dropdown values for secondaryPropertyString - // String[] dropdownValues = {"A", "B", "C"}; - // // Select one dropdown value (e.g., "A") - // String secondaryPropertyString = dropdownValues[0]; - - // Integer secondaryPropertyInt = 1234; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // String invalidProperty = "testid"; - - // System.out.println("Renaming and updating invalid secondary properties for attachment"); - // String response1 = - // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - - // // Update secondary properties for String using dropdown selected value as object with - // code - - // String dropdownValue1 = integrationTestUtils.getDropDownValue(); - // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - // RequestBody bodyDropdown1 = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown1); - - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - - // // Update secondary properties for DateTime - // RequestBody bodyDateTime = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - - // // Update secondary properties for Boolean - // RequestBody bodyBoolean = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - - // // Update invalid secondary property - // String updateSecondaryPropertyResponse5 = - // api.updateInvalidSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, invalidProperty); - - // if ("Renamed".equals(response1) - // && "Updated".equals(updateSecondaryPropertyResponse1) - // && "Updated".equals(updateSecondaryPropertyResponse2) - // && "Updated".equals(updateSecondaryPropertyResponse3) - // && "Updated".equals(updateSecondaryPropertyResponse4) - // && "Updated".equals(updateSecondaryPropertyResponse5)) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // Map attachmentMetadata = - // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); - // assertEquals("sample.pdf", attachmentMetadata.get("fileName")); - // assertNull(attachmentMetadata.get("customProperty3")); - // assertNull(attachmentMetadata.get("customProperty4")); - // assertNull(attachmentMetadata.get("customProperty1_code")); - // assertNull(attachmentMetadata.get("customProperty2")); - // assertNull(attachmentMetadata.get("customProperty6")); - // assertNull(attachmentMetadata.get("customProperty5")); - - // String expectedResponse = - // "[{\"code\":\"\",\"message\":\"The following secondary properties are not - // supported.\\n" - // + // - // "\\n" - // + // - // "\\t\\u2022 id1\\n" - // + // - // "\\n" - // + // - // "Please contact your administrator for assistance with any necessary - // adjustments.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - // if (response.equals(expectedResponse)) { - // System.out.println("Entity saved"); - // testStatus = true; - // System.out.println( - // "Rename & update secondary properties for attachment is unsuccessfull"); - // } - // } - // } - // } - // if (!testStatus) { - // fail("Could not update secondary property before entity is saved"); - // } - // } + ClassLoader classLoader = getClass().getClassLoader(); + Map postData = new HashMap<>(); + postData.put("up__ID", entityID3); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // @Test - // @Order(26) - // void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_singleAttachment() throws - // IOException { - // System.out.println( - // "Test (26): Rename & Update invalid secondary property after entity is saved"); - // System.out.println("Editing entity"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - // if (response == "Entity in draft mode") { - // String name1 = "sample.pdf"; - // String secondaryPropertyString = "A"; - // Integer secondaryPropertyInt = 12; - // LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); - // String invalidProperty = "testidinvalid"; - // System.out.println("Renaming and updating invalid secondary properties for attachment"); - // String response1 = - // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - // // Update secondary properties for DateTime - // RequestBody bodyDateTime = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); - // String updateSecondaryPropertyResponse3 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // // Update secondary properties for Boolean - // RequestBody bodyBoolean = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse4 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - // // Update invalid secondary property - // String updateSecondaryPropertyResponse5 = - // api.updateInvalidSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, invalidProperty); - // if (response1 == "Renamed" - // && updateSecondaryPropertyResponse1 == "Updated" - // && updateSecondaryPropertyResponse2 == "Updated" - // && updateSecondaryPropertyResponse3 == "Updated" - // && updateSecondaryPropertyResponse4 == "Updated" - // && updateSecondaryPropertyResponse5 == "Updated") { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // Map attachmentMetadata = - // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); - // assertEquals("sample.pdf", attachmentMetadata.get("fileName")); - // assertNull(attachmentMetadata.get("customProperty3")); - // assertNull(attachmentMetadata.get("customProperty4")); - // assertNull(attachmentMetadata.get("customProperty1_code")); - // assertNull(attachmentMetadata.get("customProperty2")); - // assertNull(attachmentMetadata.get("customProperty6")); - // assertNull(attachmentMetadata.get("customProperty5")); - - // String expectedResponse = - // "[{\"code\":\"\",\"message\":\"The following secondary properties are not - // supported.\\n" - // + // - // "\\n" - // + // - // "\\t\\u2022 id1\\n" - // + // - // "\\n" - // + // - // "Please contact your administrator for assistance with any necessary - // adjustments.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - // if (response.equals(expectedResponse)) { - // System.out.println("Entity saved"); - // testStatus = true; - // System.out.println( - // "Rename & update secondary properties for attachment is unsuccessfull"); - // } - // } - // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - // if (deleteEntityResponse != "Entity Deleted") { - // fail("Could not delete entity"); - // } - // } - // if (!testStatus) { - // fail("Could not update secondary property before entity is saved"); - // } - // } + for (int i = 0; i < filetoUpload.length; i++) { + File file = new File(classLoader.getResource(filetoUpload[i]).getFile()); + List createResponse = + api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData, file); + generatedID = createResponse.get(1); + response = + api.renameAttachment(appUrl, entityName, facetName, entityID3, generatedID, names[i]); + successCount &= "Renamed".equals(response); + } + if (successCount) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + String expected = + "{\"error\":{\"code\":\"400\",\"message\":\"The object name cannot be empty or consist entirely of space characters. Enter a value.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"details\":[{\"code\":\"\",\"message\":\"\\\"Restricted/Character\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4},{\"code\":\"\",\"message\":\"An object named \\\"duplicateName.pdf\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"@Common.numericSeverity\":4}]}}"; + if (response.equals(expected)) { + response = api.deleteEntityDraft(appUrl, entityName, entityID3); + if (response.equals("Entity Draft Deleted")) testStatus = true; + } + } + if (!testStatus) fail("Could not create entity"); + } else { + fail("Could not create entity"); + return; + } + } - // @Test - // @Order(27) - // void testUpdateValidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() - // throws IOException { - // System.out.println( - // "Test (27): Rename & Update valid secondary properties for multiple attachments before - // entity is saved"); - // System.out.println("Creating entity"); - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (response != "Could not create entity") { - // entityID3 = response; - - // System.out.println("Entity created"); - - // System.out.println("Creating attachment PDF"); - // ClassLoader classLoader = getClass().getClassLoader(); - - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Map postData1 = new HashMap<>(); - // postData1.put("up__ID", entityID3); - // postData1.put("mimeType", "application/pdf"); - // postData1.put("createdAt", new Date().toString()); - // postData1.put("createdBy", "test@test.com"); - // postData1.put("modifiedBy", "test@test.com"); - - // List createResponse1 = - // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData1, - // file); - // if (createResponse1.get(0).equals("Attachment created")) { - // attachmentID1 = createResponse1.get(1); - // System.out.println("Attachment created"); - // } + @Test + @Order(19) + void testDeleteSingleAttachment() throws IOException { + System.out.println("Test (19) : Delete single attachment"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if (response == "Entity in draft mode") { + response = api.deleteAttachment(appUrl, entityName, facetName, entityID, attachmentID1); + if (response == "Deleted") { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (response == "Saved") { + response = api.readAttachment(appUrl, entityName, facetName, entityID, attachmentID1); + if (response.equals("Could not read Attachment")) { + testStatus = true; + } + } + } + } + if (!testStatus) { + fail("Could not read Attachment"); + } + } - // System.out.println("Creating attachment TXT"); - // file = new File(classLoader.getResource("sample.txt").getFile()); - // Map postData2 = new HashMap<>(); - // postData2.put("up__ID", entityID3); - // postData2.put("mimeType", "application/txt"); - // postData2.put("createdAt", new Date().toString()); - // postData2.put("createdBy", "test@test.com"); - // postData2.put("modifiedBy", "test@test.com"); - - // List createResponse2 = - // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData2, - // file); - // if (createResponse2.get(0).equals("Attachment created")) { - // attachmentID2 = createResponse2.get(1); - // System.out.println("Attachment created"); - // } + @Test + @Order(20) + void testDeleteMultipleAttachments() throws IOException { + System.out.println("Test (20) : Delete multiple attachments"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID); + if (response == "Entity in draft mode") { + String response1 = + api.deleteAttachment(appUrl, entityName, facetName, entityID, attachmentID2); + String response2 = + api.deleteAttachment(appUrl, entityName, facetName, entityID, attachmentID3); + if (response1 == "Deleted" && response2 == "Deleted") { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID); + if (response == "Saved") { + response1 = api.readAttachment(appUrl, entityName, facetName, entityID, attachmentID2); + response2 = api.readAttachment(appUrl, entityName, facetName, entityID, attachmentID3); + if (response1.equals("Could not read Attachment") + && response2.equals("Could not read Attachment")) { + testStatus = true; + } + } + } + } + if (!testStatus) { + fail("Could not delete attachment"); + } + } - // System.out.println("Creating attachment EXE"); - // file = new File(classLoader.getResource("sample.exe").getFile()); - // Map postData3 = new HashMap<>(); - // postData3.put("up__ID", entityID3); - // postData3.put("mimeType", "application/exe"); - // postData3.put("createdAt", new Date().toString()); - // postData3.put("createdBy", "test@test.com"); - // postData3.put("modifiedBy", "test@test.com"); - - // List createResponse3 = - // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData3, - // file); - // if (createResponse3.get(0).equals("Attachment created")) { - // attachmentID3 = createResponse3.get(1); - // System.out.println("Attachment created"); - // } + @Test + @Order(21) + void testUploadBlockedMimeType() throws IOException { + System.out.println("Test (21): Upload blocked mimeType .rtf"); + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + entityID2 = response; - // String check1 = createResponse1.get(0); - // String check2 = createResponse2.get(0); - // String check3 = createResponse3.get(0); - // if (check1.equals("Attachment created") - // && check2.equals("Attachment created") - // && check3.equals("Attachment created")) { - // Boolean attachment1Updated = false; - // Boolean attachment2Updated = false; - // Boolean attachment3Updated = false; - - // String name1 = "sample1234.pdf"; - // Integer secondaryPropertyInt1 = 1234; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // System.out.println("Renaming and updating secondary properties for attachment PDF"); - // String responsePDF1 = - // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - // // Update secondary properties for String - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponsePDF1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponsePDF2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - // // Update secondary properties for DateTime - // RequestBody bodyDateTime = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponsePDF3 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // // Update secondary properties for Boolean - // RequestBody bodyBoolean = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponsePDF4 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - // if (responsePDF1 == "Renamed" - // && updateSecondaryPropertyResponsePDF1 == "Updated" - // && updateSecondaryPropertyResponsePDF2 == "Updated" - // && updateSecondaryPropertyResponsePDF3 == "Updated" - // && updateSecondaryPropertyResponsePDF4 == "Updated") { - // System.out.println("Renamed & updated Secondary properties for attachment PDF"); - // attachment1Updated = true; - // } - - // System.out.println("Updating secondary properties for attachment TXT"); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponseTXT1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); - // if (updateSecondaryPropertyResponseTXT1 == "Updated") { - // System.out.println("Updated Secondary properties for attachment TXT"); - // attachment2Updated = true; - // } - // Integer secondaryPropertyInt3 = 1234; - // LocalDateTime secondaryPropertyDateTime3 = LocalDateTime.now(); - // System.out.println("Updating secondary properties for attachment EXE"); - // // Update secondary properties for String - // String dropdownValue1 = integrationTestUtils.getDropDownValue(); - // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - // RequestBody bodyDropdown1 = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - // String updateSecondaryPropertyResponseEXE1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown1); - // // Update secondary properties for Integer - // RequestBody bodyInt3 = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - // String updateSecondaryPropertyResponseEXE2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); - // // Update secondary properties for DateTime - // RequestBody bodyDateTime3 = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime3 + "\"\n}")); - // String updateSecondaryPropertyResponseEXE3 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID3, bodyDateTime3); - - // if (updateSecondaryPropertyResponseEXE1 == "Updated" - // && updateSecondaryPropertyResponseEXE2 == "Updated" - // && updateSecondaryPropertyResponseEXE3 == "Updated") { - // System.out.println("Updated Secondary properties for attachment EXE"); - // attachment3Updated = true; - // } - - // if (attachment1Updated && attachment2Updated && attachment3Updated) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // if (response.equals("Saved")) { - // System.out.println("Entity saved"); - // testStatus = true; - // System.out.println("Renamed & updated Secondary properties for attachments"); - // } - // } - // } - // } - // if (!testStatus) { - // fail("Could not update secondary property before entity is saved"); - // } - // } + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(Objects.requireNonNull(classLoader.getResource("sample.rtf")).getFile()); - // @Test - // @Order(28) - // void testUpdateValidSecondaryProperty_afterEntityIsSaved_multipleAttachments() { - // System.out.println( - // "Test (28): Rename & Update valid secondary properties for multiple attachments after - // entity is saved"); - // System.out.println("Editing entity"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - // if (response == "Entity in draft mode") { - // Boolean attachment1Updated = false; - // Boolean attachment2Updated = false; - // Boolean attachment3Updated = false; - - // String name1 = "sample1.pdf"; - // Integer secondaryPropertyInt1 = 12; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // System.out.println("Renaming and updating secondary properties for attachment PDF"); - // String responsePDF1 = - // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - // // Update secondary properties for String - // String dropdownValue1 = integrationTestUtils.getDropDownValue(); - // String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; - // RequestBody bodyDropdown1 = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); - // String updateSecondaryPropertyResponsePDF1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown1); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponsePDF2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - // // Update secondary properties for DateTime - // RequestBody bodyDateTime = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponsePDF3 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // // Update secondary properties for Boolean - // RequestBody bodyBoolean = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponsePDF4 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - - // if (responsePDF1 == "Renamed" - // && updateSecondaryPropertyResponsePDF1 == "Updated" - // && updateSecondaryPropertyResponsePDF2 == "Updated" - // && updateSecondaryPropertyResponsePDF3 == "Updated" - // && updateSecondaryPropertyResponsePDF4 == "Updated") { - // System.out.println("Renamed & updated Secondary properties for attachment PDF"); - // attachment1Updated = true; - // } + Map postData = new HashMap<>(); + postData.put("up__ID", entityID2); + postData.put("mimeType", "application/rtf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // System.out.println("Updating secondary properties for attachment TXT"); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponseTXT1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); - // if (updateSecondaryPropertyResponseTXT1 == "Updated") { - // System.out.println("Updated Secondary properties for attachment TXT"); - // attachment2Updated = true; - // } + List createResponse = + api.createAttachment(appUrl, entityName, facetName, entityID2, srvpath, postData, file); + String actualResponse = createResponse.get(0); + String expectedJson = + "{\"error\":{\"code\":\"500\",\"message\":\"This file type is not allowed in this repository. Contact your administrator for assistance.\"}}"; - // Integer secondaryPropertyInt3 = 123; - // LocalDateTime secondaryPropertyDateTime3 = LocalDateTime.now(); - // System.out.println("Updating secondary properties for attachment EXE"); - // // Update secondary properties for String - // String dropdownValue2 = integrationTestUtils.getDropDownValue(); - // String jsonDropdown2 = "{ \"customProperty1_code\" : \"" + dropdownValue2 + "\" }"; - // RequestBody bodyDropdown2 = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown2); - // String updateSecondaryPropertyResponseEXE1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown2); - // // Update secondary properties for Integer - // RequestBody bodyInt3 = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - // String updateSecondaryPropertyResponseEXE2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); - // // Update secondary properties for DateTime - // RequestBody bodyDateTime3 = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime3 + "\"\n}")); - // String updateSecondaryPropertyResponseEXE3 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID3, bodyDateTime3); - - // if (updateSecondaryPropertyResponseEXE1 == "Updated" - // && updateSecondaryPropertyResponseEXE2 == "Updated" - // && updateSecondaryPropertyResponseEXE3 == "Updated") { - // System.out.println("Updated Secondary properties for attachment EXE"); - // attachment3Updated = true; - // } + if (expectedJson.equals(actualResponse)) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + if ("Saved".equals(response)) { + testStatus = true; + } + } else { + api.saveEntityDraft(appUrl, entityName, srvpath, entityID2); + } + } + if (!testStatus) { + fail("Attachment got uploaded with blocked .rtf MIME type"); + } + } - // if (attachment1Updated && attachment2Updated && attachment3Updated) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // if (response.equals("Saved")) { - // System.out.println("Entity saved"); - // testStatus = true; - // System.out.println("Renamed & updated Secondary properties for attachments"); - // } - // } - // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - // if (deleteEntityResponse != "Entity Deleted") { - // fail("Could not delete entity"); - // } - // } - // if (!testStatus) { - // fail("Could not update secondary property after entity is saved"); - // } - // } + @Test + @Order(22) + void testDeleteEntity() { + System.out.println("Test (22) : Delete entity"); + Boolean testStatus = false; + String response = api.deleteEntity(appUrl, entityName, entityID); + String response2 = api.deleteEntity(appUrl, entityName, entityID2); + if (response == "Entity Deleted" && response2 == "Entity Deleted") { + testStatus = true; + } + if (!testStatus) { + fail("Could not delete entity"); + } + } - // @Test - // @Order(29) - // void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() - // throws IOException { - // System.out.println( - // "Test (29): Rename & Update invalid and valid secondary properties for multiple - // attachments before entity is saved"); - // System.out.println("Creating entity"); - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (response != "Could not create entity") { - // entityID3 = response; - - // System.out.println("Entity created"); - - // System.out.println("Creating attachment PDF"); - // ClassLoader classLoader = getClass().getClassLoader(); - - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Map postData1 = new HashMap<>(); - // postData1.put("up__ID", entityID3); - // postData1.put("mimeType", "application/pdf"); - // postData1.put("createdAt", new Date().toString()); - // postData1.put("createdBy", "test@test.com"); - // postData1.put("modifiedBy", "test@test.com"); - - // List createResponse1 = - // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData1, - // file); - // if (createResponse1.get(0).equals("Attachment created")) { - // attachmentID1 = createResponse1.get(1); - // System.out.println("Attachment created"); - // } + @Test + @Order(23) + void testUpdateValidSecondaryProperty_beforeEntityIsSaved_singleAttachment() throws IOException { + System.out.println("Test (23): Rename & Update secondary property before entity is saved"); + System.out.println("Creating entity"); + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response != "Could not create entity") { + entityID3 = response; + System.out.println("Entity created"); + System.out.println("Creating attachment"); + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", entityID3); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List createResponse = + api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData, file); + String check = createResponse.get(0); + if (check.equals("Attachment created")) { + attachmentID1 = createResponse.get(1); + System.out.println("Attachment created"); + String name1 = "sample1234.pdf"; + String secondaryPropertyString = "sample12345"; + Integer secondaryPropertyInt = 1234; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + System.out.println("Renaming and updating secondary properties for attachment"); + String response1 = + api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + // Update secondary properties for String + String dropdownValue1 = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + // Update secondary properties for DateTime + RequestBody bodyDateTime = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // Update secondary properties for Boolean + RequestBody bodyBoolean = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + if (response1 == "Renamed" + && updateSecondaryPropertyResponse1 == "Updated" + && updateSecondaryPropertyResponse2 == "Updated" + && updateSecondaryPropertyResponse3 == "Updated" + && updateSecondaryPropertyResponse4 == "Updated") { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + if (response.equals("Saved")) { + System.out.println("Entity saved"); + testStatus = true; + System.out.println("Renamed & updated Secondary properties for attachment"); + } + } + } + } + if (!testStatus) { + fail("Could not update secondary property before entity is saved"); + } + } + + @Test + @Order(24) + void testUpdateValidSecondaryProperty_afterEntityIsSaved_singleAttachment() { + System.out.println("Test (24): Rename & Update secondary property after entity is saved"); + System.out.println("Editing entity"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + if (response == "Entity in draft mode") { + String name1 = "sample.pdf"; + String secondaryPropertyString = "sample"; + Integer secondaryPropertyInt = 12; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + System.out.println("Renaming and updating secondary properties for attachment"); + String response1 = + api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + // Update secondary properties for String + String dropdownValue1 = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + // Update secondary properties for DateTime + RequestBody bodyDateTime = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // Update secondary properties for Boolean + RequestBody bodyBoolean = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + if (response1 == "Renamed" + && updateSecondaryPropertyResponse1 == "Updated" + && updateSecondaryPropertyResponse2 == "Updated" + && updateSecondaryPropertyResponse3 == "Updated" + && updateSecondaryPropertyResponse4 == "Updated") { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + if (response.equals("Saved")) { + System.out.println("Entity saved"); + testStatus = true; + System.out.println("Renamed & updated Secondary properties for attachment"); + } + } + String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + if (deleteEntityResponse != "Entity Deleted") { + fail("Could not delete entity"); + } + } + if (!testStatus) { + fail("Could not update secondary property after entity is saved"); + } + } + + @Test + @Order(25) + void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_singleAttachment() + throws IOException { + System.out.println( + "Test (25): Rename & Update invalid secondary property before entity is saved"); + System.out.println("Creating entity"); + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!"Could not create entity".equals(response)) { + entityID3 = response; + System.out.println("Entity created"); + System.out.println("Creating attachment"); + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + + Map postData = new HashMap<>(); + postData.put("up__ID", entityID3); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List createResponse = + api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData, file); + String check = createResponse.get(0); + if ("Attachment created".equals(check)) { + attachmentID1 = createResponse.get(1); + System.out.println("Attachment created"); + String name1 = "sample1234.pdf"; + + // Dropdown values for secondaryPropertyString + String[] dropdownValues = {"A", "B", "C"}; + // Select one dropdown value (e.g., "A") + String secondaryPropertyString = dropdownValues[0]; + + Integer secondaryPropertyInt = 1234; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + String invalidProperty = "testid"; + + System.out.println("Renaming and updating invalid secondary properties for attachment"); + String response1 = + api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + + // Update secondary properties for String using dropdown selected value as object with code + + String dropdownValue1 = integrationTestUtils.getDropDownValue(); + String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + RequestBody bodyDropdown1 = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown1); + + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + + // Update secondary properties for DateTime + RequestBody bodyDateTime = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + + // Update secondary properties for Boolean + RequestBody bodyBoolean = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + + // Update invalid secondary property + String updateSecondaryPropertyResponse5 = + api.updateInvalidSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, invalidProperty); + + if ("Renamed".equals(response1) + && "Updated".equals(updateSecondaryPropertyResponse1) + && "Updated".equals(updateSecondaryPropertyResponse2) + && "Updated".equals(updateSecondaryPropertyResponse3) + && "Updated".equals(updateSecondaryPropertyResponse4) + && "Updated".equals(updateSecondaryPropertyResponse5)) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + Map attachmentMetadata = + api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); + assertEquals("sample.pdf", attachmentMetadata.get("fileName")); + assertNull(attachmentMetadata.get("customProperty3")); + assertNull(attachmentMetadata.get("customProperty4")); + assertNull(attachmentMetadata.get("customProperty1_code")); + assertNull(attachmentMetadata.get("customProperty2")); + assertNull(attachmentMetadata.get("customProperty6")); + assertNull(attachmentMetadata.get("customProperty5")); + + String expectedResponse = + "[{\"code\":\"\",\"message\":\"The following secondary properties are not supported.\\n" + + // + "\\n" + + // + "\\t\\u2022 id1\\n" + + // + "\\n" + + // + "Please contact your administrator for assistance with any necessary adjustments.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + if (response.equals(expectedResponse)) { + System.out.println("Entity saved"); + testStatus = true; + System.out.println( + "Rename & update secondary properties for attachment is unsuccessfull"); + } + } + } + } + if (!testStatus) { + fail("Could not update secondary property before entity is saved"); + } + } + + @Test + @Order(26) + void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_singleAttachment() throws IOException { + System.out.println( + "Test (26): Rename & Update invalid secondary property after entity is saved"); + System.out.println("Editing entity"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + if (response == "Entity in draft mode") { + String name1 = "sample.pdf"; + String secondaryPropertyString = "A"; + Integer secondaryPropertyInt = 12; + LocalDateTime secondaryPropertyDateTime = LocalDateTime.now(); + String invalidProperty = "testidinvalid"; + System.out.println("Renaming and updating invalid secondary properties for attachment"); + String response1 = + api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + // Update secondary properties for DateTime + RequestBody bodyDateTime = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime + "\"\n}")); + String updateSecondaryPropertyResponse3 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // Update secondary properties for Boolean + RequestBody bodyBoolean = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse4 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + // Update invalid secondary property + String updateSecondaryPropertyResponse5 = + api.updateInvalidSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, invalidProperty); + if (response1 == "Renamed" + && updateSecondaryPropertyResponse1 == "Updated" + && updateSecondaryPropertyResponse2 == "Updated" + && updateSecondaryPropertyResponse3 == "Updated" + && updateSecondaryPropertyResponse4 == "Updated" + && updateSecondaryPropertyResponse5 == "Updated") { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + Map attachmentMetadata = + api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); + assertEquals("sample.pdf", attachmentMetadata.get("fileName")); + assertNull(attachmentMetadata.get("customProperty3")); + assertNull(attachmentMetadata.get("customProperty4")); + assertNull(attachmentMetadata.get("customProperty1_code")); + assertNull(attachmentMetadata.get("customProperty2")); + assertNull(attachmentMetadata.get("customProperty6")); + assertNull(attachmentMetadata.get("customProperty5")); + + String expectedResponse = + "[{\"code\":\"\",\"message\":\"The following secondary properties are not supported.\\n" + + // + "\\n" + + // + "\\t\\u2022 id1\\n" + + // + "\\n" + + // + "Please contact your administrator for assistance with any necessary adjustments.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + if (response.equals(expectedResponse)) { + System.out.println("Entity saved"); + testStatus = true; + System.out.println( + "Rename & update secondary properties for attachment is unsuccessfull"); + } + } + String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + if (deleteEntityResponse != "Entity Deleted") { + fail("Could not delete entity"); + } + } + if (!testStatus) { + fail("Could not update secondary property before entity is saved"); + } + } + + @Test + @Order(27) + void testUpdateValidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() + throws IOException { + System.out.println( + "Test (27): Rename & Update valid secondary properties for multiple attachments before entity is saved"); + System.out.println("Creating entity"); + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response != "Could not create entity") { + entityID3 = response; + + System.out.println("Entity created"); + + System.out.println("Creating attachment PDF"); + ClassLoader classLoader = getClass().getClassLoader(); + + File file = new File(classLoader.getResource("sample.pdf").getFile()); + Map postData1 = new HashMap<>(); + postData1.put("up__ID", entityID3); + postData1.put("mimeType", "application/pdf"); + postData1.put("createdAt", new Date().toString()); + postData1.put("createdBy", "test@test.com"); + postData1.put("modifiedBy", "test@test.com"); + + List createResponse1 = + api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData1, file); + if (createResponse1.get(0).equals("Attachment created")) { + attachmentID1 = createResponse1.get(1); + System.out.println("Attachment created"); + } + + System.out.println("Creating attachment TXT"); + file = new File(classLoader.getResource("sample.txt").getFile()); + Map postData2 = new HashMap<>(); + postData2.put("up__ID", entityID3); + postData2.put("mimeType", "application/txt"); + postData2.put("createdAt", new Date().toString()); + postData2.put("createdBy", "test@test.com"); + postData2.put("modifiedBy", "test@test.com"); + + List createResponse2 = + api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData2, file); + if (createResponse2.get(0).equals("Attachment created")) { + attachmentID2 = createResponse2.get(1); + System.out.println("Attachment created"); + } + + System.out.println("Creating attachment EXE"); + file = new File(classLoader.getResource("sample.exe").getFile()); + Map postData3 = new HashMap<>(); + postData3.put("up__ID", entityID3); + postData3.put("mimeType", "application/exe"); + postData3.put("createdAt", new Date().toString()); + postData3.put("createdBy", "test@test.com"); + postData3.put("modifiedBy", "test@test.com"); + + List createResponse3 = + api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData3, file); + if (createResponse3.get(0).equals("Attachment created")) { + attachmentID3 = createResponse3.get(1); + System.out.println("Attachment created"); + } + + String check1 = createResponse1.get(0); + String check2 = createResponse2.get(0); + String check3 = createResponse3.get(0); + if (check1.equals("Attachment created") + && check2.equals("Attachment created") + && check3.equals("Attachment created")) { + Boolean attachment1Updated = false; + Boolean attachment2Updated = false; + Boolean attachment3Updated = false; + + String name1 = "sample1234.pdf"; + Integer secondaryPropertyInt1 = 1234; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + System.out.println("Renaming and updating secondary properties for attachment PDF"); + String responsePDF1 = + api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + // Update secondary properties for String + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponsePDF1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponsePDF2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + // Update secondary properties for DateTime + RequestBody bodyDateTime = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponsePDF3 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // Update secondary properties for Boolean + RequestBody bodyBoolean = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponsePDF4 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + if (responsePDF1 == "Renamed" + && updateSecondaryPropertyResponsePDF1 == "Updated" + && updateSecondaryPropertyResponsePDF2 == "Updated" + && updateSecondaryPropertyResponsePDF3 == "Updated" + && updateSecondaryPropertyResponsePDF4 == "Updated") { + System.out.println("Renamed & updated Secondary properties for attachment PDF"); + attachment1Updated = true; + } + + System.out.println("Updating secondary properties for attachment TXT"); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponseTXT1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); + if (updateSecondaryPropertyResponseTXT1 == "Updated") { + System.out.println("Updated Secondary properties for attachment TXT"); + attachment2Updated = true; + } + Integer secondaryPropertyInt3 = 1234; + LocalDateTime secondaryPropertyDateTime3 = LocalDateTime.now(); + System.out.println("Updating secondary properties for attachment EXE"); + // Update secondary properties for String + String dropdownValue1 = integrationTestUtils.getDropDownValue(); + String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + RequestBody bodyDropdown1 = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + String updateSecondaryPropertyResponseEXE1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown1); + // Update secondary properties for Integer + RequestBody bodyInt3 = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + String updateSecondaryPropertyResponseEXE2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); + // Update secondary properties for DateTime + RequestBody bodyDateTime3 = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime3 + "\"\n}")); + String updateSecondaryPropertyResponseEXE3 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID3, bodyDateTime3); + + if (updateSecondaryPropertyResponseEXE1 == "Updated" + && updateSecondaryPropertyResponseEXE2 == "Updated" + && updateSecondaryPropertyResponseEXE3 == "Updated") { + System.out.println("Updated Secondary properties for attachment EXE"); + attachment3Updated = true; + } + + if (attachment1Updated && attachment2Updated && attachment3Updated) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + if (response.equals("Saved")) { + System.out.println("Entity saved"); + testStatus = true; + System.out.println("Renamed & updated Secondary properties for attachments"); + } + } + } + } + if (!testStatus) { + fail("Could not update secondary property before entity is saved"); + } + } + + @Test + @Order(28) + void testUpdateValidSecondaryProperty_afterEntityIsSaved_multipleAttachments() { + System.out.println( + "Test (28): Rename & Update valid secondary properties for multiple attachments after entity is saved"); + System.out.println("Editing entity"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + if (response == "Entity in draft mode") { + Boolean attachment1Updated = false; + Boolean attachment2Updated = false; + Boolean attachment3Updated = false; + + String name1 = "sample1.pdf"; + Integer secondaryPropertyInt1 = 12; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + System.out.println("Renaming and updating secondary properties for attachment PDF"); + String responsePDF1 = + api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + // Update secondary properties for String + String dropdownValue1 = integrationTestUtils.getDropDownValue(); + String jsonDropdown1 = "{ \"customProperty1_code\" : \"" + dropdownValue1 + "\" }"; + RequestBody bodyDropdown1 = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown1); + String updateSecondaryPropertyResponsePDF1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown1); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponsePDF2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + // Update secondary properties for DateTime + RequestBody bodyDateTime = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponsePDF3 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // Update secondary properties for Boolean + RequestBody bodyBoolean = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponsePDF4 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + + if (responsePDF1 == "Renamed" + && updateSecondaryPropertyResponsePDF1 == "Updated" + && updateSecondaryPropertyResponsePDF2 == "Updated" + && updateSecondaryPropertyResponsePDF3 == "Updated" + && updateSecondaryPropertyResponsePDF4 == "Updated") { + System.out.println("Renamed & updated Secondary properties for attachment PDF"); + attachment1Updated = true; + } + + System.out.println("Updating secondary properties for attachment TXT"); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponseTXT1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); + if (updateSecondaryPropertyResponseTXT1 == "Updated") { + System.out.println("Updated Secondary properties for attachment TXT"); + attachment2Updated = true; + } + + Integer secondaryPropertyInt3 = 123; + LocalDateTime secondaryPropertyDateTime3 = LocalDateTime.now(); + System.out.println("Updating secondary properties for attachment EXE"); + // Update secondary properties for String + String dropdownValue2 = integrationTestUtils.getDropDownValue(); + String jsonDropdown2 = "{ \"customProperty1_code\" : \"" + dropdownValue2 + "\" }"; + RequestBody bodyDropdown2 = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown2); + String updateSecondaryPropertyResponseEXE1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown2); + // Update secondary properties for Integer + RequestBody bodyInt3 = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + String updateSecondaryPropertyResponseEXE2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); + // Update secondary properties for DateTime + RequestBody bodyDateTime3 = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime3 + "\"\n}")); + String updateSecondaryPropertyResponseEXE3 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID3, bodyDateTime3); + + if (updateSecondaryPropertyResponseEXE1 == "Updated" + && updateSecondaryPropertyResponseEXE2 == "Updated" + && updateSecondaryPropertyResponseEXE3 == "Updated") { + System.out.println("Updated Secondary properties for attachment EXE"); + attachment3Updated = true; + } + + if (attachment1Updated && attachment2Updated && attachment3Updated) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + if (response.equals("Saved")) { + System.out.println("Entity saved"); + testStatus = true; + System.out.println("Renamed & updated Secondary properties for attachments"); + } + } + String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + if (deleteEntityResponse != "Entity Deleted") { + fail("Could not delete entity"); + } + } + if (!testStatus) { + fail("Could not update secondary property after entity is saved"); + } + } + + @Test + @Order(29) + void testUpdateInvalidSecondaryProperty_beforeEntityIsSaved_multipleAttachments() + throws IOException { + System.out.println( + "Test (29): Rename & Update invalid and valid secondary properties for multiple attachments before entity is saved"); + System.out.println("Creating entity"); + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response != "Could not create entity") { + entityID3 = response; + + System.out.println("Entity created"); + + System.out.println("Creating attachment PDF"); + ClassLoader classLoader = getClass().getClassLoader(); + + File file = new File(classLoader.getResource("sample.pdf").getFile()); + Map postData1 = new HashMap<>(); + postData1.put("up__ID", entityID3); + postData1.put("mimeType", "application/pdf"); + postData1.put("createdAt", new Date().toString()); + postData1.put("createdBy", "test@test.com"); + postData1.put("modifiedBy", "test@test.com"); + + List createResponse1 = + api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData1, file); + if (createResponse1.get(0).equals("Attachment created")) { + attachmentID1 = createResponse1.get(1); + System.out.println("Attachment created"); + } + + System.out.println("Creating attachment TXT"); + file = new File(classLoader.getResource("sample.txt").getFile()); + Map postData2 = new HashMap<>(); + postData2.put("up__ID", entityID3); + postData2.put("mimeType", "application/txt"); + postData2.put("createdAt", new Date().toString()); + postData2.put("createdBy", "test@test.com"); + postData2.put("modifiedBy", "test@test.com"); + + List createResponse2 = + api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData2, file); + if (createResponse2.get(0).equals("Attachment created")) { + attachmentID2 = createResponse2.get(1); + System.out.println("Attachment created"); + } + + System.out.println("Creating attachment EXE"); + file = new File(classLoader.getResource("sample.exe").getFile()); + Map postData3 = new HashMap<>(); + postData3.put("up__ID", entityID3); + postData3.put("mimeType", "application/exe"); + postData3.put("createdAt", new Date().toString()); + postData3.put("createdBy", "test@test.com"); + postData3.put("modifiedBy", "test@test.com"); + + List createResponse3 = + api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData3, file); + if (createResponse3.get(0).equals("Attachment created")) { + attachmentID3 = createResponse3.get(1); + System.out.println("Attachment created"); + } + + String check1 = createResponse1.get(0); + String check2 = createResponse2.get(0); + String check3 = createResponse3.get(0); + if (check1.equals("Attachment created") + && check2.equals("Attachment created") + && check3.equals("Attachment created")) { + Boolean attachment1Updated = false; + Boolean attachment2Updated = false; + Boolean attachment3Updated = false; + + String name1 = "sample1234.pdf"; + Integer secondaryPropertyInt1 = 1234; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + String invalidPropertyPDF = "testidinvalidPDF"; + System.out.println("Renaming and updating invalid secondary properties for attachment PDF"); + String responsePDF1 = + api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + // Update secondary properties for String + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponsePDF1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyint = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponsePDF2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyint); + // Update secondary properties for DateTime + RequestBody bodyDateTime = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponsePDF3 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // Update secondary properties for Boolean + RequestBody bodyBoolean = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponsePDF4 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + // Update invalid secondary property + String updateSecondaryPropertyResponsePDF5 = + api.updateInvalidSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, invalidPropertyPDF); + if (responsePDF1 == "Renamed" + && updateSecondaryPropertyResponsePDF1 == "Updated" + && updateSecondaryPropertyResponsePDF2 == "Updated" + && updateSecondaryPropertyResponsePDF3 == "Updated" + && updateSecondaryPropertyResponsePDF4 == "Updated" + && updateSecondaryPropertyResponsePDF5 == "Updated") { + attachment1Updated = true; + } + + System.out.println("Updating valid secondary properties for attachment TXT"); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponseTXT1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); + if (updateSecondaryPropertyResponseTXT1 == "Updated") { + System.out.println("Updated Secondary properties for attachment TXT"); + attachment2Updated = true; + } + + Integer secondaryPropertyInt3 = 1234; + System.out.println("Updating valid secondary properties for attachment EXE"); + + // Update secondary properties for String + RequestBody bodyDropdown1 = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponseEXE1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown1); + // Update secondary properties for Integer + RequestBody bodyInt3 = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + String updateSecondaryPropertyResponseEXE2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); + + if (updateSecondaryPropertyResponseEXE1 == "Updated" + && updateSecondaryPropertyResponseEXE2 == "Updated") { + System.out.println("Updated Secondary properties for attachment EXE"); + attachment3Updated = true; + } + + if (attachment1Updated && attachment2Updated && attachment3Updated) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + Map attachmentMetadataPDF = + api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); + assertEquals("sample.pdf", attachmentMetadataPDF.get("fileName")); + assertNull(attachmentMetadataPDF.get("customProperty3")); + assertNull(attachmentMetadataPDF.get("customProperty4")); + assertNull(attachmentMetadataPDF.get("customProperty1_code")); + assertNull(attachmentMetadataPDF.get("customProperty2")); + assertNull(attachmentMetadataPDF.get("customProperty6")); + assertNull(attachmentMetadataPDF.get("customProperty5")); + + Map attachmentMetadataTXT = + api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID2); + assertEquals("sample.txt", attachmentMetadataTXT.get("fileName")); + assertNull(attachmentMetadataTXT.get("customProperty3")); + assertNull(attachmentMetadataTXT.get("customProperty4")); + assertNull(attachmentMetadataTXT.get("customProperty1_code")); + assertNull(attachmentMetadataTXT.get("customProperty2")); + assertTrue((Boolean) attachmentMetadataTXT.get("customProperty6")); + assertNull(attachmentMetadataTXT.get("customProperty5")); + + Map attachmentMetadataEXE = + api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID3); + assertEquals("sample.exe", attachmentMetadataEXE.get("fileName")); + assertNull(attachmentMetadataEXE.get("customProperty3")); + assertNull(attachmentMetadataEXE.get("customProperty4")); + assertEquals(dropdownValue, attachmentMetadataEXE.get("customProperty1_code")); + assertEquals(1234, attachmentMetadataEXE.get("customProperty2")); + + String expectedResponse = + "[{\"code\":\"\",\"message\":\"The following secondary properties are not supported.\\n" + + // + "\\n" + + // + "\\t\\u2022 id1\\n" + + // + "\\n" + + // + "Please contact your administrator for assistance with any necessary adjustments.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; + if (response.equals(expectedResponse)) { + System.out.println("Entity saved"); + testStatus = true; + System.out.println( + "Rename & update unsuccessfull for invalid Secondary properties and successfull for valid property attachments"); + } + } + } + } + if (!testStatus) { + fail("Could not update secondary property before entity is saved"); + } + } + + @Test + @Order(30) + void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_multipleAttachments() + throws IOException { + System.out.println( + "Test (30): Rename & Update invalid and valid secondary properties for multiple attachments after entity is saved"); + System.out.println("Editing entity"); + Boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); + if (response == "Entity in draft mode") { + Boolean attachment1Updated = false; + Boolean attachment2Updated = false; + Boolean attachment3Updated = false; + + String name1 = "sample.pdf"; + Integer secondaryPropertyInt1 = 12; + LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); + String invalidPropertyPDF = "testidinvalidPDF"; + System.out.println("Renaming and updating invalid secondary properties for attachment PDF"); + String responsePDF1 = + api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); + // Update secondary properties for String + String dropdownValue = integrationTestUtils.getDropDownValue(); + String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; + RequestBody bodyDropdown = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponsePDF1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); + // Update secondary properties for Integer + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); + String updateSecondaryPropertyResponsePDF2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); + // Update secondary properties for DateTime + RequestBody bodyDateTime = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); + String updateSecondaryPropertyResponsePDF3 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); + // Update secondary properties for Boolean + RequestBody bodyBoolean = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponsePDF4 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); + // Update invalid secondary property + String updateSecondaryPropertyResponsePDF5 = + api.updateInvalidSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID1, invalidPropertyPDF); + if (responsePDF1 == "Renamed" + && updateSecondaryPropertyResponsePDF1 == "Updated" + && updateSecondaryPropertyResponsePDF2 == "Updated" + && updateSecondaryPropertyResponsePDF3 == "Updated" + && updateSecondaryPropertyResponsePDF4 == "Updated" + && updateSecondaryPropertyResponsePDF5 == "Updated") { + attachment1Updated = true; + } + + System.out.println("Updating valid secondary properties for attachment TXT"); + // Update secondary properties for Boolean + RequestBody bodyBool = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + false + "\n}")); + String updateSecondaryPropertyResponseTXT1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); + if (updateSecondaryPropertyResponseTXT1 == "Updated") { + System.out.println("Updated Secondary properties for attachment TXT"); + attachment2Updated = true; + } + + Integer secondaryPropertyInt3 = 12; + System.out.println("Updating valid secondary properties for attachment EXE"); + + // Update secondary properties for String + RequestBody bodyDropdown1 = + RequestBody.create(MediaType.parse("application/json"), jsonDropdown); + String updateSecondaryPropertyResponseEXE1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown1); + // Update secondary properties for Integer + RequestBody bodyInt3 = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8( + "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); + String updateSecondaryPropertyResponseEXE2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); + + if (updateSecondaryPropertyResponseEXE1 == "Updated" + && updateSecondaryPropertyResponseEXE2 == "Updated") { + System.out.println("Updated Secondary properties for attachment EXE"); + attachment3Updated = true; + } - // System.out.println("Creating attachment TXT"); - // file = new File(classLoader.getResource("sample.txt").getFile()); - // Map postData2 = new HashMap<>(); - // postData2.put("up__ID", entityID3); - // postData2.put("mimeType", "application/txt"); - // postData2.put("createdAt", new Date().toString()); - // postData2.put("createdBy", "test@test.com"); - // postData2.put("modifiedBy", "test@test.com"); - - // List createResponse2 = - // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData2, - // file); - // if (createResponse2.get(0).equals("Attachment created")) { - // attachmentID2 = createResponse2.get(1); - // System.out.println("Attachment created"); - // } + if (attachment1Updated && attachment2Updated && attachment3Updated) { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); + Map attachmentMetadataPDF = + api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); + assertEquals("sample.pdf", attachmentMetadataPDF.get("fileName")); + assertNull(attachmentMetadataPDF.get("customProperty3")); + assertNull(attachmentMetadataPDF.get("customProperty4")); + assertNull(attachmentMetadataPDF.get("customProperty1_code")); + assertNull(attachmentMetadataPDF.get("customProperty2")); + assertNull(attachmentMetadataPDF.get("customProperty6")); + assertNull(attachmentMetadataPDF.get("customProperty5")); + + Map attachmentMetadataTXT = + api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID2); + assertEquals("sample.txt", attachmentMetadataTXT.get("fileName")); + assertNull(attachmentMetadataTXT.get("customProperty3")); + assertNull(attachmentMetadataTXT.get("customProperty4")); + assertNull(attachmentMetadataTXT.get("customProperty1_code")); + assertNull(attachmentMetadataTXT.get("customProperty2")); + assertFalse((Boolean) attachmentMetadataTXT.get("customProperty6")); + assertNull(attachmentMetadataTXT.get("customProperty5")); + + Map attachmentMetadataEXE = + api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID3); + assertEquals("sample.exe", attachmentMetadataEXE.get("fileName")); + assertNull(attachmentMetadataEXE.get("customProperty3")); + assertNull(attachmentMetadataEXE.get("customProperty4")); + assertEquals(dropdownValue, attachmentMetadataEXE.get("customProperty1_code")); + assertEquals(12, attachmentMetadataEXE.get("customProperty2")); + + String expectedResponse = + "[{\"code\":\"\",\"message\":\"The following secondary properties are not supported.\\n" + + // + "\\n" + + // + "\\t\\u2022 id1\\n" + + // + "\\n" + + // + "Please contact your administrator for assistance with any necessary adjustments.\\n" + + // + "\\n" + + // + "Table: attachments\\n" + + // + "Page: IntegrationTestEntity\",\"numericSeverity\":3}]"; + if (response.equals(expectedResponse)) { + System.out.println("Entity saved"); + testStatus = true; + System.out.println( + "Rename & update unsuccessfull for invalid Secondary properties and successfull for valid property attachments"); + } + String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); + if (deleteEntityResponse != "Entity Deleted") { + fail("Could not delete entity"); + } + } + } + if (!testStatus) { + fail("Could not update secondary property before entity is saved"); + } + } - // System.out.println("Creating attachment EXE"); - // file = new File(classLoader.getResource("sample.exe").getFile()); - // Map postData3 = new HashMap<>(); - // postData3.put("up__ID", entityID3); - // postData3.put("mimeType", "application/exe"); - // postData3.put("createdAt", new Date().toString()); - // postData3.put("createdBy", "test@test.com"); - // postData3.put("modifiedBy", "test@test.com"); - - // List createResponse3 = - // api.createAttachment(appUrl, entityName, facetName, entityID3, srvpath, postData3, - // file); - // if (createResponse3.get(0).equals("Attachment created")) { - // attachmentID3 = createResponse3.get(1); - // System.out.println("Attachment created"); - // } + @Test + @Order(31) + void testNAttachments_NewEntity() throws IOException { + System.out.println( + "Test (31): Creating new entity and checking only max 4 attachments are allowed to be uploaded"); + System.out.println("Creating entity"); + Boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response != "Could not create entity") { + entityID4 = response; - // String check1 = createResponse1.get(0); - // String check2 = createResponse2.get(0); - // String check3 = createResponse3.get(0); - // if (check1.equals("Attachment created") - // && check2.equals("Attachment created") - // && check3.equals("Attachment created")) { - // Boolean attachment1Updated = false; - // Boolean attachment2Updated = false; - // Boolean attachment3Updated = false; - - // String name1 = "sample1234.pdf"; - // Integer secondaryPropertyInt1 = 1234; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // String invalidPropertyPDF = "testidinvalidPDF"; - // System.out.println("Renaming and updating invalid secondary properties for attachment - // PDF"); - // String responsePDF1 = - // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - // // Update secondary properties for String - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponsePDF1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyint = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponsePDF2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyint); - // // Update secondary properties for DateTime - // RequestBody bodyDateTime = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponsePDF3 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // // Update secondary properties for Boolean - // RequestBody bodyBoolean = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponsePDF4 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - // // Update invalid secondary property - // String updateSecondaryPropertyResponsePDF5 = - // api.updateInvalidSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, invalidPropertyPDF); - // if (responsePDF1 == "Renamed" - // && updateSecondaryPropertyResponsePDF1 == "Updated" - // && updateSecondaryPropertyResponsePDF2 == "Updated" - // && updateSecondaryPropertyResponsePDF3 == "Updated" - // && updateSecondaryPropertyResponsePDF4 == "Updated" - // && updateSecondaryPropertyResponsePDF5 == "Updated") { - // attachment1Updated = true; - // } - - // System.out.println("Updating valid secondary properties for attachment TXT"); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponseTXT1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); - // if (updateSecondaryPropertyResponseTXT1 == "Updated") { - // System.out.println("Updated Secondary properties for attachment TXT"); - // attachment2Updated = true; - // } - - // Integer secondaryPropertyInt3 = 1234; - // System.out.println("Updating valid secondary properties for attachment EXE"); - - // // Update secondary properties for String - // RequestBody bodyDropdown1 = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponseEXE1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown1); - // // Update secondary properties for Integer - // RequestBody bodyInt3 = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - // String updateSecondaryPropertyResponseEXE2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); - - // if (updateSecondaryPropertyResponseEXE1 == "Updated" - // && updateSecondaryPropertyResponseEXE2 == "Updated") { - // System.out.println("Updated Secondary properties for attachment EXE"); - // attachment3Updated = true; - // } - - // if (attachment1Updated && attachment2Updated && attachment3Updated) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // Map attachmentMetadataPDF = - // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); - // assertEquals("sample.pdf", attachmentMetadataPDF.get("fileName")); - // assertNull(attachmentMetadataPDF.get("customProperty3")); - // assertNull(attachmentMetadataPDF.get("customProperty4")); - // assertNull(attachmentMetadataPDF.get("customProperty1_code")); - // assertNull(attachmentMetadataPDF.get("customProperty2")); - // assertNull(attachmentMetadataPDF.get("customProperty6")); - // assertNull(attachmentMetadataPDF.get("customProperty5")); - - // Map attachmentMetadataTXT = - // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID2); - // assertEquals("sample.txt", attachmentMetadataTXT.get("fileName")); - // assertNull(attachmentMetadataTXT.get("customProperty3")); - // assertNull(attachmentMetadataTXT.get("customProperty4")); - // assertNull(attachmentMetadataTXT.get("customProperty1_code")); - // assertNull(attachmentMetadataTXT.get("customProperty2")); - // assertTrue((Boolean) attachmentMetadataTXT.get("customProperty6")); - // assertNull(attachmentMetadataTXT.get("customProperty5")); - - // Map attachmentMetadataEXE = - // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID3); - // assertEquals("sample.exe", attachmentMetadataEXE.get("fileName")); - // assertNull(attachmentMetadataEXE.get("customProperty3")); - // assertNull(attachmentMetadataEXE.get("customProperty4")); - // assertEquals(dropdownValue, attachmentMetadataEXE.get("customProperty1_code")); - // assertEquals(1234, attachmentMetadataEXE.get("customProperty2")); - - // String expectedResponse = - // "[{\"code\":\"\",\"message\":\"The following secondary properties are not - // supported.\\n" - // + // - // "\\n" - // + // - // "\\t\\u2022 id1\\n" - // + // - // "\\n" - // + // - // "Please contact your administrator for assistance with any necessary - // adjustments.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\",\"numericSeverity\":3}]"; - // if (response.equals(expectedResponse)) { - // System.out.println("Entity saved"); - // testStatus = true; - // System.out.println( - // "Rename & update unsuccessfull for invalid Secondary properties and successfull - // for valid property attachments"); - // } - // } - // } - // } - // if (!testStatus) { - // fail("Could not update secondary property before entity is saved"); - // } - // } + System.out.println("Entity created"); - // @Test - // @Order(30) - // void testUpdateInvalidSecondaryProperty_afterEntityIsSaved_multipleAttachments() - // throws IOException { - // System.out.println( - // "Test (30): Rename & Update invalid and valid secondary properties for multiple - // attachments after entity is saved"); - // System.out.println("Editing entity"); - // Boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID3); - // if (response == "Entity in draft mode") { - // Boolean attachment1Updated = false; - // Boolean attachment2Updated = false; - // Boolean attachment3Updated = false; - - // String name1 = "sample.pdf"; - // Integer secondaryPropertyInt1 = 12; - // LocalDateTime secondaryPropertyDateTime1 = LocalDateTime.now(); - // String invalidPropertyPDF = "testidinvalidPDF"; - // System.out.println("Renaming and updating invalid secondary properties for attachment - // PDF"); - // String responsePDF1 = - // api.renameAttachment(appUrl, entityName, facetName, entityID3, attachmentID1, name1); - // // Update secondary properties for String - // String dropdownValue = integrationTestUtils.getDropDownValue(); - // String jsonDropdown = "{ \"customProperty1_code\" : \"" + dropdownValue + "\" }"; - // RequestBody bodyDropdown = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponsePDF1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDropdown); - // // Update secondary properties for Integer - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt1 + "\n}")); - // String updateSecondaryPropertyResponsePDF2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyInt); - // // Update secondary properties for DateTime - // RequestBody bodyDateTime = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty5\" : \"" + secondaryPropertyDateTime1 + "\"\n}")); - // String updateSecondaryPropertyResponsePDF3 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyDateTime); - // // Update secondary properties for Boolean - // RequestBody bodyBoolean = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponsePDF4 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, bodyBoolean); - // // Update invalid secondary property - // String updateSecondaryPropertyResponsePDF5 = - // api.updateInvalidSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID1, invalidPropertyPDF); - // if (responsePDF1 == "Renamed" - // && updateSecondaryPropertyResponsePDF1 == "Updated" - // && updateSecondaryPropertyResponsePDF2 == "Updated" - // && updateSecondaryPropertyResponsePDF3 == "Updated" - // && updateSecondaryPropertyResponsePDF4 == "Updated" - // && updateSecondaryPropertyResponsePDF5 == "Updated") { - // attachment1Updated = true; - // } + System.out.println("Creating attachment PDF"); + ClassLoader classLoader = getClass().getClassLoader(); - // System.out.println("Updating valid secondary properties for attachment TXT"); - // // Update secondary properties for Boolean - // RequestBody bodyBool = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + false + "\n}")); - // String updateSecondaryPropertyResponseTXT1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID2, bodyBool); - // if (updateSecondaryPropertyResponseTXT1 == "Updated") { - // System.out.println("Updated Secondary properties for attachment TXT"); - // attachment2Updated = true; - // } + File file = new File(classLoader.getResource("sample.pdf").getFile()); + Map postData1 = new HashMap<>(); + postData1.put("up__ID", entityID4); + postData1.put("mimeType", "application/pdf"); + postData1.put("createdAt", new Date().toString()); + postData1.put("createdBy", "test@test.com"); + postData1.put("modifiedBy", "test@test.com"); + + List createResponse1 = + api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData1, file); + if (createResponse1.get(0).equals("Attachment created")) { + attachmentID1 = createResponse1.get(1); + System.out.println("Attachment created"); + } - // Integer secondaryPropertyInt3 = 12; - // System.out.println("Updating valid secondary properties for attachment EXE"); - - // // Update secondary properties for String - // RequestBody bodyDropdown1 = - // RequestBody.create(MediaType.parse("application/json"), jsonDropdown); - // String updateSecondaryPropertyResponseEXE1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID3, bodyDropdown1); - // // Update secondary properties for Integer - // RequestBody bodyInt3 = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8( - // "{\n \"customProperty2\" : " + secondaryPropertyInt3 + "\n}")); - // String updateSecondaryPropertyResponseEXE2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, entityID3, attachmentID3, bodyInt3); - - // if (updateSecondaryPropertyResponseEXE1 == "Updated" - // && updateSecondaryPropertyResponseEXE2 == "Updated") { - // System.out.println("Updated Secondary properties for attachment EXE"); - // attachment3Updated = true; - // } + System.out.println("Creating attachment TXT"); + file = new File(classLoader.getResource("sample.txt").getFile()); + Map postData2 = new HashMap<>(); + postData2.put("up__ID", entityID4); + postData2.put("mimeType", "application/txt"); + postData2.put("createdAt", new Date().toString()); + postData2.put("createdBy", "test@test.com"); + postData2.put("modifiedBy", "test@test.com"); + + List createResponse2 = + api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData2, file); + if (createResponse2.get(0).equals("Attachment created")) { + attachmentID2 = createResponse2.get(1); + System.out.println("Attachment created"); + } - // if (attachment1Updated && attachment2Updated && attachment3Updated) { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID3); - // Map attachmentMetadataPDF = - // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID1); - // assertEquals("sample.pdf", attachmentMetadataPDF.get("fileName")); - // assertNull(attachmentMetadataPDF.get("customProperty3")); - // assertNull(attachmentMetadataPDF.get("customProperty4")); - // assertNull(attachmentMetadataPDF.get("customProperty1_code")); - // assertNull(attachmentMetadataPDF.get("customProperty2")); - // assertNull(attachmentMetadataPDF.get("customProperty6")); - // assertNull(attachmentMetadataPDF.get("customProperty5")); - - // Map attachmentMetadataTXT = - // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID2); - // assertEquals("sample.txt", attachmentMetadataTXT.get("fileName")); - // assertNull(attachmentMetadataTXT.get("customProperty3")); - // assertNull(attachmentMetadataTXT.get("customProperty4")); - // assertNull(attachmentMetadataTXT.get("customProperty1_code")); - // assertNull(attachmentMetadataTXT.get("customProperty2")); - // assertFalse((Boolean) attachmentMetadataTXT.get("customProperty6")); - // assertNull(attachmentMetadataTXT.get("customProperty5")); - - // Map attachmentMetadataEXE = - // api.fetchMetadata(appUrl, entityName, facetName, entityID3, attachmentID3); - // assertEquals("sample.exe", attachmentMetadataEXE.get("fileName")); - // assertNull(attachmentMetadataEXE.get("customProperty3")); - // assertNull(attachmentMetadataEXE.get("customProperty4")); - // assertEquals(dropdownValue, attachmentMetadataEXE.get("customProperty1_code")); - // assertEquals(12, attachmentMetadataEXE.get("customProperty2")); - - // String expectedResponse = - // "[{\"code\":\"\",\"message\":\"The following secondary properties are not - // supported.\\n" - // + // - // "\\n" - // + // - // "\\t\\u2022 id1\\n" - // + // - // "\\n" - // + // - // "Please contact your administrator for assistance with any necessary - // adjustments.\\n" - // + // - // "\\n" - // + // - // "Table: attachments\\n" - // + // - // "Page: IntegrationTestEntity\",\"numericSeverity\":3}]"; - // if (response.equals(expectedResponse)) { - // System.out.println("Entity saved"); - // testStatus = true; - // System.out.println( - // "Rename & update unsuccessfull for invalid Secondary properties and successfull for - // valid property attachments"); - // } - // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID3); - // if (deleteEntityResponse != "Entity Deleted") { - // fail("Could not delete entity"); - // } - // } - // } - // if (!testStatus) { - // fail("Could not update secondary property before entity is saved"); - // } - // } + System.out.println("Creating attachment EXE"); + file = new File(classLoader.getResource("sample.exe").getFile()); + Map postData3 = new HashMap<>(); + postData3.put("up__ID", entityID4); + postData3.put("mimeType", "application/exe"); + postData3.put("createdAt", new Date().toString()); + postData3.put("createdBy", "test@test.com"); + postData3.put("modifiedBy", "test@test.com"); + + List createResponse3 = + api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData3, file); + if (createResponse3.get(0).equals("Attachment created")) { + attachmentID3 = createResponse3.get(1); + System.out.println("Attachment created"); + } - // @Test - // @Order(31) - // void testNAttachments_NewEntity() throws IOException { - // System.out.println( - // "Test (31): Creating new entity and checking only max 4 attachments are allowed to be - // uploaded"); - // System.out.println("Creating entity"); - // Boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (response != "Could not create entity") { - // entityID4 = response; - - // System.out.println("Entity created"); - - // System.out.println("Creating attachment PDF"); - // ClassLoader classLoader = getClass().getClassLoader(); - - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Map postData1 = new HashMap<>(); - // postData1.put("up__ID", entityID4); - // postData1.put("mimeType", "application/pdf"); - // postData1.put("createdAt", new Date().toString()); - // postData1.put("createdBy", "test@test.com"); - // postData1.put("modifiedBy", "test@test.com"); - - // List createResponse1 = - // api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData1, - // file); - // if (createResponse1.get(0).equals("Attachment created")) { - // attachmentID1 = createResponse1.get(1); - // System.out.println("Attachment created"); - // } + System.out.println("Creating second attachment pdf"); + file = new File(classLoader.getResource("sample1.pdf").getFile()); + Map postData4 = new HashMap<>(); + postData4.put("up__ID", entityID4); + postData4.put("mimeType", "application/pdf"); + postData4.put("createdAt", new Date().toString()); + postData4.put("createdBy", "test@test.com"); + postData4.put("modifiedBy", "test@test.com"); + + List createResponse4 = + api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData3, file); + if (createResponse4.get(0).equals("Attachment created")) { + attachmentID4 = createResponse4.get(1); + System.out.println("Attachment created"); + } - // System.out.println("Creating attachment TXT"); - // file = new File(classLoader.getResource("sample.txt").getFile()); - // Map postData2 = new HashMap<>(); - // postData2.put("up__ID", entityID4); - // postData2.put("mimeType", "application/txt"); - // postData2.put("createdAt", new Date().toString()); - // postData2.put("createdBy", "test@test.com"); - // postData2.put("modifiedBy", "test@test.com"); - - // List createResponse2 = - // api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData2, - // file); - // if (createResponse2.get(0).equals("Attachment created")) { - // attachmentID2 = createResponse2.get(1); - // System.out.println("Attachment created"); - // } + System.out.println("Creating third attachment pdf"); + file = new File(classLoader.getResource("sample2.pdf").getFile()); + Map postData5 = new HashMap<>(); + postData5.put("up__ID", entityID4); + postData5.put("mimeType", "application/pdf"); + postData5.put("createdAt", new Date().toString()); + postData5.put("createdBy", "test@test.com"); + postData5.put("modifiedBy", "test@test.com"); + + List createResponse5 = + api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData3, file); + if (createResponse5.get(0).equals("Only 4 attachments allowed.")) { + testStatus = true; + attachmentID5 = createResponse5.get(1); + System.out.println("Expected error received: Only 4 attachments allowed."); + } + String check = createResponse5.get(0); + if (check.equals("Attachment created")) { + testStatus = false; + } else { + response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID4); + if (response.equals("Saved")) { + String expectedJson = + "{\"error\":{\"code\":\"500\",\"message\":\"Cannot upload more than 4 attachments.\"}}"; + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode actualJsonNode = objectMapper.readTree(check); + JsonNode expectedJsonNode = objectMapper.readTree(expectedJson); + if (expectedJsonNode.equals(actualJsonNode)) { + testStatus = true; + } + } + } + } + if (!testStatus) { + fail("Attachment was created"); + } + } - // System.out.println("Creating attachment EXE"); - // file = new File(classLoader.getResource("sample.exe").getFile()); - // Map postData3 = new HashMap<>(); - // postData3.put("up__ID", entityID4); - // postData3.put("mimeType", "application/exe"); - // postData3.put("createdAt", new Date().toString()); - // postData3.put("createdBy", "test@test.com"); - // postData3.put("modifiedBy", "test@test.com"); - - // List createResponse3 = - // api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData3, - // file); - // if (createResponse3.get(0).equals("Attachment created")) { - // attachmentID3 = createResponse3.get(1); - // System.out.println("Attachment created"); - // } + @Test + @Order(32) + void testUploadNAttachments() throws IOException { + System.out.println("Test (32): Upload maximum 4 attachments in an exsisting entity"); - // System.out.println("Creating second attachment pdf"); - // file = new File(classLoader.getResource("sample1.pdf").getFile()); - // Map postData4 = new HashMap<>(); - // postData4.put("up__ID", entityID4); - // postData4.put("mimeType", "application/pdf"); - // postData4.put("createdAt", new Date().toString()); - // postData4.put("createdBy", "test@test.com"); - // postData4.put("modifiedBy", "test@test.com"); - - // List createResponse4 = - // api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData3, - // file); - // if (createResponse4.get(0).equals("Attachment created")) { - // attachmentID4 = createResponse4.get(1); - // System.out.println("Attachment created"); - // } + ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.exe").getFile()); - // System.out.println("Creating third attachment pdf"); - // file = new File(classLoader.getResource("sample2.pdf").getFile()); - // Map postData5 = new HashMap<>(); - // postData5.put("up__ID", entityID4); - // postData5.put("mimeType", "application/pdf"); - // postData5.put("createdAt", new Date().toString()); - // postData5.put("createdBy", "test@test.com"); - // postData5.put("modifiedBy", "test@test.com"); - - // List createResponse5 = - // api.createAttachment(appUrl, entityName, facetName, entityID4, srvpath, postData3, - // file); - // if (createResponse5.get(0).equals("Only 4 attachments allowed.")) { - // testStatus = true; - // attachmentID5 = createResponse5.get(1); - // System.out.println("Expected error received: Only 4 attachments allowed."); - // } - // String check = createResponse5.get(0); - // if (check.equals("Attachment created")) { - // testStatus = false; - // } else { - // response = api.saveEntityDraft(appUrl, entityName, srvpath, entityID4); - // if (response.equals("Saved")) { - // String expectedJson = - // "{\"error\":{\"code\":\"500\",\"message\":\"Cannot upload more than 4 - // attachments.\"}}"; - // ObjectMapper objectMapper = new ObjectMapper(); - // JsonNode actualJsonNode = objectMapper.readTree(check); - // JsonNode expectedJsonNode = objectMapper.readTree(expectedJson); - // if (expectedJsonNode.equals(actualJsonNode)) { - // testStatus = true; - // } - // } - // } - // } - // if (!testStatus) { - // fail("Attachment was created"); - // } - // } + boolean testStatus = false; + String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID4); + System.out.println("response: " + response); + + if ("Entity in draft mode".equals(response)) { + for (int i = 1; i <= 5; i++) { + // Ensure only one file is uploaded at a time and complete before next + File tempFile = File.createTempFile("sample_" + i + "_", ".exe"); + Files.copy(originalFile.toPath(), tempFile.toPath(), StandardCopyOption.REPLACE_EXISTING); + + Map postData = new HashMap<>(); + postData.put("up__ID", entityID4); + postData.put("mimeType", "application/exe"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, entityID4, srvpath, postData, tempFile); + + String resultMessage = createResponse.get(0); + System.out.println("Result message for attachment " + i + ": " + resultMessage); + + String expectedResponse = + "{\"error\":{\"code\":\"500\",\"message\":\"Cannot upload more than 4 attachments.\"}}"; + if (resultMessage.equals(expectedResponse)) { + ObjectMapper objectMapper = new ObjectMapper(); + JsonNode actualJsonNode = objectMapper.readTree(resultMessage); + JsonNode expectedJsonNode = objectMapper.readTree(expectedResponse); + if (expectedJsonNode.equals(actualJsonNode)) { + testStatus = true; + } + } else { + testStatus = false; + } + tempFile.delete(); + } + if (!testStatus) { + fail("5th attachment did not trigger the expected error."); + } + // Delete the newly created entity + String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID4); + if (deleteEntityResponse != "Entity Deleted") { + fail("Could not delete entity"); + } else { + System.out.println("Successfully deleted the test entity4"); + } + } + } - // @Test - // @Order(32) - // void testUploadNAttachments() throws IOException { - // System.out.println("Test (32): Upload maximum 4 attachments in an exsisting entity"); + @Test + @Order(33) + void testDiscardDraftWithoutAttachments() { + System.out.println("Test (33) : Discard draft without adding attachments"); - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.exe").getFile()); - - // boolean testStatus = false; - // String response = api.editEntityDraft(appUrl, entityName, srvpath, entityID4); - // System.out.println("response: " + response); - - // if ("Entity in draft mode".equals(response)) { - // for (int i = 1; i <= 5; i++) { - // // Ensure only one file is uploaded at a time and complete before next - // File tempFile = File.createTempFile("sample_" + i + "_", ".exe"); - // Files.copy(originalFile.toPath(), tempFile.toPath(), - // StandardCopyOption.REPLACE_EXISTING); - - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID4); - // postData.put("mimeType", "application/exe"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, entityID4, srvpath, postData, tempFile); - - // String resultMessage = createResponse.get(0); - // System.out.println("Result message for attachment " + i + ": " + resultMessage); - - // String expectedResponse = - // "{\"error\":{\"code\":\"500\",\"message\":\"Cannot upload more than 4 - // attachments.\"}}"; - // if (resultMessage.equals(expectedResponse)) { - // ObjectMapper objectMapper = new ObjectMapper(); - // JsonNode actualJsonNode = objectMapper.readTree(resultMessage); - // JsonNode expectedJsonNode = objectMapper.readTree(expectedResponse); - // if (expectedJsonNode.equals(actualJsonNode)) { - // testStatus = true; - // } - // } else { - // testStatus = false; - // } - // tempFile.delete(); - // } - // if (!testStatus) { - // fail("5th attachment did not trigger the expected error."); - // } - // // Delete the newly created entity - // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, entityID4); - // if (deleteEntityResponse != "Entity Deleted") { - // fail("Could not delete entity"); - // } else { - // System.out.println("Successfully deleted the test entity4"); - // } - // } - // } + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // @Test - // @Order(33) - // void testDiscardDraftWithoutAttachments() { - // System.out.println("Test (33) : Discard draft without adding attachments"); + if (response.equals("Could not create entity")) { + fail("Could not create entity"); + } - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + response = api.deleteEntityDraft(appUrl, entityName, response); + if (!response.equals("Entity Draft Deleted")) { + fail("Draft was not discarded properly"); + } + } - // if (response.equals("Could not create entity")) { - // fail("Could not create entity"); - // } + @Test + @Order(34) + void testDiscardDraftWithAttachments() throws IOException { + System.out.println("Test (34) : Discard draft with attachments"); + boolean testStatus = false; + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!response.equals("Could not create entity")) { + entityID7 = response; + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); - // response = api.deleteEntityDraft(appUrl, entityName, response); - // if (!response.equals("Entity Draft Deleted")) { - // fail("Draft was not discarded properly"); - // } - // } + Map postData1 = new HashMap<>(); + postData1.put("up__ID", entityID7); + postData1.put("mimeType", "application/pdf"); + postData1.put("createdAt", new Date().toString()); + postData1.put("createdBy", "test@test.com"); + postData1.put("modifiedBy", "test@test.com"); - // @Test - // @Order(34) - // void testDiscardDraftWithAttachments() throws IOException { - // System.out.println("Test (34) : Discard draft with attachments"); - // boolean testStatus = false; - // String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!response.equals("Could not create entity")) { - // entityID7 = response; - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - - // Map postData1 = new HashMap<>(); - // postData1.put("up__ID", entityID7); - // postData1.put("mimeType", "application/pdf"); - // postData1.put("createdAt", new Date().toString()); - // postData1.put("createdBy", "test@test.com"); - // postData1.put("modifiedBy", "test@test.com"); - - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, entityID7, srvpath, postData1, - // file); - // if (createResponse.get(0).equals("Attachment created")) { - // attachmentID1 = createResponse.get(1); - // } - // String check = createResponse.get(0); - // if (check.equals("Attachment created")) { - // response = api.deleteEntityDraft(appUrl, entityName, entityID7); - // } - // if (response.equals("Entity Draft Deleted")) { - // testStatus = true; - // } - // } - // if (!testStatus) { - // fail("Draft was not discarded properly"); - // } - // } + List createResponse = + api.createAttachment(appUrl, entityName, facetName, entityID7, srvpath, postData1, file); + if (createResponse.get(0).equals("Attachment created")) { + attachmentID1 = createResponse.get(1); + } + String check = createResponse.get(0); + if (check.equals("Attachment created")) { + response = api.deleteEntityDraft(appUrl, entityName, entityID7); + } + if (response.equals("Entity Draft Deleted")) { + testStatus = true; + } + } + if (!testStatus) { + fail("Draft was not discarded properly"); + } + } - // @Test - // @Order(35) - // void testCopyAttachmentsSuccessNewEntity() throws IOException { - // System.out.println("Test (35): Copy attachments from one entity to another new entity"); - // List attachments = new ArrayList<>(); - // copyAttachmentSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyAttachmentTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!copyAttachmentSourceEntity.equals("Could not create entity") - // && !copyAttachmentTargetEntity.equals("Could not create entity")) { - // ClassLoader classLoader = getClass().getClassLoader(); - // List files = new ArrayList<>(); - // files.add(new File(classLoader.getResource("sample.pdf").getFile())); - // files.add(new File(classLoader.getResource("sample1.pdf").getFile())); - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID7); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyAttachmentSourceEntity, srvpath, postData, - // file); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment"); - // } - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - // List> attachmentsMetadata = new ArrayList<>(); - // Map fetchAttachmentMetadataResponse; - // for (String attachment : attachments) { - // try { - // fetchAttachmentMetadataResponse = - // api.fetchMetadata( - // appUrl, entityName, facetName, copyAttachmentSourceEntity, attachment); - // attachmentsMetadata.add(fetchAttachmentMetadataResponse); - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } - // for (Map metadata : attachmentsMetadata) { - // if (metadata.containsKey("objectId")) { - // sourceObjectIds.add(metadata.get("objectId").toString()); - // } else { - // fail("Attachment metadata does not contain objectId"); - // } - // } + @Test + @Order(35) + void testCopyAttachmentsSuccessNewEntity() throws IOException { + System.out.println("Test (35): Copy attachments from one entity to another new entity"); + List attachments = new ArrayList<>(); + copyAttachmentSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyAttachmentTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!copyAttachmentSourceEntity.equals("Could not create entity") + && !copyAttachmentTargetEntity.equals("Could not create entity")) { + ClassLoader classLoader = getClass().getClassLoader(); + List files = new ArrayList<>(); + files.add(new File(classLoader.getResource("sample.pdf").getFile())); + files.add(new File(classLoader.getResource("sample1.pdf").getFile())); + Map postData = new HashMap<>(); + postData.put("up__ID", entityID7); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // if (sourceObjectIds.size() == 2) { - // String copyResponse; - // copyResponse = - // api.copyAttachment( - // appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); - // if (copyResponse.equals("Attachments copied successfully")) { - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // if (saveEntityResponse.equals("Saved")) { - // List> fetchEntityMetadataResponse; - // fetchEntityMetadataResponse = - // api.fetchEntityMetadata(appUrl, entityName, facetName, - // copyAttachmentTargetEntity); - // targetAttachmentIds = - // fetchEntityMetadataResponse.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // String readResponse; - // for (String targetAttachmentId : targetAttachmentIds) { - // readResponse = - // api.readAttachment( - // appUrl, - // entityName, - // facetName, - // copyAttachmentTargetEntity, - // targetAttachmentId); - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment"); - // } - // } - // } else { - // fail("Could not save entity after copying attachments: " + saveEntityResponse); - // } - // } else { - // fail("Could not copy attachments: " + copyResponse); - // } - // } else { - // fail("Could not fetch objects Ids for all attachments"); - // } - // } else { - // fail("Could not create entities"); - // } - // } + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, copyAttachmentSourceEntity, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + attachments.add(createResponse.get(1)); + } else { + fail("Could not create attachment"); + } + } + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + List> attachmentsMetadata = new ArrayList<>(); + Map fetchAttachmentMetadataResponse; + for (String attachment : attachments) { + try { + fetchAttachmentMetadataResponse = + api.fetchMetadata( + appUrl, entityName, facetName, copyAttachmentSourceEntity, attachment); + attachmentsMetadata.add(fetchAttachmentMetadataResponse); + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } + for (Map metadata : attachmentsMetadata) { + if (metadata.containsKey("objectId")) { + sourceObjectIds.add(metadata.get("objectId").toString()); + } else { + fail("Attachment metadata does not contain objectId"); + } + } - // @Test - // @Order(36) - // void testCopyAttachmentsUnsuccessfulNewEntity() throws IOException { - // System.out.println("Test (36): Copy attachments from one entity to another new entity"); - // String editResponse1 = - // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - // copyAttachmentTargetEntityEmpty = - // api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (editResponse1.equals("Entity in draft mode") - // && !copyAttachmentTargetEntityEmpty.equals("Could not create entity")) { - // sourceObjectIds.add("incorrectObjectId"); - // if (sourceObjectIds.size() == 3) { - // try { - // api.copyAttachment( - // appUrl, entityName, facetName, copyAttachmentTargetEntityEmpty, sourceObjectIds); - // fail("Copy attachments did not throw an error"); - // } catch (IOException e) { - // String saveEntityResponse1 = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - // String saveEntityResponse2 = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntityEmpty); - // String deleteResponse = - // api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntityEmpty); - // if (!saveEntityResponse1.equals("Saved") - // || !saveEntityResponse2.equals("Saved") - // || !deleteResponse.equals("Entity Deleted")) { - // fail("Could not save entities"); - // } - // } - // } else { - // fail("Could not fetch objects Ids for all attachments"); - // } - // } else { - // fail("Could not edit entities"); - // } - // } + if (sourceObjectIds.size() == 2) { + String copyResponse; + copyResponse = + api.copyAttachment( + appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); + if (copyResponse.equals("Attachments copied successfully")) { + String saveEntityResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + if (saveEntityResponse.equals("Saved")) { + List> fetchEntityMetadataResponse; + fetchEntityMetadataResponse = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); + targetAttachmentIds = + fetchEntityMetadataResponse.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + String readResponse; + for (String targetAttachmentId : targetAttachmentIds) { + readResponse = + api.readAttachment( + appUrl, + entityName, + facetName, + copyAttachmentTargetEntity, + targetAttachmentId); + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment"); + } + } + } else { + fail("Could not save entity after copying attachments: " + saveEntityResponse); + } + } else { + fail("Could not copy attachments: " + copyResponse); + } + } else { + fail("Could not fetch objects Ids for all attachments"); + } + } else { + fail("Could not create entities"); + } + } - // @Test - // @Order(37) - // void testCopyAttachmentWithNotesField() throws IOException { - // System.out.println( - // "Test (37): Create entity with attachment containing notes, copy to new entity and verify - // notes field"); - // Boolean testStatus = false; - // // Create source entity - // copyCustomSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyCustomSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + @Test + @Order(36) + void testCopyAttachmentsUnsuccessfulNewEntity() throws IOException { + System.out.println("Test (36): Copy attachments from one entity to another new entity"); + String editResponse1 = + api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + copyAttachmentTargetEntityEmpty = + api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (editResponse1.equals("Entity in draft mode") + && !copyAttachmentTargetEntityEmpty.equals("Could not create entity")) { + sourceObjectIds.add("incorrectObjectId"); + if (sourceObjectIds.size() == 3) { + try { + api.copyAttachment( + appUrl, entityName, facetName, copyAttachmentTargetEntityEmpty, sourceObjectIds); + fail("Copy attachments did not throw an error"); + } catch (IOException e) { + String saveEntityResponse1 = + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + String saveEntityResponse2 = + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntityEmpty); + String deleteResponse = + api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntityEmpty); + if (!saveEntityResponse1.equals("Saved") + || !saveEntityResponse2.equals("Saved") + || !deleteResponse.equals("Entity Deleted")) { + fail("Could not save entities"); + } + } + } else { + fail("Could not fetch objects Ids for all attachments"); + } + } else { + fail("Could not edit entities"); + } + } - // // Create and upload attachment to source entity - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // Map postData = new HashMap<>(); - // postData.put("up__ID", copyCustomSourceEntity); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + @Test + @Order(37) + void testCopyAttachmentWithNotesField() throws IOException { + System.out.println( + "Test (37): Create entity with attachment containing notes, copy to new entity and verify notes field"); + Boolean testStatus = false; + // Create source entity + copyCustomSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyCustomSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + // Create and upload attachment to source entity + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + Map postData = new HashMap<>(); + postData.put("up__ID", copyCustomSourceEntity); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - // String sourceAttachmentId = createResponse.get(1); + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } - // // Update attachment with notes field - // String notesValue = "This is a test note for copy attachment verification"; - // MediaType mediaType = MediaType.parse("application/json"); - // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - // RequestBody updateBody = RequestBody.create(jsonPayload, mediaType); + String sourceAttachmentId = createResponse.get(1); - // String updateResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, - // updateBody); + // Update attachment with notes field + String notesValue = "This is a test note for copy attachment verification"; + MediaType mediaType = MediaType.parse("application/json"); + String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + RequestBody updateBody = RequestBody.create(jsonPayload, mediaType); - // if (!updateResponse.equals("Updated")) { - // fail("Could not update attachment notes field"); - // } + String updateResponse = + api.updateSecondaryProperty( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, updateBody); - // // Save source entity - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity"); - // } + if (!updateResponse.equals("Updated")) { + fail("Could not update attachment notes field"); + } - // // Fetch attachment metadata to get objectId - // Map sourceAttachmentMetadata = - // api.fetchMetadata( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + // Save source entity + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity"); + } - // if (!sourceAttachmentMetadata.containsKey("objectId")) { - // fail("Source attachment metadata does not contain objectId"); - // } + // Fetch attachment metadata to get objectId + Map sourceAttachmentMetadata = + api.fetchMetadata( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - // // Store objectId in array - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // if (sourceObjectIds.isEmpty()) { - // sourceObjectIds.add(sourceObjectId); - // } else { - // sourceObjectIds.set(0, sourceObjectId); - // } + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId"); + } - // String sourceNoteValue = - // sourceAttachmentMetadata.get("note") != null - // ? sourceAttachmentMetadata.get("note").toString() - // : null; - - // if (!notesValue.equals(sourceNoteValue)) { - // fail( - // "Notes field was not properly set in source attachment. Expected: " - // + notesValue - // + ", Got: " - // + sourceNoteValue); - // } + // Store objectId in array + String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + if (sourceObjectIds.isEmpty()) { + sourceObjectIds.add(sourceObjectId); + } else { + sourceObjectIds.set(0, sourceObjectId); + } - // // Create target entity - // copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyCustomTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + String sourceNoteValue = + sourceAttachmentMetadata.get("note") != null + ? sourceAttachmentMetadata.get("note").toString() + : null; + + if (!notesValue.equals(sourceNoteValue)) { + fail( + "Notes field was not properly set in source attachment. Expected: " + + notesValue + + ", Got: " + + sourceNoteValue); + } - // // Copy attachment to target entity - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(0)); // Use objectId from array + // Create target entity + copyCustomTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyCustomTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, - // objectIdsToCopy); + // Copy attachment to target entity + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(0)); // Use objectId from array - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity: " + copyResponse); - // } + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - // // Save target entity - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity"); - // } + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity: " + copyResponse); + } - // // Fetch target entity attachments metadata - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + // Save target entity + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity"); + } - // if (targetAttachmentsMetadata.isEmpty()) { - // fail("No attachments found in target entity"); - // } + // Fetch target entity attachments metadata + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - // // Verify the copied attachment has the same notes value - // Map copiedAttachmentMetadata = targetAttachmentsMetadata.get(0); - // String copiedNoteValue = - // copiedAttachmentMetadata.get("note") != null - // ? copiedAttachmentMetadata.get("note").toString() - // : null; - - // if (!notesValue.equals(copiedNoteValue)) { - // fail( - // "Notes field was not properly copied. Expected: " - // + notesValue - // + ", Got: " - // + copiedNoteValue); - // } + if (targetAttachmentsMetadata.isEmpty()) { + fail("No attachments found in target entity"); + } - // // Verify attachment content can be read from target entity - // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - // String readResponse = - // api.readAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + // Verify the copied attachment has the same notes value + Map copiedAttachmentMetadata = targetAttachmentsMetadata.get(0); + String copiedNoteValue = + copiedAttachmentMetadata.get("note") != null + ? copiedAttachmentMetadata.get("note").toString() + : null; + + if (!notesValue.equals(copiedNoteValue)) { + fail( + "Notes field was not properly copied. Expected: " + + notesValue + + ", Got: " + + copiedNoteValue); + } - // if (readResponse.equals("OK")) { - // testStatus = true; - // } - // if (!testStatus) { - // fail("Could not verify that notes field was copied from source to target attachment"); - // } - // } + // Verify attachment content can be read from target entity + String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + String readResponse = + api.readAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - // @Test - // @Order(38) - // void testCopyAttachmentWithSecondaryPropertiesField() throws IOException { - // System.out.println( - // "Test (38): Verify that secondary properties are preserved when copying attachments - // between entities"); - // Boolean testStatus = false; - - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // copyCustomSourceEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit source entity"); - // } + if (readResponse.equals("OK")) { + testStatus = true; + } + if (!testStatus) { + fail("Could not verify that notes field was copied from source to target attachment"); + } + } - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample1.pdf").getFile()); + @Test + @Order(38) + void testCopyAttachmentWithSecondaryPropertiesField() throws IOException { + System.out.println( + "Test (38): Verify that secondary properties are preserved when copying attachments between entities"); + Boolean testStatus = false; - // Map postData = new HashMap<>(); - // postData.put("up__ID", copyCustomSourceEntity); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source entity"); + } - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample1.pdf").getFile()); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } + Map postData = new HashMap<>(); + postData.put("up__ID", copyCustomSourceEntity); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // String sourceAttachmentId = createResponse.get(1); - - // // Update attachment with secondary properties - // // DocumentInfoRecordBoolean : Set to true - // RequestBody bodyBoolean = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, - // bodyBoolean); - - // if (!updateSecondaryPropertyResponse1.equals("Updated")) { - // fail( - // "Could not update attachment DocumentInfoRecordBoolean field. Response: " - // + updateSecondaryPropertyResponse1); - // } + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - // // customProperty2 : Set to 12345 - // Integer customProperty2Value = 12345; - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty2\" : " + customProperty2Value + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyInt); - - // if (!updateSecondaryPropertyResponse2.equals("Updated")) { - // fail( - // "Could not update attachment customProperty2 field. Response: " - // + updateSecondaryPropertyResponse2); - // } + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } - // // Save source entity - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity. Response: " + saveSourceResponse); - // } + String sourceAttachmentId = createResponse.get(1); + + // Update attachment with secondary properties + // DocumentInfoRecordBoolean : Set to true + RequestBody bodyBoolean = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyBoolean); + + if (!updateSecondaryPropertyResponse1.equals("Updated")) { + fail( + "Could not update attachment DocumentInfoRecordBoolean field. Response: " + + updateSecondaryPropertyResponse1); + } - // // Fetch attachment metadata to get objectId and verify secondary properties - // Map sourceAttachmentMetadata = - // api.fetchMetadata( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + // customProperty2 : Set to 12345 + Integer customProperty2Value = 12345; + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty2\" : " + customProperty2Value + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyInt); + + if (!updateSecondaryPropertyResponse2.equals("Updated")) { + fail( + "Could not update attachment customProperty2 field. Response: " + + updateSecondaryPropertyResponse2); + } - // if (!sourceAttachmentMetadata.containsKey("objectId")) { - // fail("Source attachment metadata does not contain objectId"); - // } + // Save source entity + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity. Response: " + saveSourceResponse); + } - // // Store objectId in array for reuse - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // if (sourceObjectIds.size() < 2) { - // sourceObjectIds.add(sourceObjectId); - // } else { - // sourceObjectIds.set(1, sourceObjectId); - // } + // Fetch attachment metadata to get objectId and verify secondary properties + Map sourceAttachmentMetadata = + api.fetchMetadata( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - // // Verify all secondary properties in source attachment - // Boolean sourceCustomProperty6 = - // sourceAttachmentMetadata.get("customProperty6") != null - // ? (Boolean) sourceAttachmentMetadata.get("customProperty6") - // : null; - // Integer sourceCustomProperty2 = - // sourceAttachmentMetadata.get("customProperty2") != null - // ? (Integer) sourceAttachmentMetadata.get("customProperty2") - // : null; - - // if (sourceCustomProperty6 == null || !sourceCustomProperty6) { - // fail( - // "DocumentInfoRecordBoolean was not properly set in source attachment. Expected: true, - // Got: " - // + sourceCustomProperty6); - // } + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId"); + } - // if (!customProperty2Value.equals(sourceCustomProperty2)) { - // fail( - // "customProperty2 was not properly set in source attachment. Expected: " - // + customProperty2Value - // + ", Got: " - // + sourceCustomProperty2); - // } + // Store objectId in array for reuse + String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + if (sourceObjectIds.size() < 2) { + sourceObjectIds.add(sourceObjectId); + } else { + sourceObjectIds.set(1, sourceObjectId); + } - // String editTargetResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!editTargetResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity"); - // } + // Verify all secondary properties in source attachment + Boolean sourceCustomProperty6 = + sourceAttachmentMetadata.get("customProperty6") != null + ? (Boolean) sourceAttachmentMetadata.get("customProperty6") + : null; + Integer sourceCustomProperty2 = + sourceAttachmentMetadata.get("customProperty2") != null + ? (Integer) sourceAttachmentMetadata.get("customProperty2") + : null; + + if (sourceCustomProperty6 == null || !sourceCustomProperty6) { + fail( + "DocumentInfoRecordBoolean was not properly set in source attachment. Expected: true, Got: " + + sourceCustomProperty6); + } - // // Copy attachment to target entity - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(1)); // Use objectId from array + if (!customProperty2Value.equals(sourceCustomProperty2)) { + fail( + "customProperty2 was not properly set in source attachment. Expected: " + + customProperty2Value + + ", Got: " + + sourceCustomProperty2); + } - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, - // objectIdsToCopy); + String editTargetResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!editTargetResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity"); + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity: " + copyResponse); - // } + // Copy attachment to target entity + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(1)); // Use objectId from array - // // Save target entity - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity"); - // } + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - // // Fetch target entity attachments metadata - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity: " + copyResponse); + } - // if (targetAttachmentsMetadata.isEmpty()) { - // fail("No attachments found in target entity"); - // } + // Save target entity + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity"); + } - // // Verify the copied attachment has the same secondary properties - // // Find the attachment we just copied by matching the filename - // Map copiedAttachmentMetadata = - // targetAttachmentsMetadata.stream() - // .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) - // .findFirst() - // .orElse(null); + // Fetch target entity attachments metadata + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - // if (copiedAttachmentMetadata == null) { - // fail("Could not find the copied attachment with file in target entity"); - // } + if (targetAttachmentsMetadata.isEmpty()) { + fail("No attachments found in target entity"); + } - // Boolean copiedCustomProperty6 = - // copiedAttachmentMetadata.get("customProperty6") != null - // ? (Boolean) copiedAttachmentMetadata.get("customProperty6") - // : null; - // Integer copiedCustomProperty2 = - // copiedAttachmentMetadata.get("customProperty2") != null - // ? (Integer) copiedAttachmentMetadata.get("customProperty2") - // : null; - - // // Verify DocumentInfoRecordBoolean - // if (copiedCustomProperty6 == null || !copiedCustomProperty6) { - // fail( - // "DocumentInfoRecordBoolean as not properly copied. Expected: true, Got: " - // + copiedCustomProperty6); - // } + // Verify the copied attachment has the same secondary properties + // Find the attachment we just copied by matching the filename + Map copiedAttachmentMetadata = + targetAttachmentsMetadata.stream() + .filter(attachment -> "sample1.pdf".equals(attachment.get("fileName"))) + .findFirst() + .orElse(null); - // // Verify customProperty2 - // if (!customProperty2Value.equals(copiedCustomProperty2)) { - // fail( - // "customProperty2 was not properly copied. Expected: " - // + customProperty2Value - // + ", Got: " - // + copiedCustomProperty2); - // } + if (copiedAttachmentMetadata == null) { + fail("Could not find the copied attachment with file in target entity"); + } - // // Verify attachment content can be read from target entity - // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - // String readResponse = - // api.readAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + Boolean copiedCustomProperty6 = + copiedAttachmentMetadata.get("customProperty6") != null + ? (Boolean) copiedAttachmentMetadata.get("customProperty6") + : null; + Integer copiedCustomProperty2 = + copiedAttachmentMetadata.get("customProperty2") != null + ? (Integer) copiedAttachmentMetadata.get("customProperty2") + : null; + + // Verify DocumentInfoRecordBoolean + if (copiedCustomProperty6 == null || !copiedCustomProperty6) { + fail( + "DocumentInfoRecordBoolean as not properly copied. Expected: true, Got: " + + copiedCustomProperty6); + } - // if (readResponse.equals("OK")) { - // testStatus = true; - // } - // if (!testStatus) { - // fail( - // "Could not verify that all secondary properties were copied from source to target - // attachment"); - // } - // } + // Verify customProperty2 + if (!customProperty2Value.equals(copiedCustomProperty2)) { + fail( + "customProperty2 was not properly copied. Expected: " + + customProperty2Value + + ", Got: " + + copiedCustomProperty2); + } - // @Test - // @Order(39) - // void testCopyAttachmentWithNotesAndSecondaryPropertiesField() throws IOException { - // System.out.println( - // "Test (39): Verify that both notes field and secondary properties are preserved during - // attachment copy"); - // Boolean testStatus = false; - - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // copyCustomSourceEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit source entity"); - // } + // Verify attachment content can be read from target entity + String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + String readResponse = + api.readAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample2.pdf").getFile()); + if (readResponse.equals("OK")) { + testStatus = true; + } + if (!testStatus) { + fail( + "Could not verify that all secondary properties were copied from source to target attachment"); + } + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", copyCustomSourceEntity); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + @Test + @Order(39) + void testCopyAttachmentWithNotesAndSecondaryPropertiesField() throws IOException { + System.out.println( + "Test (39): Verify that both notes field and secondary properties are preserved during attachment copy"); + Boolean testStatus = false; - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit source entity"); + } - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment"); - // } + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample2.pdf").getFile()); - // String sourceAttachmentId = createResponse.get(1); - - // // Update attachment with notes field - // String notesValue = "This attachment has both notes and secondary properties for testing"; - // MediaType mediaType = MediaType.parse("application/json"); - // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - // RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); - - // String updateNotesResponse = - // api.updateSecondaryProperty( - // appUrl, - // entityName, - // facetName, - // copyCustomSourceEntity, - // sourceAttachmentId, - // updateNotesBody); - - // if (!updateNotesResponse.equals("Updated")) { - // fail("Could not update attachment notes field"); - // } + Map postData = new HashMap<>(); + postData.put("up__ID", copyCustomSourceEntity); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // Update attachment with secondary properties - // // DocumentInfoRecordBoolean : Set to true - // RequestBody bodyBoolean = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); - // String updateSecondaryPropertyResponse1 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, - // bodyBoolean); - - // if (!updateSecondaryPropertyResponse1.equals("Updated")) { - // fail( - // "Could not update attachment DocumentInfoRecordBoolean (customProperty6) field. - // Response: " - // + updateSecondaryPropertyResponse1); - // } + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, copyCustomSourceEntity, srvpath, postData, file); - // // customProperty2 : Set to 99999 - // Integer customProperty2Value = 99999; - // RequestBody bodyInt = - // RequestBody.create( - // MediaType.parse("application/json"), - // ByteString.encodeUtf8("{\n \"customProperty2\" : " + customProperty2Value + "\n}")); - // String updateSecondaryPropertyResponse2 = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyInt); - - // if (!updateSecondaryPropertyResponse2.equals("Updated")) { - // fail( - // "Could not update attachment customProperty2 field. Response: " - // + updateSecondaryPropertyResponse2); - // } + if (!createResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment"); + } - // // Save source entity - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity. Response: " + saveSourceResponse); - // } + String sourceAttachmentId = createResponse.get(1); - // // Fetch attachment metadata to get objectId and verify notes and secondary properties - // Map sourceAttachmentMetadata = - // api.fetchMetadata( - // appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); + // Update attachment with notes field + String notesValue = "This attachment has both notes and secondary properties for testing"; + MediaType mediaType = MediaType.parse("application/json"); + String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); - // if (!sourceAttachmentMetadata.containsKey("objectId")) { - // fail("Source attachment metadata does not contain objectId"); - // } + String updateNotesResponse = + api.updateSecondaryProperty( + appUrl, + entityName, + facetName, + copyCustomSourceEntity, + sourceAttachmentId, + updateNotesBody); - // String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); - // if (sourceObjectIds.size() < 3) { - // sourceObjectIds.add(sourceObjectId); - // } else { - // sourceObjectIds.set(2, sourceObjectId); - // } + if (!updateNotesResponse.equals("Updated")) { + fail("Could not update attachment notes field"); + } - // String sourceNoteValue = - // sourceAttachmentMetadata.get("note") != null - // ? sourceAttachmentMetadata.get("note").toString() - // : null; - - // if (!notesValue.equals(sourceNoteValue)) { - // fail( - // "Notes field was not properly set in source attachment. Expected: " - // + notesValue - // + ", Got: " - // + sourceNoteValue); - // } + // Update attachment with secondary properties + // DocumentInfoRecordBoolean : Set to true + RequestBody bodyBoolean = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty6\" : " + true + "\n}")); + String updateSecondaryPropertyResponse1 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyBoolean); + + if (!updateSecondaryPropertyResponse1.equals("Updated")) { + fail( + "Could not update attachment DocumentInfoRecordBoolean (customProperty6) field. Response: " + + updateSecondaryPropertyResponse1); + } - // Boolean sourceCustomProperty6 = - // sourceAttachmentMetadata.get("customProperty6") != null - // ? (Boolean) sourceAttachmentMetadata.get("customProperty6") - // : null; - // Integer sourceCustomProperty2 = - // sourceAttachmentMetadata.get("customProperty2") != null - // ? (Integer) sourceAttachmentMetadata.get("customProperty2") - // : null; - - // if (sourceCustomProperty6 == null || !sourceCustomProperty6) { - // fail( - // "DocumentInfoRecordBoolean was not properly set in source attachment. Expected: true, - // Got: " - // + sourceCustomProperty6); - // } + // customProperty2 : Set to 99999 + Integer customProperty2Value = 99999; + RequestBody bodyInt = + RequestBody.create( + MediaType.parse("application/json"), + ByteString.encodeUtf8("{\n \"customProperty2\" : " + customProperty2Value + "\n}")); + String updateSecondaryPropertyResponse2 = + api.updateSecondaryProperty( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId, bodyInt); + + if (!updateSecondaryPropertyResponse2.equals("Updated")) { + fail( + "Could not update attachment customProperty2 field. Response: " + + updateSecondaryPropertyResponse2); + } - // if (!customProperty2Value.equals(sourceCustomProperty2)) { - // fail( - // "customProperty2 was not properly set in source attachment. Expected: " - // + customProperty2Value - // + ", Got: " - // + sourceCustomProperty2); - // } + // Save source entity + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity. Response: " + saveSourceResponse); + } - // String editTargetResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!editTargetResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity"); - // } + // Fetch attachment metadata to get objectId and verify notes and secondary properties + Map sourceAttachmentMetadata = + api.fetchMetadata( + appUrl, entityName, facetName, copyCustomSourceEntity, sourceAttachmentId); - // // Copy attachment to target entity - // List objectIdsToCopy = new ArrayList<>(); - // objectIdsToCopy.add(sourceObjectIds.get(2)); // Use objectId from array + if (!sourceAttachmentMetadata.containsKey("objectId")) { + fail("Source attachment metadata does not contain objectId"); + } - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, - // objectIdsToCopy); + String sourceObjectId = sourceAttachmentMetadata.get("objectId").toString(); + if (sourceObjectIds.size() < 3) { + sourceObjectIds.add(sourceObjectId); + } else { + sourceObjectIds.set(2, sourceObjectId); + } - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy attachment to target entity: " + copyResponse); - // } + String sourceNoteValue = + sourceAttachmentMetadata.get("note") != null + ? sourceAttachmentMetadata.get("note").toString() + : null; + + if (!notesValue.equals(sourceNoteValue)) { + fail( + "Notes field was not properly set in source attachment. Expected: " + + notesValue + + ", Got: " + + sourceNoteValue); + } - // // Save target entity - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity"); - // } + Boolean sourceCustomProperty6 = + sourceAttachmentMetadata.get("customProperty6") != null + ? (Boolean) sourceAttachmentMetadata.get("customProperty6") + : null; + Integer sourceCustomProperty2 = + sourceAttachmentMetadata.get("customProperty2") != null + ? (Integer) sourceAttachmentMetadata.get("customProperty2") + : null; + + if (sourceCustomProperty6 == null || !sourceCustomProperty6) { + fail( + "DocumentInfoRecordBoolean was not properly set in source attachment. Expected: true, Got: " + + sourceCustomProperty6); + } - // // Fetch target entity attachments metadata - // List> targetAttachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); + if (!customProperty2Value.equals(sourceCustomProperty2)) { + fail( + "customProperty2 was not properly set in source attachment. Expected: " + + customProperty2Value + + ", Got: " + + sourceCustomProperty2); + } - // if (targetAttachmentsMetadata.isEmpty()) { - // fail("No attachments found in target entity"); - // } + String editTargetResponse = + api.editEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!editTargetResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity"); + } - // // Verify the copied attachment has the same notes and secondary properties - // // Find the attachment we just copied by matching the filename - // Map copiedAttachmentMetadata = - // targetAttachmentsMetadata.stream() - // .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) - // .findFirst() - // .orElse(null); + // Copy attachment to target entity + List objectIdsToCopy = new ArrayList<>(); + objectIdsToCopy.add(sourceObjectIds.get(2)); // Use objectId from array - // if (copiedAttachmentMetadata == null) { - // fail("Could not find the copied attachment with fil in target entity"); - // } + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyCustomTargetEntity, objectIdsToCopy); - // // Verify notes field was copied - // String copiedNoteValue = - // copiedAttachmentMetadata.get("note") != null - // ? copiedAttachmentMetadata.get("note").toString() - // : null; - - // if (!notesValue.equals(copiedNoteValue)) { - // fail( - // "Notes field was not properly copied. Expected: " - // + notesValue - // + ", Got: " - // + copiedNoteValue); - // } + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy attachment to target entity: " + copyResponse); + } - // // Verify secondary properties were copied - // Boolean copiedCustomProperty6 = - // copiedAttachmentMetadata.get("customProperty6") != null - // ? (Boolean) copiedAttachmentMetadata.get("customProperty6") - // : null; - // Integer copiedCustomProperty2 = - // copiedAttachmentMetadata.get("customProperty2") != null - // ? (Integer) copiedAttachmentMetadata.get("customProperty2") - // : null; - - // // Verify DocumentInfoRecordBoolean - // if (copiedCustomProperty6 == null || !copiedCustomProperty6) { - // fail( - // "DocumentInfoRecordBoolean was not properly copied. Expected: true, Got: " - // + copiedCustomProperty6); - // } + // Save target entity + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyCustomTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity"); + } - // // Verify customProperty2 - // if (!customProperty2Value.equals(copiedCustomProperty2)) { - // fail( - // "customProperty2 was not properly copied. Expected: " - // + customProperty2Value - // + ", Got: " - // + copiedCustomProperty2); - // } + // Fetch target entity attachments metadata + List> targetAttachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyCustomTargetEntity); - // // Verify attachment content can be read from target entity - // String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); - // String readResponse = - // api.readAttachment( - // appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); + if (targetAttachmentsMetadata.isEmpty()) { + fail("No attachments found in target entity"); + } - // if (readResponse.equals("OK")) { - // testStatus = true; - // } - // if (!testStatus) { - // fail( - // "Could not verify that notes field and all secondary properties were copied from source - // to target attachment"); - // } - // api.deleteEntity(appUrl, entityName, copyCustomSourceEntity); - // api.deleteEntity(appUrl, entityName, copyCustomTargetEntity); - // } + // Verify the copied attachment has the same notes and secondary properties + // Find the attachment we just copied by matching the filename + Map copiedAttachmentMetadata = + targetAttachmentsMetadata.stream() + .filter(attachment -> "sample2.pdf".equals(attachment.get("fileName"))) + .findFirst() + .orElse(null); - // @Test - // @Order(40) - // void testCopyAttachmentsSuccessExistingEntity() throws IOException { - // System.out.println("Test (40): Copy attachments from one entity to another existing entity"); - // List attachments = new ArrayList<>(); - // ClassLoader classLoader = getClass().getClassLoader(); - // List files = new ArrayList<>(); - // File file1 = new File(classLoader.getResource("sample.pdf").getFile()); - // File file2 = new File(classLoader.getResource("sample1.pdf").getFile()); - // File tempFile1 = new File(System.getProperty("java.io.tmpdir"), - // "sample_copy_existing_1.pdf"); - // Files.copy(file1.toPath(), tempFile1.toPath(), StandardCopyOption.REPLACE_EXISTING); - // File tempFile2 = new File(System.getProperty("java.io.tmpdir"), - // "sample_copy_existing_2.pdf"); - // Files.copy(file2.toPath(), tempFile2.toPath(), StandardCopyOption.REPLACE_EXISTING); - // files.add(tempFile1); - // files.add(tempFile2); - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID7); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - // String editResponse1 = - // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - // String editResponse2 = - // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // if (editResponse1.equals("Entity in draft mode") - // && editResponse2.equals("Entity in draft mode")) { - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyAttachmentSourceEntity, srvpath, postData, - // file); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment"); - // } - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - // List> attachmentsMetadata = new ArrayList<>(); - // Map fetchAttachmentMetadataResponse; - // for (String attachment : attachments) { - // try { - // fetchAttachmentMetadataResponse = - // api.fetchMetadata( - // appUrl, entityName, facetName, copyAttachmentSourceEntity, attachment); - // attachmentsMetadata.add(fetchAttachmentMetadataResponse); - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } + if (copiedAttachmentMetadata == null) { + fail("Could not find the copied attachment with fil in target entity"); + } - // sourceObjectIds.clear(); - // for (Map metadata : attachmentsMetadata) { - // if (metadata.containsKey("objectId")) { - // sourceObjectIds.add(metadata.get("objectId").toString()); - // } else { - // fail("Attachment metadata does not contain objectId"); - // } - // } + // Verify notes field was copied + String copiedNoteValue = + copiedAttachmentMetadata.get("note") != null + ? copiedAttachmentMetadata.get("note").toString() + : null; + + if (!notesValue.equals(copiedNoteValue)) { + fail( + "Notes field was not properly copied. Expected: " + + notesValue + + ", Got: " + + copiedNoteValue); + } - // if (sourceObjectIds.size() == 2) { - // String copyResponse; - // copyResponse = - // api.copyAttachment( - // appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); - // if (copyResponse.equals("Attachments copied successfully")) { - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // if (saveEntityResponse.equals("Saved")) { - // List> fetchEntityMetadataResponse; - // fetchEntityMetadataResponse = - // api.fetchEntityMetadata(appUrl, entityName, facetName, - // copyAttachmentTargetEntity); - // targetAttachmentIds = - // fetchEntityMetadataResponse.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // String readResponse; - // if (targetAttachmentIds.size() == 4) { - // for (String targetAttachmentId : targetAttachmentIds) { - // readResponse = - // api.readAttachment( - // appUrl, - // entityName, - // facetName, - // copyAttachmentTargetEntity, - // targetAttachmentId); - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment"); - // } - // } - // } - // // api.deleteEntity(appUrl, entityName, copyAttachmentSourceEntity); - // // api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntity); - // } else { - // fail("Could not save entity after copying attachments: " + saveEntityResponse); - // } - // } else { - // fail("Could not copy attachments: " + copyResponse); - // } - // } else { - // fail("Could not fetch objects Ids for all attachments"); - // } - // } else { - // fail("Could not edit entities"); - // } - // } + // Verify secondary properties were copied + Boolean copiedCustomProperty6 = + copiedAttachmentMetadata.get("customProperty6") != null + ? (Boolean) copiedAttachmentMetadata.get("customProperty6") + : null; + Integer copiedCustomProperty2 = + copiedAttachmentMetadata.get("customProperty2") != null + ? (Integer) copiedAttachmentMetadata.get("customProperty2") + : null; + + // Verify DocumentInfoRecordBoolean + if (copiedCustomProperty6 == null || !copiedCustomProperty6) { + fail( + "DocumentInfoRecordBoolean was not properly copied. Expected: true, Got: " + + copiedCustomProperty6); + } - // @Test - // @Order(41) - // void testCopyAttachmentsUnsuccessfulExistingEntity() throws IOException { - // System.out.println( - // "Test (41): Copy attachments from one entity to another existing entity - unsuccessful"); - // String editResponse1 = - // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - // String editResponse2 = - // api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // if (editResponse1.equals("Entity in draft mode") - // && editResponse2.equals("Entity in draft mode")) { - // sourceObjectIds.add("incorrectObjectId"); - // if (sourceObjectIds.size() == 3) { - // try { - // api.copyAttachment( - // appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); - // fail("Copy attachments did not throw an error"); - // } catch (IOException e) { - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntity); - // api.deleteEntity(appUrl, entityName, copyAttachmentSourceEntity); - // } - // } else { - // fail("Could not fetch objects Ids for all attachments"); - // } - // } else { - // fail("Could not edit entities"); - // } - // } + // Verify customProperty2 + if (!customProperty2Value.equals(copiedCustomProperty2)) { + fail( + "customProperty2 was not properly copied. Expected: " + + customProperty2Value + + ", Got: " + + copiedCustomProperty2); + } - // @Test - // @Order(42) - // void testCreateLinkSuccess() throws IOException { - // System.out.println("Test (42): Create link in entity"); - // List attachments = new ArrayList<>(); - // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!createLinkEntity.equals("Could not create entity")) { - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse1 = - // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - // String createLinkResponse2 = - // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName + "1", - // linkUrl); - // if (createLinkResponse1.equals("Link created successfully") - // && createLinkResponse2.equals("Link created successfully")) { - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (saveEntityResponse.equals("Saved")) { - // attachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // String openAttachmentResponse; - // for (String attachment : attachments) { - // openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, createLinkEntity, attachment); - // System.out.println("openAttachmentResponse: " + openAttachmentResponse); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open created link"); - // } - // } - // } else { - // fail("Could not save entity"); - // } - // } else { - // fail("Could not create link"); - // } - // } else { - // fail("Could not create entity"); - // } - // } + // Verify attachment content can be read from target entity + String targetAttachmentId = (String) copiedAttachmentMetadata.get("ID"); + String readResponse = + api.readAttachment( + appUrl, entityName, facetName, copyCustomTargetEntity, targetAttachmentId); - // @Test - // @Order(43) - // void testCreateLinkDifferentEntity() throws IOException { - // System.out.println("Test (43): Create link with same name in different entity"); - // String createLinkDifferentEntity = - // api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!createLinkDifferentEntity.equals("Could not edit entity")) { - // String linkName = "sample"; - // String linkUrl = "https://example.com"; - // String createResponse = - // api.createLink( - // appUrl, entityName, facetName, createLinkDifferentEntity, linkName, linkUrl); - // if (!createResponse.equals("Link created successfully")) { - // fail("Could not create link in different entity with same name"); - // } - // String response = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkDifferentEntity); - // if (!response.equals("Saved")) { - // fail("Could not save entity"); - // } - // response = api.deleteEntity(appUrl, entityName, createLinkDifferentEntity); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } else { - // fail("Could not edit entity"); - // } - // } + if (readResponse.equals("OK")) { + testStatus = true; + } + if (!testStatus) { + fail( + "Could not verify that notes field and all secondary properties were copied from source to target attachment"); + } + api.deleteEntity(appUrl, entityName, copyCustomSourceEntity); + api.deleteEntity(appUrl, entityName, copyCustomTargetEntity); + } - // @Test - // @Order(44) - // void testCreateLinkFailure() throws IOException { - // System.out.println("Test (44): Create link fails due to invalid URL and name"); - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!editEntityResponse.equals("Could not edit entity")) { - // String linkName = "sample"; - // String linkUrl = "example.com"; - // try { - // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - // fail("Create link did not throw an error for invalid url"); - // } catch (IOException e) { - // String message = e.getMessage(); - // int jsonStart = message.indexOf("{"); - // String jsonPart = message.substring(jsonStart); - // JSONObject json = new JSONObject(jsonPart); - // String errorCode = json.getJSONObject("error").getString("code"); - // String errorMessage = json.getJSONObject("error").getString("message"); - // assertEquals("400018", errorCode); - // assertTrue( - // errorMessage.equals("Enter a value that is within the expected pattern.") - // || errorMessage.equals("Enter a value that matches the expected pattern."), - // "Unexpected error message: " + errorMessage); - // } - // try { - // api.createLink( - // appUrl, entityName, facetName, createLinkEntity, linkName + "//", "https://" + - // linkUrl); - // fail("Create link did not throw an error for invalid name"); - // } catch (IOException e) { - // String message = e.getMessage(); - // int jsonStart = message.indexOf("{"); - // String jsonPart = message.substring(jsonStart); - // JSONObject json = new JSONObject(jsonPart); - // String errorCode = json.getJSONObject("error").getString("code"); - // String errorMessage = json.getJSONObject("error").getString("message"); - // String expected = - // "\"sample//\" contains unsupported characters (‘/’ or ‘\\’). Rename and try again."; - // assertEquals("500", errorCode); - // assertEquals( - // expected.replaceAll("\\s+", " ").trim(), errorMessage.replaceAll("\\s+", " - // ").trim()); - // } - // try { - // api.createLink(appUrl, entityName, facetName, createLinkEntity, "", ""); - // fail("Create link did not throw an error for empty name and url"); - // } catch (IOException e) { - // String message = e.getMessage(); - // int jsonStart = message.indexOf("{"); - // String jsonPart = message.substring(jsonStart); - // JSONObject json = new JSONObject(jsonPart); - // String errorCode = json.getJSONObject("error").getString("code"); - // String errorMessage = json.getJSONObject("error").getString("message"); - // String expected = "Provide the missing value."; - // assertEquals("409008", errorCode); - // assertEquals(expected, errorMessage); - // } - // try { - // api.createLink( - // appUrl, entityName, facetName, createLinkEntity, linkName, "https://" + linkUrl); - // fail("Create link did not throw an error for duplicate name"); - // } catch (IOException e) { - // String message = e.getMessage(); - // int jsonStart = message.indexOf("{"); - // String jsonPart = message.substring(jsonStart); - // JSONObject json = new JSONObject(jsonPart); - // String errorCode = json.getJSONObject("error").getString("code"); - // String errorMessage = json.getJSONObject("error").getString("message"); - // assertEquals("500", errorCode); - // assertEquals( - // "An object named \"sample\" already exists. Rename the object and try again.", - // errorMessage); - // } - // try { - // for (int i = 2; i < 5; i++) { - // api.createLink( - // appUrl, entityName, facetName, createLinkEntity, linkName + i, "https://" + - // linkUrl); - // } - // fail("More than 5 links were created in the same entity"); - // } catch (IOException e) { - // String message = e.getMessage(); - // int jsonStart = message.indexOf("{"); - // String jsonPart = message.substring(jsonStart); - // JSONObject json = new JSONObject(jsonPart); - // String errorCode = json.getJSONObject("error").getString("code"); - // String errorMessage = json.getJSONObject("error").getString("message"); - // assertEquals("500", errorCode); - // assertEquals("Cannot upload more than 4 attachments.", errorMessage); - // } - // String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!response.equals("Saved")) { - // fail("Could not save entity"); - // } - // response = api.deleteEntity(appUrl, entityName, createLinkEntity); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } else { - // fail("Could not edit entity"); - // } - // } + @Test + @Order(40) + void testCopyAttachmentsSuccessExistingEntity() throws IOException { + System.out.println("Test (40): Copy attachments from one entity to another existing entity"); + List attachments = new ArrayList<>(); + ClassLoader classLoader = getClass().getClassLoader(); + List files = new ArrayList<>(); + File file1 = new File(classLoader.getResource("sample.pdf").getFile()); + File file2 = new File(classLoader.getResource("sample1.pdf").getFile()); + File tempFile1 = new File(System.getProperty("java.io.tmpdir"), "sample_copy_existing_1.pdf"); + Files.copy(file1.toPath(), tempFile1.toPath(), StandardCopyOption.REPLACE_EXISTING); + File tempFile2 = new File(System.getProperty("java.io.tmpdir"), "sample_copy_existing_2.pdf"); + Files.copy(file2.toPath(), tempFile2.toPath(), StandardCopyOption.REPLACE_EXISTING); + files.add(tempFile1); + files.add(tempFile2); + Map postData = new HashMap<>(); + postData.put("up__ID", entityID7); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + String editResponse1 = + api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + String editResponse2 = + api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + if (editResponse1.equals("Entity in draft mode") + && editResponse2.equals("Entity in draft mode")) { + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, copyAttachmentSourceEntity, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + attachments.add(createResponse.get(1)); + } else { + fail("Could not create attachment"); + } + } + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + List> attachmentsMetadata = new ArrayList<>(); + Map fetchAttachmentMetadataResponse; + for (String attachment : attachments) { + try { + fetchAttachmentMetadataResponse = + api.fetchMetadata( + appUrl, entityName, facetName, copyAttachmentSourceEntity, attachment); + attachmentsMetadata.add(fetchAttachmentMetadataResponse); + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } - // @Test - // @Order(45) - // void testCreateLinkNoSDMRoles() throws IOException { - // System.out.println("Test (45): Create link fails due to no SDM roles assigned"); - // String createLinkEntityNoSDMRoles = - // apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!createLinkEntityNoSDMRoles.equals("Could not edit entity")) { - // String linkName = "sample27"; - // String linkUrl = "https://example.com"; - // try { - // apiNoRoles.createLink( - // appUrl, entityName, facetName, createLinkEntityNoSDMRoles, linkName, linkUrl); - // fail("Link got created without SDM roles"); - // } catch (IOException e) { - // String message = e.getMessage(); - // int jsonStart = message.indexOf("{"); - // String jsonPart = message.substring(jsonStart); - // JSONObject json = new JSONObject(jsonPart); - // String errorCode = json.getJSONObject("error").getString("code"); - // String errorMessage = json.getJSONObject("error").getString("message"); - // assertEquals("500", errorCode); - // assertEquals( - // "You do not have the required permissions to upload attachments. Please contact your - // administrator for access.", - // errorMessage); - // } - // String response = - // apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntityNoSDMRoles); - // if (!response.equals("Saved")) { - // fail("Could not save entity"); - // } - // response = api.deleteEntity(appUrl, entityName, createLinkEntityNoSDMRoles); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } else { - // fail("Could not edit entity"); - // } - // } + sourceObjectIds.clear(); + for (Map metadata : attachmentsMetadata) { + if (metadata.containsKey("objectId")) { + sourceObjectIds.add(metadata.get("objectId").toString()); + } else { + fail("Attachment metadata does not contain objectId"); + } + } - // @Test - // @Order(46) - // void testDeleteLink() throws IOException { - // System.out.println("Test (46): Delete link in entity"); - // List attachments = new ArrayList<>(); - // String createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!createLinkEntity.equals("Could not create entity")) { - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - // if (createLinkResponse.equals("Link created successfully")) { - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (saveEntityResponse.equals("Saved")) { - // attachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // String editEntityResponse = - // api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } - // String deleteLinkResponse = - // api.deleteAttachment( - // appUrl, entityName, facetName, createLinkEntity, attachments.get(0)); - // if (!deleteLinkResponse.equals("Deleted")) { - // fail("Could not delete created link"); - // } else { - // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } - // attachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // if (attachments.size() != 0) { - // fail("Link wasn't deleted"); - // } - // String response = api.deleteEntity(appUrl, entityName, createLinkEntity); - // if (!response.equals("Entity Deleted")) { - // fail("Could not delete entity"); - // } - // } - // } else { - // fail("Could not save entity"); - // } - // } else { - // fail("Could not create link"); - // } - // } else { - // fail("Could not create entity"); - // } - // } + if (sourceObjectIds.size() == 2) { + String copyResponse; + copyResponse = + api.copyAttachment( + appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); + if (copyResponse.equals("Attachments copied successfully")) { + String saveEntityResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + if (saveEntityResponse.equals("Saved")) { + List> fetchEntityMetadataResponse; + fetchEntityMetadataResponse = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); + targetAttachmentIds = + fetchEntityMetadataResponse.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + String readResponse; + if (targetAttachmentIds.size() == 4) { + for (String targetAttachmentId : targetAttachmentIds) { + readResponse = + api.readAttachment( + appUrl, + entityName, + facetName, + copyAttachmentTargetEntity, + targetAttachmentId); + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment"); + } + } + } + // api.deleteEntity(appUrl, entityName, copyAttachmentSourceEntity); + // api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntity); + } else { + fail("Could not save entity after copying attachments: " + saveEntityResponse); + } + } else { + fail("Could not copy attachments: " + copyResponse); + } + } else { + fail("Could not fetch objects Ids for all attachments"); + } + } else { + fail("Could not edit entities"); + } + } - // @Test - // @Order(47) - // void testRenameLinkSuccess() throws IOException { - // System.out.println("Test (47): Rename link in entity"); - // List attachments = new ArrayList<>(); + @Test + @Order(41) + void testCopyAttachmentsUnsuccessfulExistingEntity() throws IOException { + System.out.println( + "Test (41): Copy attachments from one entity to another existing entity - unsuccessful"); + String editResponse1 = + api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + String editResponse2 = + api.editEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + if (editResponse1.equals("Entity in draft mode") + && editResponse2.equals("Entity in draft mode")) { + sourceObjectIds.add("incorrectObjectId"); + if (sourceObjectIds.size() == 3) { + try { + api.copyAttachment( + appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); + fail("Copy attachments did not throw an error"); + } catch (IOException e) { + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentSourceEntity); + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntity); + api.deleteEntity(appUrl, entityName, copyAttachmentSourceEntity); + } + } else { + fail("Could not fetch objects Ids for all attachments"); + } + } else { + fail("Could not edit entities"); + } + } - // createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (createLinkEntity.equals("Could not create entity")) { - // fail("Could not create entity"); - // } + @Test + @Order(42) + void testCreateLinkSuccess() throws IOException { + System.out.println("Test (42): Create link in entity"); + List attachments = new ArrayList<>(); + createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!createLinkEntity.equals("Could not create entity")) { + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse1 = + api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + String createLinkResponse2 = + api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName + "1", linkUrl); + if (createLinkResponse1.equals("Link created successfully") + && createLinkResponse2.equals("Link created successfully")) { + String saveEntityResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (saveEntityResponse.equals("Saved")) { + attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + String openAttachmentResponse; + for (String attachment : attachments) { + openAttachmentResponse = + api.openAttachment(appUrl, entityName, facetName, createLinkEntity, attachment); + System.out.println("openAttachmentResponse: " + openAttachmentResponse); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open created link"); + } + } + } else { + fail("Could not save entity"); + } + } else { + fail("Could not create link"); + } + } else { + fail("Could not create entity"); + } + } - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link"); - // } + @Test + @Order(43) + void testCreateLinkDifferentEntity() throws IOException { + System.out.println("Test (43): Create link with same name in different entity"); + String createLinkDifferentEntity = + api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!createLinkDifferentEntity.equals("Could not edit entity")) { + String linkName = "sample"; + String linkUrl = "https://example.com"; + String createResponse = + api.createLink( + appUrl, entityName, facetName, createLinkDifferentEntity, linkName, linkUrl); + if (!createResponse.equals("Link created successfully")) { + fail("Could not create link in different entity with same name"); + } + String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkDifferentEntity); + if (!response.equals("Saved")) { + fail("Could not save entity"); + } + response = api.deleteEntity(appUrl, entityName, createLinkDifferentEntity); + if (!response.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } else { + fail("Could not edit entity"); + } + } - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + @Test + @Order(44) + void testCreateLinkFailure() throws IOException { + System.out.println("Test (44): Create link fails due to invalid URL and name"); + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Could not edit entity")) { + String linkName = "sample"; + String linkUrl = "example.com"; + try { + api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + fail("Create link did not throw an error for invalid url"); + } catch (IOException e) { + String message = e.getMessage(); + int jsonStart = message.indexOf("{"); + String jsonPart = message.substring(jsonStart); + JSONObject json = new JSONObject(jsonPart); + String errorCode = json.getJSONObject("error").getString("code"); + String errorMessage = json.getJSONObject("error").getString("message"); + assertEquals("400018", errorCode); + assertTrue( + errorMessage.equals("Enter a value that is within the expected pattern.") + || errorMessage.equals("Enter a value that matches the expected pattern."), + "Unexpected error message: " + errorMessage); + } + try { + api.createLink( + appUrl, entityName, facetName, createLinkEntity, linkName + "//", "https://" + linkUrl); + fail("Create link did not throw an error for invalid name"); + } catch (IOException e) { + String message = e.getMessage(); + int jsonStart = message.indexOf("{"); + String jsonPart = message.substring(jsonStart); + JSONObject json = new JSONObject(jsonPart); + String errorCode = json.getJSONObject("error").getString("code"); + String errorMessage = json.getJSONObject("error").getString("message"); + String expected = + "\"sample//\" contains unsupported characters (‘/’ or ‘\\’). Rename and try again."; + assertEquals("500", errorCode); + assertEquals( + expected.replaceAll("\\s+", " ").trim(), errorMessage.replaceAll("\\s+", " ").trim()); + } + try { + api.createLink(appUrl, entityName, facetName, createLinkEntity, "", ""); + fail("Create link did not throw an error for empty name and url"); + } catch (IOException e) { + String message = e.getMessage(); + int jsonStart = message.indexOf("{"); + String jsonPart = message.substring(jsonStart); + JSONObject json = new JSONObject(jsonPart); + String errorCode = json.getJSONObject("error").getString("code"); + String errorMessage = json.getJSONObject("error").getString("message"); + String expected = "Provide the missing value."; + assertEquals("409008", errorCode); + assertEquals(expected, errorMessage); + } + try { + api.createLink( + appUrl, entityName, facetName, createLinkEntity, linkName, "https://" + linkUrl); + fail("Create link did not throw an error for duplicate name"); + } catch (IOException e) { + String message = e.getMessage(); + int jsonStart = message.indexOf("{"); + String jsonPart = message.substring(jsonStart); + JSONObject json = new JSONObject(jsonPart); + String errorCode = json.getJSONObject("error").getString("code"); + String errorMessage = json.getJSONObject("error").getString("message"); + assertEquals("500", errorCode); + assertEquals( + "An object named \"sample\" already exists. Rename the object and try again.", + errorMessage); + } + try { + for (int i = 2; i < 5; i++) { + api.createLink( + appUrl, entityName, facetName, createLinkEntity, linkName + i, "https://" + linkUrl); + } + fail("More than 5 links were created in the same entity"); + } catch (IOException e) { + String message = e.getMessage(); + int jsonStart = message.indexOf("{"); + String jsonPart = message.substring(jsonStart); + JSONObject json = new JSONObject(jsonPart); + String errorCode = json.getJSONObject("error").getString("code"); + String errorMessage = json.getJSONObject("error").getString("message"); + assertEquals("500", errorCode); + assertEquals("Cannot upload more than 4 attachments.", errorMessage); + } + String response = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!response.equals("Saved")) { + fail("Could not save entity"); + } + response = api.deleteEntity(appUrl, entityName, createLinkEntity); + if (!response.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } else { + fail("Could not edit entity"); + } + } - // attachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + @Test + @Order(45) + void testCreateLinkNoSDMRoles() throws IOException { + System.out.println("Test (45): Create link fails due to no SDM roles assigned"); + String createLinkEntityNoSDMRoles = + apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!createLinkEntityNoSDMRoles.equals("Could not edit entity")) { + String linkName = "sample27"; + String linkUrl = "https://example.com"; + try { + apiNoRoles.createLink( + appUrl, entityName, facetName, createLinkEntityNoSDMRoles, linkName, linkUrl); + fail("Link got created without SDM roles"); + } catch (IOException e) { + String message = e.getMessage(); + int jsonStart = message.indexOf("{"); + String jsonPart = message.substring(jsonStart); + JSONObject json = new JSONObject(jsonPart); + String errorCode = json.getJSONObject("error").getString("code"); + String errorMessage = json.getJSONObject("error").getString("message"); + assertEquals("500", errorCode); + assertEquals( + "You do not have the required permissions to upload attachments. Please contact your administrator for access.", + errorMessage); + } + String response = + apiNoRoles.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntityNoSDMRoles); + if (!response.equals("Saved")) { + fail("Could not save entity"); + } + response = api.deleteEntity(appUrl, entityName, createLinkEntityNoSDMRoles); + if (!response.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } else { + fail("Could not edit entity"); + } + } - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + @Test + @Order(46) + void testDeleteLink() throws IOException { + System.out.println("Test (46): Delete link in entity"); + List attachments = new ArrayList<>(); + String createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!createLinkEntity.equals("Could not create entity")) { + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + if (createLinkResponse.equals("Link created successfully")) { + String saveEntityResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (saveEntityResponse.equals("Saved")) { + attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + String editEntityResponse = + api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } + String deleteLinkResponse = + api.deleteAttachment( + appUrl, entityName, facetName, createLinkEntity, attachments.get(0)); + if (!deleteLinkResponse.equals("Deleted")) { + fail("Could not delete created link"); + } else { + saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } + attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + if (attachments.size() != 0) { + fail("Link wasn't deleted"); + } + String response = api.deleteEntity(appUrl, entityName, createLinkEntity); + if (!response.equals("Entity Deleted")) { + fail("Could not delete entity"); + } + } + } else { + fail("Could not save entity"); + } + } else { + fail("Could not create link"); + } + } else { + fail("Could not create entity"); + } + } - // attachmentID9 = attachments.get(0); - // String renameLinkResponse = - // api.renameAttachment( - // appUrl, entityName, facetName, createLinkEntity, attachments.get(0), - // "sampleRenamed"); - // if (!renameLinkResponse.equals("Renamed")) fail("Could not Renamed created link"); + @Test + @Order(47) + void testRenameLinkSuccess() throws IOException { + System.out.println("Test (47): Rename link in entity"); + List attachments = new ArrayList<>(); - // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } - // } + createLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (createLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } - // @Test - // @Order(48) - // void testRenameLinkDuplicate() throws IOException { - // System.out.println("Test (48): Rename link in entity fails due to duplicate error"); - // List attachments = new ArrayList<>(); - - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link"); + } - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link"); - // } + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - // editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // attachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // .filter(item -> !attachmentID9.equals(item.get("ID"))) // skip unwanted filename - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // attachmentID10 = attachments.get(0); - // api.renameAttachment( - // appUrl, entityName, facetName, createLinkEntity, attachments.get(0), "sampleRenamed"); - - // String saveError = - // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // String expectedWarning = - // "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"sampleRenamed\\\" already - // exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\"}}"; - // ObjectMapper mapper = new ObjectMapper(); - // assertEquals(mapper.readTree(expectedWarning), mapper.readTree(saveError)); - - // String deleteEntityResponse = api.deleteEntityDraft(appUrl, entityName, createLinkEntity); - // if (!deleteEntityResponse.equals("Entity Draft Deleted")) { - // fail("Entity draft not deleted"); - // } - // } + attachmentID9 = attachments.get(0); + String renameLinkResponse = + api.renameAttachment( + appUrl, entityName, facetName, createLinkEntity, attachments.get(0), "sampleRenamed"); + if (!renameLinkResponse.equals("Renamed")) fail("Could not Renamed created link"); - // @Test - // @Order(49) - // void testRenameLinkUnsupportedCharacters() throws IOException { - // System.out.println( - // "Test (49): Rename link in entity fails due to unsupported characters in name"); - // List attachments = new ArrayList<>(); + saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } + } - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + @Test + @Order(48) + void testRenameLinkDuplicate() throws IOException { + System.out.println("Test (48): Rename link in entity fails due to duplicate error"); + List attachments = new ArrayList<>(); + + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // String linkName = "sample2"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link"); - // } + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link"); + } - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // createLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // attachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() - // // .filter(item -> "sample2".equals(item.get("filename"))) // skip unwanted filename - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // System.out.println("attachments: " + attachments); - - // editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } + editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // api.renameAttachment( - // appUrl, entityName, facetName, createLinkEntity, attachments.get(0), "sampleRenamed//"); - // String warning = - // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); - // String expectedWarning = - // "{\"error\":{\"code\":\"400\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported - // characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: - // IntegrationTestEntity\"}}"; - // ObjectMapper mapper = new ObjectMapper(); - // assertEquals(mapper.readTree(expectedWarning), mapper.readTree(warning)); - - // String deleteEntityResponse = api.deleteEntity(appUrl, entityName, createLinkEntity); - // if (!deleteEntityResponse.equals("Entity Deleted")) { - // fail("Entity draft not deleted"); - // } - // } + attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + .filter(item -> !attachmentID9.equals(item.get("ID"))) // skip unwanted filename + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + attachmentID10 = attachments.get(0); + api.renameAttachment( + appUrl, entityName, facetName, createLinkEntity, attachments.get(0), "sampleRenamed"); + + String saveError = + saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + String expectedWarning = + "{\"error\":{\"code\":\"400\",\"message\":\"An object named \\\"sampleRenamed\\\" already exists. Rename the object and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; + ObjectMapper mapper = new ObjectMapper(); + assertEquals(mapper.readTree(expectedWarning), mapper.readTree(saveError)); + + String deleteEntityResponse = api.deleteEntityDraft(appUrl, entityName, createLinkEntity); + if (!deleteEntityResponse.equals("Entity Draft Deleted")) { + fail("Entity draft not deleted"); + } + } - // @Test - // @Order(50) - // void testEditLinkSuccess() throws IOException { - // System.out.println("Test (50): Edit existing link in entity"); + @Test + @Order(49) + void testRenameLinkUnsupportedCharacters() throws IOException { + System.out.println( + "Test (49): Rename link in entity fails due to unsupported characters in name"); + List attachments = new ArrayList<>(); - // List attachments = new ArrayList<>(); - // editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (editLinkEntity.equals("Could not create entity")) { - // fail("Could not create entity"); - // } - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link"); - // } + String linkName = "sample2"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, createLinkEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link"); + } - // String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } - // attachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - - // if (attachments.isEmpty()) { - // fail("Could not edit link"); - // } - // String linkId = attachments.get(0); - // String updatedUrl = "https://editedexample.com"; - // String editLinkResponse = - // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - // if (!editLinkResponse.equals("Link edited successfully")) { - // fail("Could not edit link"); - // } - // saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!saveEntityResponse.equals("Saved")) { - // fail("Could not save entity"); - // } - // String openAttachmentResponse; - // for (String attachment : attachments) { - // openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, editLinkEntity, attachment); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open created link"); - // } - // } - // } + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } - // @Test - // @Order(51) - // void testEditLinkFailureInvalidURL() throws IOException { - // System.out.println("Test (51): Edit existing link with invalid url"); - // Boolean testStatus = false; - // List attachments = new ArrayList<>(); - - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } - // attachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - - // if (attachments.isEmpty()) { - // fail("Could not edit link"); - // } - // String linkId = attachments.get(0); - // String updatedUrl = "https://editedexample"; - // try { - - // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - // fail("Create link did not throw an error for invalid url"); - // } catch (IOException e) { - // String message = e.getMessage(); - // int jsonStart = message.indexOf("{"); - // String jsonPart = message.substring(jsonStart); - // JSONObject json = new JSONObject(jsonPart); - // String errorCode = json.getJSONObject("error").getString("code"); - // String errorMessage = json.getJSONObject("error").getString("message"); - // assertEquals("400018", errorCode); - // assertTrue( - // errorMessage.equals("Enter a value that is within the expected pattern.") - // || errorMessage.equals("Enter a value that matches the expected pattern."), - // "Unexpected error message: " + errorMessage); - - // testStatus = true; - // } - // api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!testStatus) { - // fail("Could not edit link with an invalid URL"); - // } - // } + attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, createLinkEntity).stream() + // .filter(item -> "sample2".equals(item.get("filename"))) // skip unwanted filename + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + System.out.println("attachments: " + attachments); + + editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } - // @Test - // @Order(52) - // void testEditLinkFailureEmptyURL() throws IOException { - // System.out.println("Test (52): Edit existing link with an empty url"); - // Boolean testStatus = false; - // List attachments = new ArrayList<>(); - - // String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } - // attachments = - // api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - - // if (attachments.isEmpty()) { - // fail("Could not edit link"); - // } - // String linkId = attachments.get(0); - // String updatedUrl = ""; - // try { - // api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - // fail("edit link did not throw an error for empty url"); - // } catch (IOException e) { - // String message = e.getMessage(); - // int jsonStart = message.indexOf("{"); - // String jsonPart = message.substring(jsonStart); - // JSONObject json = new JSONObject(jsonPart); - // String errorCode = json.getJSONObject("error").getString("code"); - // String errorMessage = json.getJSONObject("error").getString("message"); - // String expected = "Provide the missing value."; - // assertEquals("409008", errorCode); - // assertEquals(expected, errorMessage); - // testStatus = true; - // } - // api.deleteEntityDraft(appUrl, entityName, editLinkEntity); - // if (!testStatus) { - // fail("Could not edit link with an empty URL"); - // } - // } + api.renameAttachment( + appUrl, entityName, facetName, createLinkEntity, attachments.get(0), "sampleRenamed//"); + String warning = + saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, createLinkEntity); + String expectedWarning = + "{\"error\":{\"code\":\"400\",\"message\":\"\\\"sampleRenamed//\\\" contains unsupported characters (‘/’ or ‘\\\\’). Rename and try again.\\n\\nTable: attachments\\nPage: IntegrationTestEntity\"}}"; + ObjectMapper mapper = new ObjectMapper(); + assertEquals(mapper.readTree(expectedWarning), mapper.readTree(warning)); + + String deleteEntityResponse = api.deleteEntity(appUrl, entityName, createLinkEntity); + if (!deleteEntityResponse.equals("Entity Deleted")) { + fail("Entity draft not deleted"); + } + } - // @Test - // @Order(53) - // void testEditLinkNoSDMRoles() throws IOException { - // System.out.println("Test (53): Edit link fails due to no SDM roles assigned"); + @Test + @Order(50) + void testEditLinkSuccess() throws IOException { + System.out.println("Test (50): Edit existing link in entity"); - // Boolean testStatus = false; - // List attachments = new ArrayList<>(); + List attachments = new ArrayList<>(); + editLinkEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (editLinkEntity.equals("Could not create entity")) { + fail("Could not create entity"); + } + String linkName = "sample"; + String linkUrl = "https://www.example.com"; - // String editEntityResponse = - // apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); - // if (!editEntityResponse.equals("Entity in draft mode")) { - // fail("Could not edit entity"); - // } - // attachments = - // apiNoRoles.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - - // if (attachments.isEmpty()) { - // fail("Could not edit link"); - // } - // String linkId = attachments.get(0); - // String updatedUrl = "https://www.example1.com"; - // try { - // apiNoRoles.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); - // fail("Link got edited without SDM roles in facet: \" + facetName"); - // } catch (IOException e) { - // String message = e.getMessage(); - // int jsonStart = message.indexOf("{"); - // String jsonPart = message.substring(jsonStart); - // JSONObject json = new JSONObject(jsonPart); - // String errorCode = json.getJSONObject("error").getString("code"); - // String errorMessage = json.getJSONObject("error").getString("message"); - // assertEquals("500", errorCode); - // assertEquals( - // "You do not have the required permissions to update attachments. Kindly contact the - // admin", - // errorMessage); - // testStatus = true; - // } - // apiNoRoles.deleteEntity(appUrl, entityName, createLinkEntity); - // if (!testStatus) { - // fail("Link got edited without SDM roles"); - // } - // api.deleteEntity(appUrl, entityName, editLinkEntity); - // } + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, editLinkEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link"); + } - // @Test - // @Order(54) - // void testCopyLinkSuccessNewEntity() throws IOException { - // System.out.println("Test (54): Copy link from one entity to another new entity"); - // List> attachmentsMetadata = new ArrayList<>(); + String saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } + attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + if (attachments.isEmpty()) { + fail("Could not edit link"); + } + String linkId = attachments.get(0); + String updatedUrl = "https://editedexample.com"; + String editLinkResponse = + api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + if (!editLinkResponse.equals("Link edited successfully")) { + fail("Could not edit link"); + } + saveEntityResponse = api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!saveEntityResponse.equals("Saved")) { + fail("Could not save entity"); + } + String openAttachmentResponse; + for (String attachment : attachments) { + openAttachmentResponse = + api.openAttachment(appUrl, entityName, facetName, editLinkEntity, attachment); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open created link"); + } + } + } - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + @Test + @Order(51) + void testEditLinkFailureInvalidURL() throws IOException { + System.out.println("Test (51): Edit existing link with invalid url"); + Boolean testStatus = false; + List attachments = new ArrayList<>(); - // if (copyLinkSourceEntity.equals("Could not create entity") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not create source or target entity"); - // } + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } + attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + if (attachments.isEmpty()) { + fail("Could not edit link"); + } + String linkId = attachments.get(0); + String updatedUrl = "https://editedexample"; + try { + + api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + fail("Create link did not throw an error for invalid url"); + } catch (IOException e) { + String message = e.getMessage(); + int jsonStart = message.indexOf("{"); + String jsonPart = message.substring(jsonStart); + JSONObject json = new JSONObject(jsonPart); + String errorCode = json.getJSONObject("error").getString("code"); + String errorMessage = json.getJSONObject("error").getString("message"); + assertEquals("400018", errorCode); + assertTrue( + errorMessage.equals("Enter a value that is within the expected pattern.") + || errorMessage.equals("Enter a value that matches the expected pattern."), + "Unexpected error message: " + errorMessage); - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in source entity"); - // } + testStatus = true; + } + api.saveEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!testStatus) { + fail("Could not edit link with an invalid URL"); + } + } - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + @Test + @Order(52) + void testEditLinkFailureEmptyURL() throws IOException { + System.out.println("Test (52): Edit existing link with an empty url"); + Boolean testStatus = false; + List attachments = new ArrayList<>(); - // List sourceObjectIds = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + String editEntityResponse = api.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } + attachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + if (attachments.isEmpty()) { + fail("Could not edit link"); + } + String linkId = attachments.get(0); + String updatedUrl = ""; + try { + api.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + fail("edit link did not throw an error for empty url"); + } catch (IOException e) { + String message = e.getMessage(); + int jsonStart = message.indexOf("{"); + String jsonPart = message.substring(jsonStart); + JSONObject json = new JSONObject(jsonPart); + String errorCode = json.getJSONObject("error").getString("code"); + String errorMessage = json.getJSONObject("error").getString("message"); + String expected = "Provide the missing value."; + assertEquals("409008", errorCode); + assertEquals(expected, errorMessage); + testStatus = true; + } + api.deleteEntityDraft(appUrl, entityName, editLinkEntity); + if (!testStatus) { + fail("Could not edit link with an empty URL"); + } + } - // if (sourceObjectIds.isEmpty()) { - // fail("Could not fetch object Id for link"); - // } + @Test + @Order(53) + void testEditLinkNoSDMRoles() throws IOException { + System.out.println("Test (53): Edit link fails due to no SDM roles assigned"); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy link: " + copyResponse); - // } + Boolean testStatus = false; + List attachments = new ArrayList<>(); - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target entity after copying link"); - // } + String editEntityResponse = + apiNoRoles.editEntityDraft(appUrl, entityName, srvpath, editLinkEntity); + if (!editEntityResponse.equals("Entity in draft mode")) { + fail("Could not edit entity"); + } + attachments = + apiNoRoles.fetchEntityMetadata(appUrl, entityName, facetName, editLinkEntity).stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + + if (attachments.isEmpty()) { + fail("Could not edit link"); + } + String linkId = attachments.get(0); + String updatedUrl = "https://www.example1.com"; + try { + apiNoRoles.editLink(appUrl, entityName, facetName, editLinkEntity, linkId, updatedUrl); + fail("Link got edited without SDM roles in facet: \" + facetName"); + } catch (IOException e) { + String message = e.getMessage(); + int jsonStart = message.indexOf("{"); + String jsonPart = message.substring(jsonStart); + JSONObject json = new JSONObject(jsonPart); + String errorCode = json.getJSONObject("error").getString("code"); + String errorMessage = json.getJSONObject("error").getString("message"); + assertEquals("500", errorCode); + assertEquals( + "You do not have the required permissions to update attachments. Kindly contact the admin", + errorMessage); + testStatus = true; + } + apiNoRoles.deleteEntity(appUrl, entityName, createLinkEntity); + if (!testStatus) { + fail("Link got edited without SDM roles"); + } + api.deleteEntity(appUrl, entityName, editLinkEntity); + } - // attachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - // Map copiedAttachment = attachmentsMetadata.get(0); - // String receivedType = (String) copiedAttachment.get("type"); - // String receivedUrl = (String) copiedAttachment.get("linkUrl"); - - // String expectedType = "sap-icon://internet-browser"; - // assertTrue( - // expectedType.equalsIgnoreCase(receivedType), - // "Attachment type mismatch. Expected '" - // + expectedType - // + "' but got '" - // + receivedType - // + "'."); - - // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); - - // System.out.println("Attachment type and URL validated successfully."); - - // String attachmentId = (String) copiedAttachment.get("ID"); - // String openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open the attachment"); - // } + @Test + @Order(54) + void testCopyLinkSuccessNewEntity() throws IOException { + System.out.println("Test (54): Copy link from one entity to another new entity"); + List> attachmentsMetadata = new ArrayList<>(); - // String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - // if (!deleteSourceResponse.equals("Entity Deleted") - // || !deleteTargetResponse.equals("Entity Deleted")) { - // fail("could not delete source or target entity"); - // } - // } + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // @Test - // @Order(55) - // void testCopyLinkUnsuccessfulNewEntity() throws IOException { - // System.out.println( - // "Test (55): Copy invalid type of link from one entity to another new entity"); + if (copyLinkSourceEntity.equals("Could not create entity") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not create source or target entity"); + } - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in source entity"); + } - // if (copyLinkSourceEntity.equals("Could not create entity") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not create source or target entity"); - // } + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // List invalidObjectIds = Collections.singletonList("incorrectObjectId"); + List sourceObjectIds = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - // try { - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, invalidObjectIds); - // fail("Copy attachments did not throw error for invalid ID"); - // } catch (IOException e) { - // System.out.println("Caught expected error: " + e.getMessage()); - // } + if (sourceObjectIds.isEmpty()) { + fail("Could not fetch object Id for link"); + } - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target entity after unsuccessful copy"); - // } + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy link: " + copyResponse); + } - // String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // if (!deleteSourceResponse.equals("Entity Deleted")) { - // fail("Could not delete source entity"); - // } - // } + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveResponse.equals("Saved")) { + fail("Could not save target entity after copying link"); + } - // @Test - // @Order(56) - // void testCopyLinkFromNewEntityToExistingEntity() throws IOException { - // System.out.println("Test (56): Copy link from a new entity to an existing target entity"); - // List> attachmentsMetadata = new ArrayList<>(); - - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyLinkSourceEntity.equals("Could not create entity")) { - // fail("Could not create new source entity"); - // } + attachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + Map copiedAttachment = attachmentsMetadata.get(0); + String receivedType = (String) copiedAttachment.get("type"); + String receivedUrl = (String) copiedAttachment.get("linkUrl"); + + String expectedType = "sap-icon://internet-browser"; + assertTrue( + expectedType.equalsIgnoreCase(receivedType), + "Attachment type mismatch. Expected '" + + expectedType + + "' but got '" + + receivedType + + "'."); + + assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); + + System.out.println("Attachment type and URL validated successfully."); + + String attachmentId = (String) copiedAttachment.get("ID"); + String openAttachmentResponse = + api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open the attachment"); + } - // String linkName = "Sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in new source entity"); - // } + String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + if (!deleteSourceResponse.equals("Entity Deleted") + || !deleteTargetResponse.equals("Entity Deleted")) { + fail("could not delete source or target entity"); + } + } - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save new source entity"); - // } + @Test + @Order(55) + void testCopyLinkUnsuccessfulNewEntity() throws IOException { + System.out.println( + "Test (55): Copy invalid type of link from one entity to another new entity"); - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft"); - // } + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // List sourceObjectIds = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + if (copyLinkSourceEntity.equals("Could not create entity") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not create source or target entity"); + } - // if (sourceObjectIds.isEmpty()) { - // fail("Could not fetch objectId from new source entity"); - // } + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + List invalidObjectIds = Collections.singletonList("incorrectObjectId"); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy link from new source entity to existing target entity: " + - // copyResponse); - // } + try { + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, invalidObjectIds); + fail("Copy attachments did not throw error for invalid ID"); + } catch (IOException e) { + System.out.println("Caught expected error: " + e.getMessage()); + } - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveResponse.equals("Saved")) { + fail("Could not save target entity after unsuccessful copy"); + } - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity after copying link"); - // } + String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + if (!deleteSourceResponse.equals("Entity Deleted")) { + fail("Could not delete source entity"); + } + } - // attachmentsMetadata = - // api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); - // Map copiedAttachment = attachmentsMetadata.get(0); - // String receivedType = (String) copiedAttachment.get("type"); - // String receivedUrl = (String) copiedAttachment.get("linkUrl"); + @Test + @Order(56) + void testCopyLinkFromNewEntityToExistingEntity() throws IOException { + System.out.println("Test (56): Copy link from a new entity to an existing target entity"); + List> attachmentsMetadata = new ArrayList<>(); + + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyLinkSourceEntity.equals("Could not create entity")) { + fail("Could not create new source entity"); + } - // String expectedType = "sap-icon://internet-browser"; - // assertTrue( - // expectedType.equalsIgnoreCase(receivedType), - // "Attachment type mismatch. Expected '" - // + expectedType - // + "' but got '" - // + receivedType - // + "'."); + String linkName = "Sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in new source entity"); + } - // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save new source entity"); + } - // System.out.println("Attachment type and URL validated successfully."); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } - // String attachmentId = (String) copiedAttachment.get("ID"); - // assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); + List sourceObjectIds = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - // String openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open the attachment"); - // } + if (sourceObjectIds.isEmpty()) { + fail("Could not fetch objectId from new source entity"); + } - // String deleteResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // if (!deleteResponse.equals("Entity Deleted")) { - // fail("Could not delete new source entity"); - // } - // } + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy link from new source entity to existing target entity: " + copyResponse); + } - // @Test - // @Order(57) - // void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { - // System.out.println( - // "Test (57): Copy invalid type of link from new entity to existing target entity"); + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (copyLinkSourceEntity.equals("Could not create entity")) { - // fail("Could not create new source entity"); - // } + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity after copying link"); + } - // String linkName = "Sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in new source entity"); - // } + attachmentsMetadata = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyLinkTargetEntity); + Map copiedAttachment = attachmentsMetadata.get(0); + String receivedType = (String) copiedAttachment.get("type"); + String receivedUrl = (String) copiedAttachment.get("linkUrl"); - // String saveSourceResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save new source entity"); - // } + String expectedType = "sap-icon://internet-browser"; + assertTrue( + expectedType.equalsIgnoreCase(receivedType), + "Attachment type mismatch. Expected '" + + expectedType + + "' but got '" + + receivedType + + "'."); - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!editResponse.equals("Entity in draft mode")) { - // fail("Could not edit target entity draft"); - // } + assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); - // List invalidObjectIds = Collections.singletonList("invalidObjectId123"); + System.out.println("Attachment type and URL validated successfully."); - // try { - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, invalidObjectIds); - // fail("Copy did not throw error for invalid link ID"); - // } catch (IOException e) { - // System.out.println("Caught expected error while copying invalid link: " + e.getMessage()); - // } + String attachmentId = (String) copiedAttachment.get("ID"); + assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); - // // No need to wait for upload completion as copy failed, but ensure clean state - // String saveTargetResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity after unsuccessful copy"); - // } + String openAttachmentResponse = + api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open the attachment"); + } - // String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); - // String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - // if (!deleteSourceResponse.equals("Entity Deleted") - // || !deleteTargetResponse.equals("Entity Deleted")) { - // fail("Could not delete new source entity or target entity"); - // } - // } + String deleteResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + if (!deleteResponse.equals("Entity Deleted")) { + fail("Could not delete new source entity"); + } + } - // @Test - // @Order(58) - // void testCopyLinkSuccessNewEntityDraft() throws IOException { - // System.out.println("Test (58): Copy link from one entity to another new entity draft mode"); + @Test + @Order(57) + void testCopyInvalidLinkFromNewEntityToExistingEntity() throws IOException { + System.out.println( + "Test (57): Copy invalid type of link from new entity to existing target entity"); - // copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (copyLinkSourceEntity.equals("Could not create entity")) { + fail("Could not create new source entity"); + } - // if (copyLinkSourceEntity.equals("Could not create entity") - // || copyLinkTargetEntity.equals("Could not create entity")) { - // fail("Could not create source or target entity"); - // } + String linkName = "Sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in new source entity"); + } - // String linkName = "sample"; - // String linkUrl = "https://www.example.com"; - // String createLinkResponse = - // api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); - // if (!createLinkResponse.equals("Link created successfully")) { - // fail("Could not create link in source entity"); - // } + String saveSourceResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save new source entity"); + } - // List sourceObjectIds = - // api.fetchEntityMetadataDraft(appUrl, entityName, facetName, - // copyLinkSourceEntity).stream() - // .map(item -> (String) item.get("objectId")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!editResponse.equals("Entity in draft mode")) { + fail("Could not edit target entity draft"); + } - // if (sourceObjectIds.isEmpty()) { - // fail("Could not fetch object Id for link"); - // } + List invalidObjectIds = Collections.singletonList("invalidObjectId123"); - // String copyResponse = - // api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); - // if (!copyResponse.equals("Attachments copied successfully")) { - // fail("Could not copy link: " + copyResponse); - // } + try { + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, invalidObjectIds); + fail("Copy did not throw error for invalid link ID"); + } catch (IOException e) { + System.out.println("Caught expected error while copying invalid link: " + e.getMessage()); + } - // List> attachmentsMetadata = new ArrayList<>(); - // attachmentsMetadata = - // api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyLinkTargetEntity); - // Map copiedAttachment = attachmentsMetadata.get(0); - // String receivedType = (String) copiedAttachment.get("type"); - // String receivedUrl = (String) copiedAttachment.get("linkUrl"); - - // String expectedType = "sap-icon://internet-browser"; - // assertTrue( - // expectedType.equalsIgnoreCase(receivedType), - // "Attachment type mismatch. Expected '" - // + expectedType - // + "' but got '" - // + receivedType - // + "'."); - - // assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); - - // System.out.println("Attachment type and URL validated successfully."); - - // String attachmentId = (String) copiedAttachment.get("ID"); - // assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); - - // String openAttachmentResponse = - // api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); - // if (!openAttachmentResponse.equals("Attachment opened successfully")) { - // fail("Could not open the attachment"); - // } + // No need to wait for upload completion as copy failed, but ensure clean state + String saveTargetResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity after unsuccessful copy"); + } - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); - // if (!saveResponse.equals("Saved")) { - // fail("Could not save target entity after copying link"); - // } - // api.deleteEntityDraft(appUrl, entityName, copyLinkSourceEntity); - // api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); - // } + String deleteSourceResponse = api.deleteEntity(appUrl, entityName, copyLinkSourceEntity); + String deleteTargetResponse = api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + if (!deleteSourceResponse.equals("Entity Deleted") + || !deleteTargetResponse.equals("Entity Deleted")) { + fail("Could not delete new source entity or target entity"); + } + } - // @Test - // @Order(59) - // void testCopyAttachmentsSuccessNewEntityDraft() throws IOException { - // System.out.println( - // "Test (59): Copy attachments from one entity to another new entity draft mode"); - // List attachments = new ArrayList<>(); - // copyAttachmentSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // copyAttachmentTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (!copyAttachmentSourceEntity.equals("Could not create entity") - // && !copyAttachmentTargetEntity.equals("Could not create entity")) { - // ClassLoader classLoader = getClass().getClassLoader(); - // List files = new ArrayList<>(); - // files.add(new File(classLoader.getResource("sample.pdf").getFile())); - // files.add(new File(classLoader.getResource("sample1.pdf").getFile())); - // Map postData = new HashMap<>(); - // postData.put("up__ID", entityID7); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); - - // sourceObjectIds.clear(); - - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, copyAttachmentSourceEntity, srvpath, postData, - // file); - // if (createResponse.get(0).equals("Attachment created")) { - // attachments.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment"); - // } - // } + @Test + @Order(58) + void testCopyLinkSuccessNewEntityDraft() throws IOException { + System.out.println("Test (58): Copy link from one entity to another new entity draft mode"); - // List> attachmentsMetadata = new ArrayList<>(); - // Map fetchAttachmentMetadataResponse; - // for (String attachment : attachments) { - // try { - // fetchAttachmentMetadataResponse = - // api.fetchMetadataDraft( - // appUrl, entityName, facetName, copyAttachmentSourceEntity, attachment); - // attachmentsMetadata.add(fetchAttachmentMetadataResponse); - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } - // for (Map metadata : attachmentsMetadata) { - // if (metadata.containsKey("objectId")) { - // sourceObjectIds.add(metadata.get("objectId").toString()); - // } else { - // fail("Attachment metadata does not contain objectId"); - // } - // } + copyLinkSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyLinkTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (sourceObjectIds.size() == 2) { - // String copyResponse; - // copyResponse = - // api.copyAttachment( - // appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); - // if (copyResponse.equals("Attachments copied successfully")) { - // String saveEntityResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); - // if (saveEntityResponse.equals("Saved")) { - // List> fetchEntityMetadataResponse; - // fetchEntityMetadataResponse = - // api.fetchEntityMetadata(appUrl, entityName, facetName, - // copyAttachmentTargetEntity); - // targetAttachmentIds = - // fetchEntityMetadataResponse.stream() - // .map(item -> (String) item.get("ID")) - // .filter(Objects::nonNull) - // .collect(Collectors.toList()); - // String readResponse; - // for (String targetAttachmentId : targetAttachmentIds) { - // readResponse = - // api.readAttachment( - // appUrl, - // entityName, - // facetName, - // copyAttachmentTargetEntity, - // targetAttachmentId); - // if (!readResponse.equals("OK")) { - // fail("Could not read copied attachment"); - // } - // } - // } else { - // fail("Could not save entity after copying attachments: " + saveEntityResponse); - // } - // } else { - // fail("Could not copy attachments: " + copyResponse); - // } - // } else { - // fail("Could not fetch objects Ids for all attachments"); - // } - // } else { - // fail("Could not create entities"); - // } - // api.deleteEntityDraft(appUrl, entityName, copyAttachmentSourceEntity); - // api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntity); - // } + if (copyLinkSourceEntity.equals("Could not create entity") + || copyLinkTargetEntity.equals("Could not create entity")) { + fail("Could not create source or target entity"); + } - // @Test - // @Order(60) - // void testViewChangelogForNewlyCreatedAttachment() throws IOException { - // System.out.println("Test (60): View changelog for newly created attachment"); + String linkName = "sample"; + String linkUrl = "https://www.example.com"; + String createLinkResponse = + api.createLink(appUrl, entityName, facetName, copyLinkSourceEntity, linkName, linkUrl); + if (!createLinkResponse.equals("Link created successfully")) { + fail("Could not create link in source entity"); + } - // // Create a new entity for changelog test - // changelogEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // assertNotNull(changelogEntityID, "Failed to create changelog test entity"); - // assertNotEquals("Could not create entity", changelogEntityID); + List sourceObjectIds = + api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyLinkSourceEntity).stream() + .map(item -> (String) item.get("objectId")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); - // // Prepare a sample file to upload - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.txt").getFile()); - // assertTrue(file.exists(), "Sample file should exist"); + if (sourceObjectIds.isEmpty()) { + fail("Could not fetch object Id for link"); + } - // // Create attachment - // Map postData = new HashMap<>(); - // postData.put("up__ID", changelogEntityID); - // postData.put("mimeType", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + String copyResponse = + api.copyAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, sourceObjectIds); + if (!copyResponse.equals("Attachments copied successfully")) { + fail("Could not copy link: " + copyResponse); + } - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, changelogEntityID, srvpath, postData, file); - - // assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - // String status = createResponse.get(0); - // changelogAttachmentID = createResponse.get(1); - - // assertEquals("Attachment created", status, "Attachment should be created successfully"); - // assertNotNull(changelogAttachmentID, "Attachment ID should not be null"); - // assertNotEquals("", changelogAttachmentID, "Attachment ID should not be empty"); - - // // Fetch changelog for the newly created attachment - // Map changelogResponse = - // api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, - // changelogAttachmentID); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - - // // Verify changelog structure - // assertEquals(false, changelogResponse.get("hasMoreItems"), "hasMoreItems should be false"); - // assertEquals( - // "sample.txt", changelogResponse.get("filename"), "Filename should match uploaded file"); - // assertNotNull(changelogResponse.get("objectId"), "ObjectId should not be null"); - // assertEquals(1, changelogResponse.get("numItems"), "Should have 1 changelog entry"); - - // // Verify the changelog entry - // @SuppressWarnings("unchecked") - // List> changeLogs = - // (List>) changelogResponse.get("changeLogs"); - // assertEquals(1, changeLogs.size(), "Should have exactly 1 changelog entry"); - - // Map logEntry = changeLogs.get(0); - // assertEquals("created", logEntry.get("operation"), "Operation should be 'created'"); - // assertNotNull(logEntry.get("time"), "Time should not be null"); - // assertNotNull(logEntry.get("user"), "User should not be null"); - // assertFalse( - // logEntry.containsKey("changeDetail"), "Created operation should not have changeDetail"); - // } + List> attachmentsMetadata = new ArrayList<>(); + attachmentsMetadata = + api.fetchEntityMetadataDraft(appUrl, entityName, facetName, copyLinkTargetEntity); + Map copiedAttachment = attachmentsMetadata.get(0); + String receivedType = (String) copiedAttachment.get("type"); + String receivedUrl = (String) copiedAttachment.get("linkUrl"); + + String expectedType = "sap-icon://internet-browser"; + assertTrue( + expectedType.equalsIgnoreCase(receivedType), + "Attachment type mismatch. Expected '" + + expectedType + + "' but got '" + + receivedType + + "'."); + + assertEquals(linkUrl, receivedUrl, "Attachment URL mismatch."); + + System.out.println("Attachment type and URL validated successfully."); + + String attachmentId = (String) copiedAttachment.get("ID"); + assertNotNull(attachmentId, "Could not find 'ID' in the copied attachment metadata."); + + String openAttachmentResponse = + api.openAttachment(appUrl, entityName, facetName, copyLinkTargetEntity, attachmentId); + if (!openAttachmentResponse.equals("Attachment opened successfully")) { + fail("Could not open the attachment"); + } - // @Test - // @Order(61) - // void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { - // System.out.println( - // "Test (61): Modify note field and custom property, then verify changelog shows created + - // 3 updated entries"); - - // // Update attachment with notes field (entity is already in draft mode from test 60) - // String notesValue = "Test note for changelog verification"; - // MediaType mediaType = MediaType.parse("application/json"); - // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - // RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); - - // String updateNotesResponse = - // api.updateSecondaryProperty( - // appUrl, - // entityName, - // facetName, - // changelogEntityID, - // changelogAttachmentID, - // updateNotesBody); - // assertEquals("Updated", updateNotesResponse, "Should successfully update notes field"); - - // // Update attachment with custom property - // Integer customProperty2Value = 12345; - // RequestBody bodyInt = - // RequestBody.create( - // "{\"customProperty2\": " + customProperty2Value + "}", - // MediaType.parse("application/json")); - // String updateCustomPropertyResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID, bodyInt); - // assertEquals( - // "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); - - // // Save the entity - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - // assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - - // // Edit entity again to fetch changelog - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - - // // Fetch changelog after modifications - // Map changelogResponse = - // api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, - // changelogAttachmentID); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - - // // Verify changelog content - should have 1 created + 3 updated (note, customProperty2, and - // // internal update) - // assertEquals(false, changelogResponse.get("hasMoreItems"), "hasMoreItems should be false"); - // assertEquals( - // 4, - // changelogResponse.get("numItems"), - // "Should have 4 changelog entries (1 created + 3 updated)"); - - // @SuppressWarnings("unchecked") - // List> changeLogs = - // (List>) changelogResponse.get("changeLogs"); - // assertEquals(4, changeLogs.size(), "Should have exactly 4 changelog entries"); - - // // Verify first entry is 'created' - // Map createdEntry = changeLogs.get(0); - // assertEquals( - // "created", createdEntry.get("operation"), "First entry should be 'created' operation"); - - // // Verify remaining entries are 'updated' - // long updatedCount = - // changeLogs.stream().filter(log -> "updated".equals(log.get("operation"))).count(); - // assertEquals(3, updatedCount, "Should have 3 'updated' operations"); - - // // Verify that changeDetail exists in updated entries for note field - // boolean hasNoteUpdate = - // changeLogs.stream() - // .filter(log -> "updated".equals(log.get("operation"))) - // .anyMatch( - // log -> { - // @SuppressWarnings("unchecked") - // Map changeDetail = (Map) - // log.get("changeDetail"); - // return changeDetail != null - // && "cmis:description".equals(changeDetail.get("field")); - // }); - // assertTrue(hasNoteUpdate, "Should have an update entry for note field (cmis:description)"); - // assertTrue(hasNoteUpdate, "Should have an update entry for note field (cmis:description)"); - - // // Save the entity so test 62 can edit it - // String saveResponseFinal = api.saveEntityDraft(appUrl, entityName, srvpath, - // changelogEntityID); - // assertEquals("Saved", saveResponseFinal, "Entity should be saved successfully"); - // } + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, copyLinkTargetEntity); + if (!saveResponse.equals("Saved")) { + fail("Could not save target entity after copying link"); + } + api.deleteEntityDraft(appUrl, entityName, copyLinkSourceEntity); + api.deleteEntity(appUrl, entityName, copyLinkTargetEntity); + } - // @Test - // @Order(62) - // void testChangelogAfterRenamingAttachment() throws IOException { - // System.out.println( - // "Test (62): Rename attachment and verify changelog increases with rename entry"); - - // // Edit entity to put it in draft mode (entity was saved at end of test 61) - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - - // // Rename the attachment - // String newFileName = "renamed_sample.txt"; - // String renameResponse = - // api.renameAttachment( - // appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID, - // newFileName); - // assertEquals("Renamed", renameResponse, "Should successfully rename attachment"); - - // // Save entity after rename - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - // assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); - - // // Edit entity again and fetch changelog - // editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID); - // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - - // // Fetch changelog after rename - // Map changelogAfterRename = - // api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, - // changelogAttachmentID); - - // assertNotNull(changelogAfterRename, "Changelog response should not be null after rename"); - - // // Verify changelog has increased (rename operation adds 1 entry for cmis:name change) - // // Expected: 1 created + 3 initial updates + 1 rename update = 5 total - // assertEquals( - // 5, changelogAfterRename.get("numItems"), "Should have 5 changelog entries after rename"); - - // @SuppressWarnings("unchecked") - // List> changeLogsAfterRename = - // (List>) changelogAfterRename.get("changeLogs"); - // assertEquals( - // 5, changeLogsAfterRename.size(), "Should have exactly 5 changelog entries after rename"); - - // // Verify updated count is 4 (3 initial + 1 from rename operation) - // long updatedCountAfterRename = - // changeLogsAfterRename.stream() - // .filter(log -> "updated".equals(log.get("operation"))) - // .count(); - // assertEquals(4, updatedCountAfterRename, "Should have 4 'updated' operations after rename"); - - // // Verify filename change in changelog - // boolean hasFilenameUpdate = - // changeLogsAfterRename.stream() - // .filter(log -> "updated".equals(log.get("operation"))) - // .anyMatch( - // log -> { - // @SuppressWarnings("unchecked") - // Map changeDetail = (Map) - // log.get("changeDetail"); - // return changeDetail != null && "cmis:name".equals(changeDetail.get("field")); - // }); - // assertTrue(hasFilenameUpdate, "Should have an update entry for filename (cmis:name)"); - - // // Cleanup - entity was saved after rename, so delete the active entity - // api.deleteEntity(appUrl, entityName, changelogEntityID); - // } + @Test + @Order(59) + void testCopyAttachmentsSuccessNewEntityDraft() throws IOException { + System.out.println( + "Test (59): Copy attachments from one entity to another new entity draft mode"); + List attachments = new ArrayList<>(); + copyAttachmentSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + copyAttachmentTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (!copyAttachmentSourceEntity.equals("Could not create entity") + && !copyAttachmentTargetEntity.equals("Could not create entity")) { + ClassLoader classLoader = getClass().getClassLoader(); + List files = new ArrayList<>(); + files.add(new File(classLoader.getResource("sample.pdf").getFile())); + files.add(new File(classLoader.getResource("sample1.pdf").getFile())); + Map postData = new HashMap<>(); + postData.put("up__ID", entityID7); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // @Test - // @Order(63) - // void testChangelogWithCustomPropertyEditSave() throws IOException { - // System.out.println( - // "Test (63): Create entity with custom property, save, edit and save again - verify - // changelog remains at 3 entries"); + sourceObjectIds.clear(); - // // Create a new entity - // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // assertNotNull(newEntityID, "Failed to create new entity"); - // assertNotEquals("Could not create entity", newEntityID); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, copyAttachmentSourceEntity, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + attachments.add(createResponse.get(1)); + } else { + fail("Could not create attachment"); + } + } - // // Prepare a sample file to upload - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // assertTrue(file.exists(), "Sample file should exist"); + List> attachmentsMetadata = new ArrayList<>(); + Map fetchAttachmentMetadataResponse; + for (String attachment : attachments) { + try { + fetchAttachmentMetadataResponse = + api.fetchMetadataDraft( + appUrl, entityName, facetName, copyAttachmentSourceEntity, attachment); + attachmentsMetadata.add(fetchAttachmentMetadataResponse); + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } + for (Map metadata : attachmentsMetadata) { + if (metadata.containsKey("objectId")) { + sourceObjectIds.add(metadata.get("objectId").toString()); + } else { + fail("Attachment metadata does not contain objectId"); + } + } - // // Create attachment - // Map postData = new HashMap<>(); - // postData.put("up__ID", newEntityID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + if (sourceObjectIds.size() == 2) { + String copyResponse; + copyResponse = + api.copyAttachment( + appUrl, entityName, facetName, copyAttachmentTargetEntity, sourceObjectIds); + if (copyResponse.equals("Attachments copied successfully")) { + String saveEntityResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, copyAttachmentTargetEntity); + if (saveEntityResponse.equals("Saved")) { + List> fetchEntityMetadataResponse; + fetchEntityMetadataResponse = + api.fetchEntityMetadata(appUrl, entityName, facetName, copyAttachmentTargetEntity); + targetAttachmentIds = + fetchEntityMetadataResponse.stream() + .map(item -> (String) item.get("ID")) + .filter(Objects::nonNull) + .collect(Collectors.toList()); + String readResponse; + for (String targetAttachmentId : targetAttachmentIds) { + readResponse = + api.readAttachment( + appUrl, + entityName, + facetName, + copyAttachmentTargetEntity, + targetAttachmentId); + if (!readResponse.equals("OK")) { + fail("Could not read copied attachment"); + } + } + } else { + fail("Could not save entity after copying attachments: " + saveEntityResponse); + } + } else { + fail("Could not copy attachments: " + copyResponse); + } + } else { + fail("Could not fetch objects Ids for all attachments"); + } + } else { + fail("Could not create entities"); + } + api.deleteEntityDraft(appUrl, entityName, copyAttachmentSourceEntity); + api.deleteEntity(appUrl, entityName, copyAttachmentTargetEntity); + } - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, - // file); - - // assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - // String status = createResponse.get(0); - // String attachmentID = createResponse.get(1); - - // assertEquals("Attachment created", status, "Attachment should be created successfully"); - // assertNotNull(attachmentID, "Attachment ID should not be null"); - // assertNotEquals("", attachmentID, "Attachment ID should not be empty"); - - // // Add a custom property - // Integer customPropertyValue = 99999; - // RequestBody bodyInt = - // RequestBody.create( - // "{\"customProperty2\": " + customPropertyValue + "}", - // MediaType.parse("application/json")); - // String updateCustomPropertyResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, newEntityID, attachmentID, bodyInt); - // assertEquals( - // "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); - - // // Save the entity - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - - // // Edit entity to fetch initial changelog - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - - // // Fetch changelog after initial save - // Map changelogResponse = - // api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - - // // Verify changelog has 3 entries: 1 created + 2 updated (cmis:secondaryObjectTypeIds + - // // customProperty2) - // assertEquals(3, changelogResponse.get("numItems"), "Should have 3 changelog entries - // initially"); - - // @SuppressWarnings("unchecked") - // List> changeLogs = - // (List>) changelogResponse.get("changeLogs"); - // assertEquals(3, changeLogs.size(), "Should have exactly 3 changelog entries"); - - // // Save entity again without any modifications - // saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); - - // // Edit entity again and fetch changelog - // editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - - // // Fetch changelog after second save - // Map changelogAfterSecondSave = - // api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); - - // assertNotNull( - // changelogAfterSecondSave, "Changelog response should not be null after second save"); - - // // Verify changelog still has only 3 entries (no new entries added) - // assertEquals( - // 3, - // changelogAfterSecondSave.get("numItems"), - // "Should still have only 3 changelog entries after edit-save without modifications"); - - // @SuppressWarnings("unchecked") - // List> changeLogsAfterSecondSave = - // (List>) changelogAfterSecondSave.get("changeLogs"); - // assertEquals( - // 3, - // changeLogsAfterSecondSave.size(), - // "Should still have exactly 3 changelog entries after second save"); - - // // Clean up the entity - // api.deleteEntity(appUrl, entityName, newEntityID); - // } + @Test + @Order(60) + void testViewChangelogForNewlyCreatedAttachment() throws IOException { + System.out.println("Test (60): View changelog for newly created attachment"); - // @Test - // @Order(64) - // void testChangelogForSavedAttachmentWithoutModification() throws IOException { - // System.out.println( - // "Test (64): Create entity, upload attachment, save, edit and save again - verify - // changelog still has only 'created' entry"); + // Create a new entity for changelog test + changelogEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + assertNotNull(changelogEntityID, "Failed to create changelog test entity"); + assertNotEquals("Could not create entity", changelogEntityID); - // // Create a new entity - // String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // assertNotNull(newEntityID, "Failed to create new entity"); - // assertNotEquals("Could not create entity", newEntityID); + // Prepare a sample file to upload + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.txt").getFile()); + assertTrue(file.exists(), "Sample file should exist"); - // // Prepare a sample file to upload - // ClassLoader classLoader = getClass().getClassLoader(); - // File file = new File(classLoader.getResource("sample.pdf").getFile()); - // assertTrue(file.exists(), "Sample file should exist"); + // Create attachment + Map postData = new HashMap<>(); + postData.put("up__ID", changelogEntityID); + postData.put("mimeType", "text/plain"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // Create attachment - // Map postData = new HashMap<>(); - // postData.put("up__ID", newEntityID); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, changelogEntityID, srvpath, postData, file); - // List createResponse = - // api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, - // file); - - // assertEquals(2, createResponse.size(), "Should return status and attachment ID"); - // String status = createResponse.get(0); - // String newAttachmentID = createResponse.get(1); - - // assertEquals("Attachment created", status, "Attachment should be created successfully"); - // assertNotNull(newAttachmentID, "Attachment ID should not be null"); - // assertNotEquals("", newAttachmentID, "Attachment ID should not be empty"); - - // // Save the entity immediately without any modifications - // String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - - // // Edit entity again without making any changes to the attachment - // String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - - // // Save entity again without modifying the attachment - // saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); - // assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); - - // // Edit entity to fetch changelog - // editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); - // assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - - // // Fetch changelog for the attachment - // Map changelogResponse = - // api.fetchChangelog(appUrl, entityName, facetName, newEntityID, newAttachmentID); - - // assertNotNull(changelogResponse, "Changelog response should not be null"); - - // // Verify changelog content - should only have 'created' entry even after edit and save - // assertEquals(false, changelogResponse.get("hasMoreItems"), "hasMoreItems should be false"); - // assertEquals( - // "sample.pdf", changelogResponse.get("filename"), "Filename should match uploaded file"); - // assertNotNull(changelogResponse.get("objectId"), "ObjectId should not be null"); - // assertEquals(1, changelogResponse.get("numItems"), "Should have only 1 changelog entry"); - - // // Verify the changelog entry - // @SuppressWarnings("unchecked") - // List> changeLogs = - // (List>) changelogResponse.get("changeLogs"); - // assertEquals(1, changeLogs.size(), "Should have exactly 1 changelog entry"); - - // Map logEntry = changeLogs.get(0); - // assertEquals("created", logEntry.get("operation"), "Operation should be 'created'"); - // assertNotNull(logEntry.get("time"), "Time should not be null"); - // assertNotNull(logEntry.get("user"), "User should not be null"); - // assertFalse( - // logEntry.containsKey("changeDetail"), "Created operation should not have changeDetail"); - - // // Clean up the new entity - // api.deleteEntity(appUrl, entityName, newEntityID); - // } + assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + String status = createResponse.get(0); + changelogAttachmentID = createResponse.get(1); - // @Test - // @Order(65) - // void testMoveAttachmentsWithSourceFacet() throws IOException { - // System.out.println( - // "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); + assertEquals("Attachment created", status, "Attachment should be created successfully"); + assertNotNull(changelogAttachmentID, "Attachment ID should not be null"); + assertNotEquals("", changelogAttachmentID, "Attachment ID should not be empty"); - // // Create source entity and add attachments - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + // Fetch changelog for the newly created attachment + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); - // // Prepare sample files - // ClassLoader classLoader = getClass().getClassLoader(); - // List files = new ArrayList<>(); - // files.add(new File(classLoader.getResource("sample.pdf").getFile())); - // files.add(new File(classLoader.getResource("sample.txt").getFile())); + assertNotNull(changelogResponse, "Changelog response should not be null"); - // Map postData = new HashMap<>(); - // postData.put("up__ID", moveSourceEntity); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + // Verify changelog structure + assertEquals(false, changelogResponse.get("hasMoreItems"), "hasMoreItems should be false"); + assertEquals( + "sample.txt", changelogResponse.get("filename"), "Filename should match uploaded file"); + assertNotNull(changelogResponse.get("objectId"), "ObjectId should not be null"); + assertEquals(1, changelogResponse.get("numItems"), "Should have 1 changelog entry"); + + // Verify the changelog entry + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals(1, changeLogs.size(), "Should have exactly 1 changelog entry"); + + Map logEntry = changeLogs.get(0); + assertEquals("created", logEntry.get("operation"), "Operation should be 'created'"); + assertNotNull(logEntry.get("time"), "Time should not be null"); + assertNotNull(logEntry.get("user"), "User should not be null"); + assertFalse( + logEntry.containsKey("changeDetail"), "Created operation should not have changeDetail"); + } - // // Create attachments in source entity - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in source entity"); - // } - // } + @Test + @Order(61) + void testChangelogAfterModifyingNoteAndCustomProperty() throws IOException { + System.out.println( + "Test (61): Modify note field and custom property, then verify changelog shows created + 3 updated entries"); - // // Save source entity - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + // Update attachment with notes field (entity is already in draft mode from test 60) + String notesValue = "Test note for changelog verification"; + MediaType mediaType = MediaType.parse("application/json"); + String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); - // // Fetch object IDs from source entity - // moveObjectIds.clear(); - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // // Get source folder ID - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } else { - // fail("Attachment metadata does not contain objectId"); - // } - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } + String updateNotesResponse = + api.updateSecondaryProperty( + appUrl, + entityName, + facetName, + changelogEntityID, + changelogAttachmentID, + updateNotesBody); + assertEquals("Updated", updateNotesResponse, "Should successfully update notes field"); + + // Update attachment with custom property + Integer customProperty2Value = 12345; + RequestBody bodyInt = + RequestBody.create( + "{\"customProperty2\": " + customProperty2Value + "}", + MediaType.parse("application/json")); + String updateCustomPropertyResponse = + api.updateSecondaryProperty( + appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID, bodyInt); + assertEquals( + "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + // Save the entity + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - // // Create target entity - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + // Edit entity again to fetch changelog + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - // // Save target before move - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity: " + saveTargetBeforeMoveResponse); - // } + // Fetch changelog after modifications + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); - // // Move attachments from source to target with sourceFacet - // String sourceFacet = serviceName + "." + entityName + "." + facetName; - // String targetFacet = serviceName + "." + entityName + "." + facetName; - // api.moveAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // All attachments moved to target entity in SDM & UI - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // assertEquals( - // sourceAttachmentIds.size(), - // targetMetadataAfterMove.size(), - // "Target entity should have all attachments after move"); - - // // Verify attachments can be read from target entity - // for (Map metadata : targetMetadataAfterMove) { - // String targetAttachmentId = (String) metadata.get("ID"); - // String readResponse = - // api.readAttachment(appUrl, entityName, facetName, moveTargetEntity, - // targetAttachmentId); - // if (!readResponse.equals("OK")) { - // fail("Could not read moved attachment from target entity"); - // } - // } + assertNotNull(changelogResponse, "Changelog response should not be null"); - // // All attachments removed from source entity in SDM & UI - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // assertEquals( - // 0, sourceMetadataAfterMove.size(), "Source entity should have 0 attachments after move"); + // Verify changelog content - should have 1 created + 3 updated (note, customProperty2, and + // internal update) + assertEquals(false, changelogResponse.get("hasMoreItems"), "hasMoreItems should be false"); + assertEquals( + 4, + changelogResponse.get("numItems"), + "Should have 4 changelog entries (1 created + 3 updated)"); - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals(4, changeLogs.size(), "Should have exactly 4 changelog entries"); - // @Test - // @Order(66) - // public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { - // System.out.println( - // "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); + // Verify first entry is 'created' + Map createdEntry = changeLogs.get(0); + assertEquals( + "created", createdEntry.get("operation"), "First entry should be 'created' operation"); + + // Verify remaining entries are 'updated' + long updatedCount = + changeLogs.stream().filter(log -> "updated".equals(log.get("operation"))).count(); + assertEquals(3, updatedCount, "Should have 3 'updated' operations"); + + // Verify that changeDetail exists in updated entries for note field + boolean hasNoteUpdate = + changeLogs.stream() + .filter(log -> "updated".equals(log.get("operation"))) + .anyMatch( + log -> { + @SuppressWarnings("unchecked") + Map changeDetail = (Map) log.get("changeDetail"); + return changeDetail != null + && "cmis:description".equals(changeDetail.get("field")); + }); + assertTrue(hasNoteUpdate, "Should have an update entry for note field (cmis:description)"); + assertTrue(hasNoteUpdate, "Should have an update entry for note field (cmis:description)"); + + // Save the entity so test 62 can edit it + String saveResponseFinal = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + assertEquals("Saved", saveResponseFinal, "Entity should be saved successfully"); + } - // // Create source entity and add attachments - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + @Test + @Order(62) + void testChangelogAfterRenamingAttachment() throws IOException { + System.out.println( + "Test (62): Rename attachment and verify changelog increases with rename entry"); - // // Prepare sample files - // ClassLoader classLoader = getClass().getClassLoader(); - // List files = new ArrayList<>(); - // files.add(new File(classLoader.getResource("sample.pdf").getFile())); - // files.add(new File(classLoader.getResource("sample.txt").getFile())); + // Edit entity to put it in draft mode (entity was saved at end of test 61) + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - // Map postData = new HashMap<>(); - // postData.put("up__ID", moveSourceEntity); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + // Rename the attachment + String newFileName = "renamed_sample.txt"; + String renameResponse = + api.renameAttachment( + appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID, newFileName); + assertEquals("Renamed", renameResponse, "Should successfully rename attachment"); - // // Create attachments in source entity - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in source entity"); - // } - // } + // Save entity after rename + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully after rename"); - // // Save source entity - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + // Edit entity again and fetch changelog + editResponse = api.editEntityDraft(appUrl, entityName, srvpath, changelogEntityID); + assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - // // Fetch object IDs from source entity - // moveObjectIds.clear(); - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (Exception e) { - // fail("Could not fetch metadata for attachment: " + attachmentId); - // } - // } + // Fetch changelog after rename + Map changelogAfterRename = + api.fetchChangelog(appUrl, entityName, facetName, changelogEntityID, changelogAttachmentID); - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch all objectIds from source entity"); - // } + assertNotNull(changelogAfterRename, "Changelog response should not be null after rename"); - // // Create target entity and add attachment - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + // Verify changelog has increased (rename operation adds 1 entry for cmis:name change) + // Expected: 1 created + 3 initial updates + 1 rename update = 5 total + assertEquals( + 5, changelogAfterRename.get("numItems"), "Should have 5 changelog entries after rename"); - // Map targetPostData = new HashMap<>(); - // targetPostData.put("up__ID", moveTargetEntity); - // targetPostData.put("mimeType", "application/pdf"); - // targetPostData.put("createdAt", new Date().toString()); - // targetPostData.put("createdBy", "test@test.com"); - // targetPostData.put("modifiedBy", "test@test.com"); + @SuppressWarnings("unchecked") + List> changeLogsAfterRename = + (List>) changelogAfterRename.get("changeLogs"); + assertEquals( + 5, changeLogsAfterRename.size(), "Should have exactly 5 changelog entries after rename"); + + // Verify updated count is 4 (3 initial + 1 from rename operation) + long updatedCountAfterRename = + changeLogsAfterRename.stream() + .filter(log -> "updated".equals(log.get("operation"))) + .count(); + assertEquals(4, updatedCountAfterRename, "Should have 4 'updated' operations after rename"); + + // Verify filename change in changelog + boolean hasFilenameUpdate = + changeLogsAfterRename.stream() + .filter(log -> "updated".equals(log.get("operation"))) + .anyMatch( + log -> { + @SuppressWarnings("unchecked") + Map changeDetail = (Map) log.get("changeDetail"); + return changeDetail != null && "cmis:name".equals(changeDetail.get("field")); + }); + assertTrue(hasFilenameUpdate, "Should have an update entry for filename (cmis:name)"); + + // Cleanup - entity was saved after rename, so delete the active entity + api.deleteEntity(appUrl, entityName, changelogEntityID); + } - // File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); - // List targetCreateResponse = - // api.createAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity, - // srvpath, - // targetPostData, - // duplicateFile); - - // if (!targetCreateResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment on target entity"); - // } + @Test + @Order(63) + void testChangelogWithCustomPropertyEditSave() throws IOException { + System.out.println( + "Test (63): Create entity with custom property, save, edit and save again - verify changelog remains at 3 entries"); - // // Save target entity to persist the attachment - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse); - // } + // Create a new entity + String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + assertNotNull(newEntityID, "Failed to create new entity"); + assertNotEquals("Could not create entity", newEntityID); - // // Fetch target metadata before move (target entity is now saved with 1 attachment) - // List> targetMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // int targetCountBeforeMove = targetMetadataBeforeMove.size(); - - // // Move attachments from source to target with sourceFacet - // String sourceFacet = serviceName + "." + entityName + "." + facetName; - // String targetFacet = serviceName + "." + entityName + "." + facetName; - // api.moveAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // // Verify target has duplicate skipped, other attachments moved - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - - // // Expected: original attachments + non-duplicate moved attachments - // int expectedTargetCount = targetCountBeforeMove + (sourceAttachmentIds.size() - 1); - // assertEquals( - // expectedTargetCount, - // targetMetadataAfterMove.size(), - // "Target should have duplicate skipped, other attachments moved"); - - // // Verify source entity has only the duplicate attachment remaining - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // // Calculate expected source count: number of duplicates that couldn't be moved - // int expectedSourceCount = - // sourceAttachmentIds.size() - (targetMetadataAfterMove.size() - targetCountBeforeMove); - // assertEquals( - // expectedSourceCount, - // sourceMetadataAfterMove.size(), - // "Source should have duplicate attachment remaining"); - - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // Prepare a sample file to upload + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + assertTrue(file.exists(), "Sample file should exist"); - // @Test - // @Order(67) - // public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { - // System.out.println( - // "Test (67): Move attachments with notes and secondary properties with sourceFacet"); + // Create attachment + Map postData = new HashMap<>(); + postData.put("up__ID", newEntityID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // Create source entity and add attachments - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + List createResponse = + api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); + + assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + String status = createResponse.get(0); + String attachmentID = createResponse.get(1); + + assertEquals("Attachment created", status, "Attachment should be created successfully"); + assertNotNull(attachmentID, "Attachment ID should not be null"); + assertNotEquals("", attachmentID, "Attachment ID should not be empty"); + + // Add a custom property + Integer customPropertyValue = 99999; + RequestBody bodyInt = + RequestBody.create( + "{\"customProperty2\": " + customPropertyValue + "}", + MediaType.parse("application/json")); + String updateCustomPropertyResponse = + api.updateSecondaryProperty( + appUrl, entityName, facetName, newEntityID, attachmentID, bodyInt); + assertEquals( + "Updated", updateCustomPropertyResponse, "Should successfully update custom property"); - // // Prepare sample files - // ClassLoader classLoader = getClass().getClassLoader(); - // List files = new ArrayList<>(); - // files.add(new File(classLoader.getResource("sample.pdf").getFile())); - // files.add(new File(classLoader.getResource("sample.txt").getFile())); + // Save the entity + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - // Map postData = new HashMap<>(); - // postData.put("up__ID", moveSourceEntity); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + // Edit entity to fetch initial changelog + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - // // Create attachments in source entity - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in source entity"); - // } - // } + // Fetch changelog after initial save + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); - // // Add notes to attachments - // String notesValue = "Test note for verification"; - // MediaType mediaType = MediaType.parse("application/json"); - // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - // RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); - - // for (String attachmentId : sourceAttachmentIds) { - // String updateNotesResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, moveSourceEntity, attachmentId, updateNotesBody); - // if (!updateNotesResponse.equals("Updated")) { - // fail("Could not update notes for attachment: " + attachmentId); - // } - // } + assertNotNull(changelogResponse, "Changelog response should not be null"); - // // Add custom property to attachments - // Integer customProperty2Value = 54321; - // RequestBody bodyInt = - // RequestBody.create( - // "{\"customProperty2\": " + customProperty2Value + "}", - // MediaType.parse("application/json")); - - // for (String attachmentId : sourceAttachmentIds) { - // String updateCustomPropertyResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, moveSourceEntity, attachmentId, bodyInt); - // if (!updateCustomPropertyResponse.equals("Updated")) { - // fail("Could not update custom property for attachment: " + attachmentId); - // } - // } + // Verify changelog has 3 entries: 1 created + 2 updated (cmis:secondaryObjectTypeIds + + // customProperty2) + assertEquals(3, changelogResponse.get("numItems"), "Should have 3 changelog entries initially"); - // // Save source entity - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals(3, changeLogs.size(), "Should have exactly 3 changelog entries"); - // // Fetch object IDs from source entity - // moveObjectIds.clear(); - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (Exception e) { - // fail("Could not fetch metadata for attachment: " + attachmentId); - // } - // } + // Save entity again without any modifications + saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch all objectIds from source entity"); - // } + // Edit entity again and fetch changelog + editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - // // Create target entity - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + // Fetch changelog after second save + Map changelogAfterSecondSave = + api.fetchChangelog(appUrl, entityName, facetName, newEntityID, attachmentID); - // // Save target before move - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse); - // } + assertNotNull( + changelogAfterSecondSave, "Changelog response should not be null after second save"); - // // Move attachments from source to target with sourceFacet - // String sourceFacet = serviceName + "." + entityName + "." + facetName; - // String targetFacet = serviceName + "." + entityName + "." + facetName; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + // Verify changelog still has only 3 entries (no new entries added) + assertEquals( + 3, + changelogAfterSecondSave.get("numItems"), + "Should still have only 3 changelog entries after edit-save without modifications"); - // // Verify all attachments moved to target - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // assertEquals( - // sourceAttachmentIds.size(), - // targetMetadataAfterMove.size(), - // "Target entity should have all attachments after move"); - - // // Verify notes and secondary properties are preserved - // for (Map metadata : targetMetadataAfterMove) { - // String targetAttachmentId = (String) metadata.get("ID"); - // assertNotNull(targetAttachmentId, "Target attachment ID should not be null"); - - // Map detailedMetadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); - - // // Verify notes are preserved - // if (detailedMetadata.containsKey("note")) { - // assertEquals( - // notesValue, - // detailedMetadata.get("note"), - // "Notes should be preserved after move for attachment: " + targetAttachmentId); - // } else { - // fail("Notes property missing after move for attachment: " + targetAttachmentId); - // } + @SuppressWarnings("unchecked") + List> changeLogsAfterSecondSave = + (List>) changelogAfterSecondSave.get("changeLogs"); + assertEquals( + 3, + changeLogsAfterSecondSave.size(), + "Should still have exactly 3 changelog entries after second save"); - // // Verify custom property is preserved - // if (detailedMetadata.containsKey("customProperty2")) { - // assertEquals( - // customProperty2Value, - // detailedMetadata.get("customProperty2"), - // "Custom property should be preserved after move for attachment: " + - // targetAttachmentId); - // } else { - // fail("Custom property missing after move for attachment: " + targetAttachmentId); - // } - // } + // Clean up the entity + api.deleteEntity(appUrl, entityName, newEntityID); + } - // // Verify source entity has no attachments (all moved with sourceFacet) - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // assertEquals(0, sourceMetadataAfterMove.size(), "Source entity has no attachments after - // move"); + @Test + @Order(64) + void testChangelogForSavedAttachmentWithoutModification() throws IOException { + System.out.println( + "Test (64): Create entity, upload attachment, save, edit and save again - verify changelog still has only 'created' entry"); - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // Create a new entity + String newEntityID = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + assertNotNull(newEntityID, "Failed to create new entity"); + assertNotEquals("Could not create entity", newEntityID); - // @Test - // @Order(68) - // public void testMoveAttachmentsWithoutSourceFacet() throws Exception { - // System.out.println( - // "Test (68): Move valid attachments from Source Entity to Target Entity without - // sourceFacet"); + // Prepare a sample file to upload + ClassLoader classLoader = getClass().getClassLoader(); + File file = new File(classLoader.getResource("sample.pdf").getFile()); + assertTrue(file.exists(), "Sample file should exist"); - // // Create source entity and add attachments - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + // Create attachment + Map postData = new HashMap<>(); + postData.put("up__ID", newEntityID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // Prepare sample files - // ClassLoader classLoader = getClass().getClassLoader(); - // List files = new ArrayList<>(); - // files.add(new File(classLoader.getResource("sample.pdf").getFile())); - // files.add(new File(classLoader.getResource("sample.txt").getFile())); + List createResponse = + api.createAttachment(appUrl, entityName, facetName, newEntityID, srvpath, postData, file); - // Map postData = new HashMap<>(); - // postData.put("up__ID", moveSourceEntity); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + assertEquals(2, createResponse.size(), "Should return status and attachment ID"); + String status = createResponse.get(0); + String newAttachmentID = createResponse.get(1); - // // Create attachments in source entity - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in source entity"); - // } - // } + assertEquals("Attachment created", status, "Attachment should be created successfully"); + assertNotNull(newAttachmentID, "Attachment ID should not be null"); + assertNotEquals("", newAttachmentID, "Attachment ID should not be empty"); - // // Save source entity - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + // Save the entity immediately without any modifications + String saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully"); - // // Fetch object IDs from source entity - // moveObjectIds.clear(); - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // // Get source folder ID from first attachment - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } else { - // fail("Attachment metadata does not contain objectId"); - // } - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } + // Edit entity again without making any changes to the attachment + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + // Save entity again without modifying the attachment + saveResponse = api.saveEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Saved", saveResponse, "Entity should be saved successfully again"); - // // Create target entity - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + // Edit entity to fetch changelog + editResponse = api.editEntityDraft(appUrl, entityName, srvpath, newEntityID); + assertEquals("Entity in draft mode", editResponse, "Entity should be in draft mode"); - // // Save target before move - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move"); - // } + // Fetch changelog for the attachment + Map changelogResponse = + api.fetchChangelog(appUrl, entityName, facetName, newEntityID, newAttachmentID); - // // Move attachments without sourceFacet (pass null for sourceFacet parameter) - // String targetFacet = serviceName + "." + entityName + "." + facetName; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // null); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + assertNotNull(changelogResponse, "Changelog response should not be null"); - // // Verify attachments are in target entity - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // assertEquals( - // moveObjectIds.size(), - // targetMetadataAfterMove.size(), - // "Target entity should have all moved attachments"); - - // // Verify attachments can be read from target entity - // for (Map metadata : targetMetadataAfterMove) { - // String targetAttachmentId = (String) metadata.get("ID"); - // String readResponse = - // api.readAttachment(appUrl, entityName, facetName, moveTargetEntity, - // targetAttachmentId); - // if (!readResponse.equals("OK")) { - // fail("Could not read moved attachment from target entity"); - // } - // } + // Verify changelog content - should only have 'created' entry even after edit and save + assertEquals(false, changelogResponse.get("hasMoreItems"), "hasMoreItems should be false"); + assertEquals( + "sample.pdf", changelogResponse.get("filename"), "Filename should match uploaded file"); + assertNotNull(changelogResponse.get("objectId"), "ObjectId should not be null"); + assertEquals(1, changelogResponse.get("numItems"), "Should have only 1 changelog entry"); + + // Verify the changelog entry + @SuppressWarnings("unchecked") + List> changeLogs = + (List>) changelogResponse.get("changeLogs"); + assertEquals(1, changeLogs.size(), "Should have exactly 1 changelog entry"); + + Map logEntry = changeLogs.get(0); + assertEquals("created", logEntry.get("operation"), "Operation should be 'created'"); + assertNotNull(logEntry.get("time"), "Time should not be null"); + assertNotNull(logEntry.get("user"), "User should not be null"); + assertFalse( + logEntry.containsKey("changeDetail"), "Created operation should not have changeDetail"); + + // Clean up the new entity + api.deleteEntity(appUrl, entityName, newEntityID); + } - // // Expected Behavior: Attachments remain in source entity UI (without sourceFacet) - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // assertEquals( - // moveObjectIds.size(), - // sourceMetadataAfterMove.size(), - // "Source entity should still have attachments in UI when sourceFacet is not specified"); - - // // Verify the same objectIds are still visible in source - // for (Map metadata : sourceMetadataAfterMove) { - // String objectId = (String) metadata.get("objectId"); - // assertTrue( - // moveObjectIds.contains(objectId), - // "Source entity should still show attachment with objectId: " + objectId); - // } + @Test + @Order(65) + void testMoveAttachmentsWithSourceFacet() throws IOException { + System.out.println( + "Test (65): Move attachments from Source Entity to Target Entity with sourceFacet"); - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // Create source entity and add attachments + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // @Test - // @Order(69) - // public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { - // System.out.println( - // "Test (69): Move attachments into existing Target Entity when duplicate exists without - // sourceFacet"); + // Prepare sample files + ClassLoader classLoader = getClass().getClassLoader(); + List files = new ArrayList<>(); + files.add(new File(classLoader.getResource("sample.pdf").getFile())); + files.add(new File(classLoader.getResource("sample.txt").getFile())); - // // Create source entity and add attachments - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + Map postData = new HashMap<>(); + postData.put("up__ID", moveSourceEntity); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // Prepare sample files - // ClassLoader classLoader = getClass().getClassLoader(); - // List files = new ArrayList<>(); - // files.add(new File(classLoader.getResource("sample.pdf").getFile())); - // files.add(new File(classLoader.getResource("sample.txt").getFile())); + // Create attachments in source entity + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment in source entity"); + } + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", moveSourceEntity); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + // Save source entity + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // // Create attachments in source entity - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in source entity"); - // } - // } + // Fetch object IDs from source entity + moveObjectIds.clear(); + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + // Get source folder ID + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } else { + fail("Attachment metadata does not contain objectId"); + } + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } - // // Save source entity - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // // Fetch object IDs from source entity - // moveObjectIds.clear(); - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // // Get source folder ID from first attachment - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } else { - // fail("Attachment metadata does not contain objectId"); - // } - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } + // Create target entity + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + // Save target before move + String saveTargetBeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity: " + saveTargetBeforeMoveResponse); + } - // // Create target entity and add duplicate attachment (sample.pdf) - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + // Move attachments from source to target with sourceFacet + String sourceFacet = serviceName + "." + entityName + "." + facetName; + String targetFacet = serviceName + "." + entityName + "." + facetName; + api.moveAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // All attachments moved to target entity in SDM & UI + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + assertEquals( + sourceAttachmentIds.size(), + targetMetadataAfterMove.size(), + "Target entity should have all attachments after move"); + + // Verify attachments can be read from target entity + for (Map metadata : targetMetadataAfterMove) { + String targetAttachmentId = (String) metadata.get("ID"); + String readResponse = + api.readAttachment(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); + if (!readResponse.equals("OK")) { + fail("Could not read moved attachment from target entity"); + } + } - // // Add the same first file (sample.pdf) to target entity to create duplicate - // Map targetPostData = new HashMap<>(); - // targetPostData.put("up__ID", moveTargetEntity); - // targetPostData.put("mimeType", "application/pdf"); - // targetPostData.put("createdAt", new Date().toString()); - // targetPostData.put("createdBy", "test@test.com"); - // targetPostData.put("modifiedBy", "test@test.com"); + // All attachments removed from source entity in SDM & UI + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals( + 0, sourceMetadataAfterMove.size(), "Source entity should have 0 attachments after move"); - // List createTargetResponse = - // api.createAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity, - // srvpath, - // targetPostData, - // files.get(0)); // Add same file (sample.pdf) - // if (!createTargetResponse.get(0).equals("Attachment created")) { - // fail("Could not create duplicate attachment in target entity"); - // } + // Clean up - delete both entities + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } - // // Save target entity before move - // String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity: " + saveTargetResponse); - // } + @Test + @Order(66) + public void testMoveAttachmentsToEntityWithDuplicateWithSourceFacet() throws Exception { + System.out.println( + "Test (66): Move attachments to entity with duplicate attachment with sourceFacet"); - // // Get initial target metadata count - // List> targetMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // int initialTargetCount = targetMetadataBeforeMove.size(); - - // // Step 3: Move attachments without sourceFacet (duplicate should be skipped) - // String targetFacet = serviceName + "." + entityName + "." + facetName; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // null); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + // Create source entity and add attachments + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // // Expected Behavior - Verify duplicate was skipped, other attachments moved - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - - // int nonDuplicateCount = moveObjectIds.size() - 1; - // int expectedTargetCount = initialTargetCount + nonDuplicateCount; - - // assertEquals( - // expectedTargetCount, - // targetMetadataAfterMove.size(), - // "Target entity should have initial attachments plus non-duplicate moved attachments"); - - // // Verify at least one non-duplicate attachment was moved - // assertTrue( - // targetMetadataAfterMove.size() > initialTargetCount, - // "Target should have more attachments after move (non-duplicates added)"); - - // // Verify all attachments still remain in source entity UI (without sourceFacet) - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // assertEquals( - // moveObjectIds.size(), - // sourceMetadataAfterMove.size(), - // "Source entity should still have all attachments in UI when sourceFacet is not - // specified"); - - // // Verify all original objectIds are still visible in source - // List sourceObjectIds = new ArrayList<>(); - // for (Map metadata : sourceMetadataAfterMove) { - // sourceObjectIds.add((String) metadata.get("objectId")); - // } - // for (String objectId : moveObjectIds) { - // assertTrue( - // sourceObjectIds.contains(objectId), - // "Source entity should still show attachment with objectId: " + objectId); - // } + // Prepare sample files + ClassLoader classLoader = getClass().getClassLoader(); + List files = new ArrayList<>(); + files.add(new File(classLoader.getResource("sample.pdf").getFile())); + files.add(new File(classLoader.getResource("sample.txt").getFile())); - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + Map postData = new HashMap<>(); + postData.put("up__ID", moveSourceEntity); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // @Test - // @Order(70) - // public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() - // throws Exception { - // System.out.println( - // "Test (70): Move attachments with notes and secondary properties without sourceFacet"); + // Create attachments in source entity + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment in source entity"); + } + } - // // Create source entity and add attachments - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + // Save source entity + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } + + // Fetch object IDs from source entity + moveObjectIds.clear(); + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (Exception e) { + fail("Could not fetch metadata for attachment: " + attachmentId); + } + } - // // Prepare sample files - // ClassLoader classLoader = getClass().getClassLoader(); - // List files = new ArrayList<>(); - // files.add(new File(classLoader.getResource("sample.pdf").getFile())); - // files.add(new File(classLoader.getResource("sample.txt").getFile())); + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", moveSourceEntity); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + // Create target entity and add attachment + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // // Create attachments in source entity - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in source entity"); - // } - // } + Map targetPostData = new HashMap<>(); + targetPostData.put("up__ID", moveTargetEntity); + targetPostData.put("mimeType", "application/pdf"); + targetPostData.put("createdAt", new Date().toString()); + targetPostData.put("createdBy", "test@test.com"); + targetPostData.put("modifiedBy", "test@test.com"); - // // Add notes to attachments - // String notesValue = "Test note for migration verification"; - // MediaType mediaType = MediaType.parse("application/json"); - // String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; - // RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); - - // for (String attachmentId : sourceAttachmentIds) { - // String updateNotesResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, moveSourceEntity, attachmentId, updateNotesBody); - // if (!updateNotesResponse.equals("Updated")) { - // fail("Could not update notes for attachment: " + attachmentId); - // } - // } + File duplicateFile = new File(classLoader.getResource("sample.pdf").getFile()); + List targetCreateResponse = + api.createAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity, + srvpath, + targetPostData, + duplicateFile); - // // Add custom property to attachments - // Integer customProperty2Value = 54321; - // RequestBody bodyInt = - // RequestBody.create( - // "{\"customProperty2\": " + customProperty2Value + "}", - // MediaType.parse("application/json")); - - // for (String attachmentId : sourceAttachmentIds) { - // String updateCustomPropertyResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, moveSourceEntity, attachmentId, bodyInt); - // if (!updateCustomPropertyResponse.equals("Updated")) { - // fail("Could not update custom property for attachment: " + attachmentId); - // } - // } + if (!targetCreateResponse.get(0).equals("Attachment created")) { + fail("Could not create attachment on target entity"); + } - // // Save source entity - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + // Save target entity to persist the attachment + String saveTargetBeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse); + } - // // Fetch object IDs from source entity - // moveObjectIds.clear(); - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (Exception e) { - // fail("Could not fetch metadata for attachment: " + attachmentId); - // } - // } + // Fetch target metadata before move (target entity is now saved with 1 attachment) + List> targetMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + int targetCountBeforeMove = targetMetadataBeforeMove.size(); + + // Move attachments from source to target with sourceFacet + String sourceFacet = serviceName + "." + entityName + "." + facetName; + String targetFacet = serviceName + "." + entityName + "." + facetName; + api.moveAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + // Verify target has duplicate skipped, other attachments moved + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + + // Expected: original attachments + non-duplicate moved attachments + int expectedTargetCount = targetCountBeforeMove + (sourceAttachmentIds.size() - 1); + assertEquals( + expectedTargetCount, + targetMetadataAfterMove.size(), + "Target should have duplicate skipped, other attachments moved"); + + // Verify source entity has only the duplicate attachment remaining + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + // Calculate expected source count: number of duplicates that couldn't be moved + int expectedSourceCount = + sourceAttachmentIds.size() - (targetMetadataAfterMove.size() - targetCountBeforeMove); + assertEquals( + expectedSourceCount, + sourceMetadataAfterMove.size(), + "Source should have duplicate attachment remaining"); - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch all objectIds from source entity"); - // } + // Clean up - delete both entities + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } - // // Get source attachment count before move - // List> sourceMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + @Test + @Order(67) + public void testMoveAttachmentsWithNotesAndSecondaryProperties() throws Exception { + System.out.println( + "Test (67): Move attachments with notes and secondary properties with sourceFacet"); - // // Create target entity - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + // Create source entity and add attachments + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // // Save target before move - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move"); - // } + // Prepare sample files + ClassLoader classLoader = getClass().getClassLoader(); + List files = new ArrayList<>(); + files.add(new File(classLoader.getResource("sample.pdf").getFile())); + files.add(new File(classLoader.getResource("sample.txt").getFile())); - // // Get target attachment count before move - // List> targetMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // int targetCountBeforeMove = targetMetadataBeforeMove.size(); - - // // Move attachments from source to target WITHOUT sourceFacet - // String targetFacet = serviceName + "." + entityName + "." + facetName; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // null); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + Map postData = new HashMap<>(); + postData.put("up__ID", moveSourceEntity); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // Verify expected number of attachments moved to target - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // int expectedTargetCount = targetCountBeforeMove + sourceAttachmentIds.size(); - // assertEquals( - // expectedTargetCount, - // targetMetadataAfterMove.size(), - // "Target entity should have " + expectedTargetCount + " attachments after move"); - - // // Verify notes and secondary properties are preserved - // for (Map metadata : targetMetadataAfterMove) { - // String targetAttachmentId = (String) metadata.get("ID"); - // assertNotNull(targetAttachmentId, "Target attachment ID should not be null"); - - // Map detailedMetadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); - - // // Verify notes are preserved - // if (detailedMetadata.containsKey("note")) { - // assertEquals( - // notesValue, - // detailedMetadata.get("note"), - // "Notes should be preserved after move for attachment: " + targetAttachmentId); - // } else { - // fail("Notes property missing after move for attachment: " + targetAttachmentId); - // } + // Create attachments in source entity + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment in source entity"); + } + } - // // Verify custom property is preserved - // if (detailedMetadata.containsKey("customProperty2")) { - // assertEquals( - // customProperty2Value, - // detailedMetadata.get("customProperty2"), - // "Custom property should be preserved after move for attachment: " + - // targetAttachmentId); - // } else { - // fail("Custom property missing after move for attachment: " + targetAttachmentId); - // } - // } + // Add notes to attachments + String notesValue = "Test note for verification"; + MediaType mediaType = MediaType.parse("application/json"); + String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); + + for (String attachmentId : sourceAttachmentIds) { + String updateNotesResponse = + api.updateSecondaryProperty( + appUrl, entityName, facetName, moveSourceEntity, attachmentId, updateNotesBody); + if (!updateNotesResponse.equals("Updated")) { + fail("Could not update notes for attachment: " + attachmentId); + } + } - // // Verify source entity still has all attachments (without sourceFacet) - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // assertEquals( - // sourceCountBeforeMove, - // sourceMetadataAfterMove.size(), - // "Source entity should still have " - // + sourceCountBeforeMove - // + " attachments (without sourceFacet)"); - - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // Add custom property to attachments + Integer customProperty2Value = 54321; + RequestBody bodyInt = + RequestBody.create( + "{\"customProperty2\": " + customProperty2Value + "}", + MediaType.parse("application/json")); + + for (String attachmentId : sourceAttachmentIds) { + String updateCustomPropertyResponse = + api.updateSecondaryProperty( + appUrl, entityName, facetName, moveSourceEntity, attachmentId, bodyInt); + if (!updateCustomPropertyResponse.equals("Updated")) { + fail("Could not update custom property for attachment: " + attachmentId); + } + } - // @Test - // @Order(71) - // public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { - // System.out.println( - // "Test (71): Move attachments with invalid or undefined secondary properties"); + // Save source entity + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // // Create source entity and add attachments - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + // Fetch object IDs from source entity + moveObjectIds.clear(); + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (Exception e) { + fail("Could not fetch metadata for attachment: " + attachmentId); + } + } - // // Prepare sample files - // ClassLoader classLoader = getClass().getClassLoader(); - // List files = new ArrayList<>(); - // files.add(new File(classLoader.getResource("sample.pdf").getFile())); - // files.add(new File(classLoader.getResource("sample.txt").getFile())); - // files.add(new File(classLoader.getResource("WDIRSCodeList.csv").getFile())); + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", moveSourceEntity); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + // Create target entity + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // // Create attachments in source entity - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in source entity"); - // } - // } + // Save target before move + String saveTargetBeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse); + } - // // Add valid secondary properties to first attachment (customProperty2) - // String validAttachmentId = sourceAttachmentIds.get(0); - // Integer validCustomProperty2Value = 12345; - // RequestBody validPropertyBody = - // RequestBody.create( - // "{\"customProperty2\": " + validCustomProperty2Value + "}", - // MediaType.parse("application/json")); - - // String validPropertyResponse = - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, moveSourceEntity, validAttachmentId, - // validPropertyBody); - // if (!validPropertyResponse.equals("Updated")) { - // fail("Could not update valid property for attachment: " + validAttachmentId); - // } + // Move attachments from source to target with sourceFacet + String sourceFacet = serviceName + "." + entityName + "." + facetName; + String targetFacet = serviceName + "." + entityName + "." + facetName; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // // add invalid secondary properties to second attachment (non-existent property) - // String invalidAttachmentId = sourceAttachmentIds.get(1); - // RequestBody invalidPropertyBody = - // RequestBody.create( - // "{\"nonExistentProperty\": \"invalid\"}", MediaType.parse("application/json")); - - // api.updateSecondaryProperty( - // appUrl, entityName, facetName, moveSourceEntity, invalidAttachmentId, - // invalidPropertyBody); - - // // add undefined properties to third attachment - // String undefinedAttachmentId = sourceAttachmentIds.get(2); - // RequestBody undefinedPropertyBody = - // RequestBody.create( - // "{\"undefinedField\": \"test\", \"anotherUndefined\": 999}", - // MediaType.parse("application/json")); - - // api.updateSecondaryProperty( - // appUrl, - // entityName, - // facetName, - // moveSourceEntity, - // undefinedAttachmentId, - // undefinedPropertyBody); - - // // Save source entity - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + // Verify all attachments moved to target + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + assertEquals( + sourceAttachmentIds.size(), + targetMetadataAfterMove.size(), + "Target entity should have all attachments after move"); + + // Verify notes and secondary properties are preserved + for (Map metadata : targetMetadataAfterMove) { + String targetAttachmentId = (String) metadata.get("ID"); + assertNotNull(targetAttachmentId, "Target attachment ID should not be null"); + + Map detailedMetadata = + api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); + + // Verify notes are preserved + if (detailedMetadata.containsKey("note")) { + assertEquals( + notesValue, + detailedMetadata.get("note"), + "Notes should be preserved after move for attachment: " + targetAttachmentId); + } else { + fail("Notes property missing after move for attachment: " + targetAttachmentId); + } - // // Fetch object IDs from source entity - // moveObjectIds.clear(); - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (Exception e) { - // fail("Could not fetch metadata for attachment: " + attachmentId); - // } - // } + // Verify custom property is preserved + if (detailedMetadata.containsKey("customProperty2")) { + assertEquals( + customProperty2Value, + detailedMetadata.get("customProperty2"), + "Custom property should be preserved after move for attachment: " + targetAttachmentId); + } else { + fail("Custom property missing after move for attachment: " + targetAttachmentId); + } + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch all objectIds from source entity"); - // } + // Verify source entity has no attachments (all moved with sourceFacet) + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals(0, sourceMetadataAfterMove.size(), "Source entity has no attachments after move"); - // // Get source attachment count before move - // List> sourceMetadataBeforeMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); + // Clean up - delete both entities + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } - // // Create target entity - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + @Test + @Order(68) + public void testMoveAttachmentsWithoutSourceFacet() throws Exception { + System.out.println( + "Test (68): Move valid attachments from Source Entity to Target Entity without sourceFacet"); - // // Save target before move - // String saveTargetBeforeMoveResponse68 = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse68.equals("Saved")) { - // fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse68); - // } + // Create source entity and add attachments + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // // Move attachments from source to target with sourceFacet - // String sourceFacet = serviceName + "." + entityName + "." + facetName; - // String targetFacet = serviceName + "." + entityName + "." + facetName; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + // Prepare sample files + ClassLoader classLoader = getClass().getClassLoader(); + List files = new ArrayList<>(); + files.add(new File(classLoader.getResource("sample.pdf").getFile())); + files.add(new File(classLoader.getResource("sample.txt").getFile())); - // // Verify attachments moved to target - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - - // assertTrue( - // targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); - // assertEquals( - // sourceCountBeforeMove, - // targetMetadataAfterMove.size(), - // "All attachments should move (invalid properties are ignored)"); - - // // Verify only allowed properties are populated in target - // for (Map metadata : targetMetadataAfterMove) { - // String targetAttachmentId = (String) metadata.get("ID"); - // assertNotNull(targetAttachmentId, "Target attachment ID should not be null"); - - // // Fetch detailed metadata to verify properties - // Map detailedMetadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); - - // // Check if this is the attachment with valid customProperty2 - // if (detailedMetadata.containsKey("customProperty2") - // && detailedMetadata.get("customProperty2") != null) { - // assertEquals( - // validCustomProperty2Value, - // detailedMetadata.get("customProperty2"), - // "Valid customProperty2 should be preserved"); - // } - // } + Map postData = new HashMap<>(); + postData.put("up__ID", moveSourceEntity); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // Verify source entity has no attachments - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // assertEquals( - // 0, - // sourceMetadataAfterMove.size(), - // "Source entity should have no attachments after move with sourceFacet"); - - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // Create attachments in source entity + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment in source entity"); + } + } - // @Test - // @Order(72) - // public void testMoveAttachmentsFromSourceEntityInDraftMode() throws Exception { - // System.out.println( - // "Test (72): Move attachments from Source Entity when Source Entity is in draft mode"); + // Save source entity + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // // Create source entity and keep it in draft mode - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + // Fetch object IDs from source entity + moveObjectIds.clear(); + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + // Get source folder ID from first attachment + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } else { + fail("Attachment metadata does not contain objectId"); + } + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } - // // Prepare sample files - // ClassLoader classLoader = getClass().getClassLoader(); - // List files = new ArrayList<>(); - // files.add(new File(classLoader.getResource("sample.pdf").getFile())); - // files.add(new File(classLoader.getResource("sample.txt").getFile())); - // files.add(new File(classLoader.getResource("WDIRSCodeList.csv").getFile())); + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", moveSourceEntity); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + // Create target entity + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // // Create attachments in source entity - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in source entity"); - // } - // } + // Save target before move + String saveTargetBeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity before move"); + } - // // Verify attachments are added to source entity - // int sourceCountBeforeMove = sourceAttachmentIds.size(); - // assertTrue(sourceCountBeforeMove > 0, "Source entity should have attachments before move"); - // assertEquals( - // files.size(), sourceCountBeforeMove, "Source should have " + files.size() + " - // attachments"); - - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + // Move attachments without sourceFacet (pass null for sourceFacet parameter) + String targetFacet = serviceName + "." + entityName + "." + facetName; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + null); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // // Fetch object IDs from source entity - // moveObjectIds.clear(); - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // // Get source folder ID from first attachment - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } + // Verify attachments are in target entity + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + assertEquals( + moveObjectIds.size(), + targetMetadataAfterMove.size(), + "Target entity should have all moved attachments"); + + // Verify attachments can be read from target entity + for (Map metadata : targetMetadataAfterMove) { + String targetAttachmentId = (String) metadata.get("ID"); + String readResponse = + api.readAttachment(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); + if (!readResponse.equals("OK")) { + fail("Could not read moved attachment from target entity"); + } + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + // Expected Behavior: Attachments remain in source entity UI (without sourceFacet) + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals( + moveObjectIds.size(), + sourceMetadataAfterMove.size(), + "Source entity should still have attachments in UI when sourceFacet is not specified"); - // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // Verify the same objectIds are still visible in source + for (Map metadata : sourceMetadataAfterMove) { + String objectId = (String) metadata.get("objectId"); + assertTrue( + moveObjectIds.contains(objectId), + "Source entity should still show attachment with objectId: " + objectId); + } - // String editSourceResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!editSourceResponse.equals("Entity in draft mode")) { - // fail("Could not edit source entity back to draft mode"); - // } + // Clean up - delete both entities + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } - // // Create target entity - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + @Test + @Order(69) + public void testMoveAttachmentsToEntityWithDuplicateWithoutSourceFacet() throws Exception { + System.out.println( + "Test (69): Move attachments into existing Target Entity when duplicate exists without sourceFacet"); - // // Save target before move - // String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveTargetEntity); - // if (!saveTargetResponse.equals("Saved")) { - // fail("Could not save target entity: " + saveTargetResponse); - // } + // Create source entity and add attachments + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // // Move attachments from draft source to target using sourceFacet - // String targetFacet = serviceName + "." + entityName + "." + facetName; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // null); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + // Prepare sample files + ClassLoader classLoader = getClass().getClassLoader(); + List files = new ArrayList<>(); + files.add(new File(classLoader.getResource("sample.pdf").getFile())); + files.add(new File(classLoader.getResource("sample.txt").getFile())); - // // Verify attachments moved to target - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // assertTrue( - // targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); - // assertEquals( - // sourceCountBeforeMove, - // targetMetadataAfterMove.size(), - // "Target should have " + sourceCountBeforeMove + " attachments after move"); - - // // Verify all expected attachments are in target - // Set targetFileNames = - // targetMetadataAfterMove.stream() - // .map(m -> (String) m.get("fileName")) - // .collect(java.util.stream.Collectors.toSet()); - - // for (File file : files) { - // assertTrue( - // targetFileNames.contains(file.getName()), - // "Target should contain attachment: " + file.getName()); - // } + Map postData = new HashMap<>(); + postData.put("up__ID", moveSourceEntity); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // Now save the source entity - // String saveSourceAfterMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceAfterMoveResponse.equals("Saved")) { - // fail("Could not save source entity after move: " + saveSourceAfterMoveResponse); - // } + // Create attachments in source entity + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment in source entity"); + } + } - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // assertEquals( - // sourceCountBeforeMove, - // sourceMetadataAfterMove.size(), - // "Source entity in draft mode retains attachments after move (copy behavior)"); - - // Set sourceFileNamesAfterMove = - // sourceMetadataAfterMove.stream() - // .map(m -> (String) m.get("fileName")) - // .collect(java.util.stream.Collectors.toSet()); - - // for (File file : files) { - // assertTrue( - // sourceFileNamesAfterMove.contains(file.getName()), - // "Source (draft) should still contain attachment: " + file.getName()); - // } + // Save source entity + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // Fetch object IDs from source entity + moveObjectIds.clear(); + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + // Get source folder ID from first attachment + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } else { + fail("Attachment metadata does not contain objectId"); + } + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } + } - // @Test - // @Order(73) - // public void testEditAttachmentFileNameAndMoveToTarget() throws Exception { - // System.out.println( - // "Test (73): Edit attachment file name in Source Entity and move it to Target Entity"); + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } - // // Create source entity and add attachment - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + // Create target entity and add duplicate attachment (sample.pdf) + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // // Add attachment with original name (sample.txt) - // ClassLoader classLoader = getClass().getClassLoader(); - // File originalFile = new File(classLoader.getResource("sample.txt").getFile()); + // Add the same first file (sample.pdf) to target entity to create duplicate + Map targetPostData = new HashMap<>(); + targetPostData.put("up__ID", moveTargetEntity); + targetPostData.put("mimeType", "application/pdf"); + targetPostData.put("createdAt", new Date().toString()); + targetPostData.put("createdBy", "test@test.com"); + targetPostData.put("modifiedBy", "test@test.com"); - // Map postData = new HashMap<>(); - // postData.put("up__ID", moveSourceEntity); - // postData.put("mimeType", "text/plain"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + List createTargetResponse = + api.createAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity, + srvpath, + targetPostData, + files.get(0)); // Add same file (sample.pdf) + if (!createTargetResponse.get(0).equals("Attachment created")) { + fail("Could not create duplicate attachment in target entity"); + } - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, originalFile); - // if (!createResponse.get(0).equals("Attachment created")) { - // fail("Could not create attachment in source entity"); - // } + // Save target entity before move + String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity: " + saveTargetResponse); + } - // String attachmentId = createResponse.get(1); - // assertNotNull(attachmentId, "Attachment ID should not be null"); + // Get initial target metadata count + List> targetMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + int initialTargetCount = targetMetadataBeforeMove.size(); - // // Save source entity - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + // Step 3: Move attachments without sourceFacet (duplicate should be skipped) + String targetFacet = serviceName + "." + entityName + "." + facetName; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + null); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // // Verify original filename - // List> metadataBeforeRename = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // assertEquals(1, metadataBeforeRename.size(), "Source should have 1 attachment"); - // assertEquals( - // "sample.txt", - // metadataBeforeRename.get(0).get("fileName"), - // "Original filename should be sample.txt"); - - // // Edit source entity back to draft mode - // String editSourceResponse = api.editEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!editSourceResponse.equals("Entity in draft mode")) { - // fail("Could not edit source entity to draft mode"); - // } + // Expected Behavior - Verify duplicate was skipped, other attachments moved + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // // Rename the attachment to testEdited.txt - // String newFileName = "testEdited.txt"; - // String renameResponse = - // api.renameAttachment( - // appUrl, entityName, facetName, moveSourceEntity, attachmentId, newFileName); - // assertEquals("Renamed", renameResponse, "Attachment should be renamed successfully"); - - // // Save source entity after rename - // saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity after rename: " + saveSourceResponse); - // } + int nonDuplicateCount = moveObjectIds.size() - 1; + int expectedTargetCount = initialTargetCount + nonDuplicateCount; - // // Verify renamed filename in source - // List> metadataAfterRename = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // assertEquals(1, metadataAfterRename.size(), "Source should still have 1 attachment"); - // assertEquals( - // newFileName, - // metadataAfterRename.get(0).get("fileName"), - // "Filename should be updated to " + newFileName); - - // // Get objectId and folderId for move operation - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - // String objectId = metadata.get("objectId").toString(); - // moveSourceFolderId = metadata.get("folderId").toString(); - // assertNotNull(objectId, "Object ID should not be null"); - // assertNotNull(moveSourceFolderId, "Folder ID should not be null"); - - // moveObjectIds.clear(); - // moveObjectIds.add(objectId); - - // // Create target entity - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity"); - // } + assertEquals( + expectedTargetCount, + targetMetadataAfterMove.size(), + "Target entity should have initial attachments plus non-duplicate moved attachments"); + + // Verify at least one non-duplicate attachment was moved + assertTrue( + targetMetadataAfterMove.size() > initialTargetCount, + "Target should have more attachments after move (non-duplicates added)"); + + // Verify all attachments still remain in source entity UI (without sourceFacet) + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals( + moveObjectIds.size(), + sourceMetadataAfterMove.size(), + "Source entity should still have all attachments in UI when sourceFacet is not specified"); + + // Verify all original objectIds are still visible in source + List sourceObjectIds = new ArrayList<>(); + for (Map metadata : sourceMetadataAfterMove) { + sourceObjectIds.add((String) metadata.get("objectId")); + } + for (String objectId : moveObjectIds) { + assertTrue( + sourceObjectIds.contains(objectId), + "Source entity should still show attachment with objectId: " + objectId); + } - // // Save target before move - // String saveTargetBeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTargetBeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity before move"); - // } + // Clean up - delete both entities + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } - // // Move attachment from source to target with sourceFacet - // String sourceFacet = serviceName + "." + entityName + "." + facetName; - // String targetFacet = serviceName + "." + entityName + "." + facetName; - // Map moveResult = - // api.moveAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // fail("Move operation returned null result"); - // } + @Test + @Order(70) + public void testMoveAttachmentsWithNotesAndSecondaryPropertiesWithoutSourceFacet() + throws Exception { + System.out.println( + "Test (70): Move attachments with notes and secondary properties without sourceFacet"); - // // Verify attachment moved to target with renamed filename - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // assertEquals(1, targetMetadataAfterMove.size(), "Target should have 1 attachment after - // move"); - // assertEquals( - // newFileName, - // targetMetadataAfterMove.get(0).get("fileName"), - // "Target should have attachment with renamed filename: " + newFileName); - - // // Verify attachment removed from source - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // assertEquals( - // 0, - // sourceMetadataAfterMove.size(), - // "Source entity should have no attachments after move with sourceFacet"); - - // // Clean up - delete both entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // Create source entity and add attachments + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // @Test - // @Order(74) - // public void testChainMoveAttachmentsFromSourceToTarget1ToTarget2() throws Exception { - // System.out.println( - // "Test (74): Move attachments from Source Entity to Target Entity 1 and then to Target - // Entity 2"); + // Prepare sample files + ClassLoader classLoader = getClass().getClassLoader(); + List files = new ArrayList<>(); + files.add(new File(classLoader.getResource("sample.pdf").getFile())); + files.add(new File(classLoader.getResource("sample.txt").getFile())); - // // Create source entity and add attachments - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + Map postData = new HashMap<>(); + postData.put("up__ID", moveSourceEntity); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // Prepare sample files - // ClassLoader classLoader = getClass().getClassLoader(); - // List files = new ArrayList<>(); - // files.add(new File(classLoader.getResource("sample.pdf").getFile())); - // files.add(new File(classLoader.getResource("sample.txt").getFile())); + // Create attachments in source entity + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment in source entity"); + } + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", moveSourceEntity); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + // Add notes to attachments + String notesValue = "Test note for migration verification"; + MediaType mediaType = MediaType.parse("application/json"); + String jsonPayload = "{\"note\": \"" + notesValue + "\"}"; + RequestBody updateNotesBody = RequestBody.create(jsonPayload, mediaType); + + for (String attachmentId : sourceAttachmentIds) { + String updateNotesResponse = + api.updateSecondaryProperty( + appUrl, entityName, facetName, moveSourceEntity, attachmentId, updateNotesBody); + if (!updateNotesResponse.equals("Updated")) { + fail("Could not update notes for attachment: " + attachmentId); + } + } - // // Create attachments in source entity - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in source entity"); - // } - // } + // Add custom property to attachments + Integer customProperty2Value = 54321; + RequestBody bodyInt = + RequestBody.create( + "{\"customProperty2\": " + customProperty2Value + "}", + MediaType.parse("application/json")); + + for (String attachmentId : sourceAttachmentIds) { + String updateCustomPropertyResponse = + api.updateSecondaryProperty( + appUrl, entityName, facetName, moveSourceEntity, attachmentId, bodyInt); + if (!updateCustomPropertyResponse.equals("Updated")) { + fail("Could not update custom property for attachment: " + attachmentId); + } + } - // // Save source entity - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + // Save source entity + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // // Get count of attachments in source - // int sourceCountInitial = sourceAttachmentIds.size(); - // assertTrue(sourceCountInitial > 0, "Source should have attachments"); + // Fetch object IDs from source entity + moveObjectIds.clear(); + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (Exception e) { + fail("Could not fetch metadata for attachment: " + attachmentId); + } + } - // // Fetch object IDs from source entity - // moveObjectIds.clear(); - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // // Get source folder ID from first attachment - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + // Get source attachment count before move + List> sourceMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // Create target entity + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // // Create Target Entity 1 - // moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity 1"); - // } + // Save target before move + String saveTargetBeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity before move"); + } - // // Save target1 before move - // String saveTarget1BeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); - // if (!saveTarget1BeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity 1 before move"); - // } + // Get target attachment count before move + List> targetMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + int targetCountBeforeMove = targetMetadataBeforeMove.size(); - // // Move attachments from source to Target Entity 1 with sourceFacet - // String sourceFacet = serviceName + "." + entityName + "." + facetName; - // String targetFacet = serviceName + "." + entityName + "." + facetName; - // Map moveResult1 = - // api.moveAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult1 == null) { - // fail("Move operation from source to target 1 returned null result"); - // } + // Move attachments from source to target WITHOUT sourceFacet + String targetFacet = serviceName + "." + entityName + "." + facetName; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + null); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // // Verify attachments moved to Target Entity 1 - // List> target1MetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // assertTrue( - // target1MetadataAfterMove.size() > 0, "Target entity 1 should have attachments after - // move"); - // assertEquals( - // sourceCountInitial, - // target1MetadataAfterMove.size(), - // "Target 1 should have " + sourceCountInitial + " attachments"); - - // // Verify all expected files are in Target Entity 1 - // Set target1FileNames = - // target1MetadataAfterMove.stream() - // .map(m -> (String) m.get("fileName")) - // .collect(java.util.stream.Collectors.toSet()); - - // for (File file : files) { - // assertTrue( - // target1FileNames.contains(file.getName()), - // "Target 1 should contain attachment: " + file.getName()); - // } + // Verify expected number of attachments moved to target + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + int expectedTargetCount = targetCountBeforeMove + sourceAttachmentIds.size(); + assertEquals( + expectedTargetCount, + targetMetadataAfterMove.size(), + "Target entity should have " + expectedTargetCount + " attachments after move"); + + // Verify notes and secondary properties are preserved + for (Map metadata : targetMetadataAfterMove) { + String targetAttachmentId = (String) metadata.get("ID"); + assertNotNull(targetAttachmentId, "Target attachment ID should not be null"); + + Map detailedMetadata = + api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); + + // Verify notes are preserved + if (detailedMetadata.containsKey("note")) { + assertEquals( + notesValue, + detailedMetadata.get("note"), + "Notes should be preserved after move for attachment: " + targetAttachmentId); + } else { + fail("Notes property missing after move for attachment: " + targetAttachmentId); + } - // // Verify attachments removed from source - // List> sourceMetadataAfterFirstMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // assertEquals( - // 0, - // sourceMetadataAfterFirstMove.size(), - // "Source entity should have no attachments after move to target 1"); - - // // Create Target Entity 2 - // String moveTargetEntity2 = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity2.equals("Could not create entity")) { - // fail("Could not create target entity 2"); - // } + // Verify custom property is preserved + if (detailedMetadata.containsKey("customProperty2")) { + assertEquals( + customProperty2Value, + detailedMetadata.get("customProperty2"), + "Custom property should be preserved after move for attachment: " + targetAttachmentId); + } else { + fail("Custom property missing after move for attachment: " + targetAttachmentId); + } + } - // // Save target2 before move - // String saveTarget2BeforeMoveResponse = - // api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity2); - // if (!saveTarget2BeforeMoveResponse.equals("Saved")) { - // fail("Could not save target entity 2 before move"); - // } + // Verify source entity still has all attachments (without sourceFacet) + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals( + sourceCountBeforeMove, + sourceMetadataAfterMove.size(), + "Source entity should still have " + + sourceCountBeforeMove + + " attachments (without sourceFacet)"); + + // Clean up - delete both entities + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } - // // Get new object IDs and folder ID from Target Entity 1 for second move - // List target1AttachmentIds = new ArrayList<>(); - // for (Map metadata : target1MetadataAfterMove) { - // String attachmentId = metadata.get("ID").toString(); - // target1AttachmentIds.add(attachmentId); - // } + @Test + @Order(71) + public void testMoveAttachmentsWithInvalidOrUndefinedSecondaryProperties() throws Exception { + System.out.println( + "Test (71): Move attachments with invalid or undefined secondary properties"); - // moveObjectIds.clear(); - // String target1FolderId = null; - // for (String attachmentId : target1AttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // // Get folder ID from first attachment - // if (target1FolderId == null && metadata.containsKey("folderId")) { - // target1FolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (IOException e) { - // fail("Could not fetch attachment metadata from target 1: " + e.getMessage()); - // } - // } + // Create source entity and add attachments + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); + } - // assertNotNull(target1FolderId, "Target 1 folder ID should not be null"); - - // // Move attachments from Target Entity 1 to Target Entity 2 with sourceFacet - // Map moveResult2 = - // api.moveAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity2, - // target1FolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult2 == null) { - // fail("Move operation from target 1 to target 2 returned null result"); - // } + // Prepare sample files + ClassLoader classLoader = getClass().getClassLoader(); + List files = new ArrayList<>(); + files.add(new File(classLoader.getResource("sample.pdf").getFile())); + files.add(new File(classLoader.getResource("sample.txt").getFile())); + files.add(new File(classLoader.getResource("WDIRSCodeList.csv").getFile())); - // // Verify attachments moved to Target Entity 2 - // List> target2MetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity2); - // assertTrue( - // target2MetadataAfterMove.size() > 0, "Target entity 2 should have attachments after - // move"); - // assertEquals( - // sourceCountInitial, - // target2MetadataAfterMove.size(), - // "Target 2 should have " + sourceCountInitial + " attachments"); - - // // Verify all expected files are in Target Entity 2 - // Set target2FileNames = - // target2MetadataAfterMove.stream() - // .map(m -> (String) m.get("fileName")) - // .collect(java.util.stream.Collectors.toSet()); - - // for (File file : files) { - // assertTrue( - // target2FileNames.contains(file.getName()), - // "Target 2 should contain attachment: " + file.getName()); - // } + Map postData = new HashMap<>(); + postData.put("up__ID", moveSourceEntity); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); - // // Verify attachments removed from Target Entity 1 - // List> target1MetadataAfterSecondMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // assertEquals( - // 0, - // target1MetadataAfterSecondMove.size(), - // "Target entity 1 should have no attachments after move to target 2"); - - // // Clean up - delete all three entities - // api.deleteEntity(appUrl, entityName, moveTargetEntity2); - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // Create attachments in source entity + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment in source entity"); + } + } - // @Test - // @Order(75) - // public void testMoveAttachmentsWithoutSDMRole() throws Exception { - // System.out.println("Test (75): Move attachments when user does not have SDM Role"); - - // // Create source entity with SDM role and add attachments - // moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveSourceEntity.equals("Could not create entity")) { - // fail("Could not create source entity"); - // } + // Add valid secondary properties to first attachment (customProperty2) + String validAttachmentId = sourceAttachmentIds.get(0); + Integer validCustomProperty2Value = 12345; + RequestBody validPropertyBody = + RequestBody.create( + "{\"customProperty2\": " + validCustomProperty2Value + "}", + MediaType.parse("application/json")); + + String validPropertyResponse = + api.updateSecondaryProperty( + appUrl, entityName, facetName, moveSourceEntity, validAttachmentId, validPropertyBody); + if (!validPropertyResponse.equals("Updated")) { + fail("Could not update valid property for attachment: " + validAttachmentId); + } - // // Prepare sample files - // ClassLoader classLoader = getClass().getClassLoader(); - // List files = new ArrayList<>(); - // files.add(new File(classLoader.getResource("sample.pdf").getFile())); - // files.add(new File(classLoader.getResource("sample.txt").getFile())); + // add invalid secondary properties to second attachment (non-existent property) + String invalidAttachmentId = sourceAttachmentIds.get(1); + RequestBody invalidPropertyBody = + RequestBody.create( + "{\"nonExistentProperty\": \"invalid\"}", MediaType.parse("application/json")); + + api.updateSecondaryProperty( + appUrl, entityName, facetName, moveSourceEntity, invalidAttachmentId, invalidPropertyBody); + + // add undefined properties to third attachment + String undefinedAttachmentId = sourceAttachmentIds.get(2); + RequestBody undefinedPropertyBody = + RequestBody.create( + "{\"undefinedField\": \"test\", \"anotherUndefined\": 999}", + MediaType.parse("application/json")); + + api.updateSecondaryProperty( + appUrl, + entityName, + facetName, + moveSourceEntity, + undefinedAttachmentId, + undefinedPropertyBody); + + // Save source entity + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); + } - // Map postData = new HashMap<>(); - // postData.put("up__ID", moveSourceEntity); - // postData.put("mimeType", "application/pdf"); - // postData.put("createdAt", new Date().toString()); - // postData.put("createdBy", "test@test.com"); - // postData.put("modifiedBy", "test@test.com"); + // Fetch object IDs from source entity + moveObjectIds.clear(); + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (Exception e) { + fail("Could not fetch metadata for attachment: " + attachmentId); + } + } - // // Create attachments in source entity with SDM role - // List sourceAttachmentIds = new ArrayList<>(); - // for (File file : files) { - // List createResponse = - // api.createAttachment( - // appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); - // if (createResponse.get(0).equals("Attachment created")) { - // sourceAttachmentIds.add(createResponse.get(1)); - // } else { - // fail("Could not create attachment in source entity"); - // } - // } + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch all objectIds from source entity"); + } - // // Save source entity with SDM role - // String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, - // moveSourceEntity); - // if (!saveSourceResponse.equals("Saved")) { - // fail("Could not save source entity: " + saveSourceResponse); - // } + // Get source attachment count before move + List> sourceMetadataBeforeMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + int sourceCountBeforeMove = sourceMetadataBeforeMove.size(); - // // Get count of attachments in source - // int sourceCountInitial = sourceAttachmentIds.size(); - // assertTrue(sourceCountInitial > 0, "Source should have attachments"); + // Create target entity + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } - // // Fetch object IDs from source entity - // moveObjectIds.clear(); - // for (String attachmentId : sourceAttachmentIds) { - // try { - // Map metadata = - // api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); - // if (metadata.containsKey("objectId")) { - // moveObjectIds.add(metadata.get("objectId").toString()); - // // Get source folder ID from first attachment - // if (moveSourceFolderId == null && metadata.containsKey("folderId")) { - // moveSourceFolderId = metadata.get("folderId").toString(); - // } - // } - // } catch (IOException e) { - // fail("Could not fetch attachment metadata: " + e.getMessage()); - // } - // } + // Save target before move + String saveTargetBeforeMoveResponse68 = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse68.equals("Saved")) { + fail("Could not save target entity before move: " + saveTargetBeforeMoveResponse68); + } - // if (moveObjectIds.size() != sourceAttachmentIds.size()) { - // fail("Could not fetch object IDs for all attachments"); - // } + // Move attachments from source to target with sourceFacet + String sourceFacet = serviceName + "." + entityName + "." + facetName; + String targetFacet = serviceName + "." + entityName + "." + facetName; + Map moveResult = + api.moveAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null result"); + } - // assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + // Verify attachments moved to target + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // // Create target entity with no SDM role - // moveTargetEntity = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); - // if (moveTargetEntity.equals("Could not create entity")) { - // fail("Could not create target entity with no SDM role"); - // } + assertTrue( + targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); + assertEquals( + sourceCountBeforeMove, + targetMetadataAfterMove.size(), + "All attachments should move (invalid properties are ignored)"); + + // Verify only allowed properties are populated in target + for (Map metadata : targetMetadataAfterMove) { + String targetAttachmentId = (String) metadata.get("ID"); + assertNotNull(targetAttachmentId, "Target attachment ID should not be null"); + + // Fetch detailed metadata to verify properties + Map detailedMetadata = + api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, targetAttachmentId); + + // Check if this is the attachment with valid customProperty2 + if (detailedMetadata.containsKey("customProperty2") + && detailedMetadata.get("customProperty2") != null) { + assertEquals( + validCustomProperty2Value, + detailedMetadata.get("customProperty2"), + "Valid customProperty2 should be preserved"); + } + } - // // Try to move attachments from source to target using user without SDM role - // String sourceFacet = serviceName + "." + entityName + "." + facetName; - // String targetFacet = serviceName + "." + entityName + "." + facetName; - // Map moveResult = null; - // boolean moveOperationFailed = false; - // String errorMessage = null; - - // try { - // moveResult = - // apiNoRoles.moveAttachment( - // appUrl, - // entityName, - // facetName, - // moveTargetEntity, - // moveSourceFolderId, - // moveObjectIds, - // targetFacet, - // sourceFacet); - - // if (moveResult == null) { - // moveOperationFailed = true; - // errorMessage = "Move operation returned null"; - // } else if (moveResult.containsKey("error")) { - // moveOperationFailed = true; - // errorMessage = moveResult.get("error").toString(); - // } - // } catch (Exception e) { - // moveOperationFailed = true; - // errorMessage = e.getMessage(); - // } + // Verify source entity has no attachments + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals( + 0, + sourceMetadataAfterMove.size(), + "Source entity should have no attachments after move with sourceFacet"); - // // Verify move operation failed - // assertTrue(moveOperationFailed, "Move operation should fail when user does not have SDM - // role"); - // assertNotNull(errorMessage, "Error message should be present when move operation fails"); - // System.out.println("Move operation failed as expected. Error: " + errorMessage); - - // // Verify attachments are still in source entity (not moved) - // List> sourceMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); - // assertEquals( - // sourceCountInitial, - // sourceMetadataAfterMove.size(), - // "Source should still have all attachments after failed move"); - - // // Verify target entity has no attachments - // List> targetMetadataAfterMove = - // api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); - // assertEquals( - // 0, targetMetadataAfterMove.size(), "Target should have no attachments after failed - // move"); - - // // Clean up - delete both entities using SDM role - // api.deleteEntity(appUrl, entityName, moveTargetEntity); - // api.deleteEntity(appUrl, entityName, moveSourceEntity); - // } + // Clean up - delete both entities + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); + } @Test - @Order(76) - void testDownloadMultipleAttachments() throws IOException { + @Order(72) + public void testMoveAttachmentsFromSourceEntityInDraftMode() throws Exception { System.out.println( - "Test (76): Create entity, upload 3 attachments (pdf, txt, exe), and download all"); - boolean testStatus = false; + "Test (72): Move attachments from Source Entity when Source Entity is in draft mode"); - // Step 1: Create entity - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (response.equals("Could not create entity")) { - fail("Could not create entity"); - return; + // Create source entity and keep it in draft mode + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); } - String downloadTestEntityID = response; + // Prepare sample files ClassLoader classLoader = getClass().getClassLoader(); + List files = new ArrayList<>(); + files.add(new File(classLoader.getResource("sample.pdf").getFile())); + files.add(new File(classLoader.getResource("sample.txt").getFile())); + files.add(new File(classLoader.getResource("WDIRSCodeList.csv").getFile())); - // Step 2: Upload pdf, txt, exe in one draft session Map postData = new HashMap<>(); - postData.put("up__ID", downloadTestEntityID); + postData.put("up__ID", moveSourceEntity); + postData.put("mimeType", "application/pdf"); postData.put("createdAt", new Date().toString()); postData.put("createdBy", "test@test.com"); postData.put("modifiedBy", "test@test.com"); - // Upload pdf - postData.put("mimeType", "application/pdf"); - File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); - List createResponse1 = - api.createAttachment( - appUrl, entityName, facetName, downloadTestEntityID, srvpath, postData, pdfFile); - if (!createResponse1.get(0).equals("Attachment created")) { - api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); - fail("Could not upload sample.pdf"); - return; + // Create attachments in source entity + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment in source entity"); + } } - String downloadAttachmentID1 = createResponse1.get(1); - // Upload txt - postData.put("mimeType", "application/txt"); - File txtFile = new File(classLoader.getResource("sample.txt").getFile()); - List createResponse2 = - api.createAttachment( - appUrl, entityName, facetName, downloadTestEntityID, srvpath, postData, txtFile); - if (!createResponse2.get(0).equals("Attachment created")) { - api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); - fail("Could not upload sample.txt"); - return; + // Verify attachments are added to source entity + int sourceCountBeforeMove = sourceAttachmentIds.size(); + assertTrue(sourceCountBeforeMove > 0, "Source entity should have attachments before move"); + assertEquals( + files.size(), sourceCountBeforeMove, "Source should have " + files.size() + " attachments"); + + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); } - String downloadAttachmentID2 = createResponse2.get(1); - // Upload exe - postData.put("mimeType", "application/exe"); - File exeFile = new File(classLoader.getResource("sample.exe").getFile()); - List createResponse3 = - api.createAttachment( - appUrl, entityName, facetName, downloadTestEntityID, srvpath, postData, exeFile); - if (!createResponse3.get(0).equals("Attachment created")) { - api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); - fail("Could not upload sample.exe"); - return; + // Fetch object IDs from source entity + moveObjectIds.clear(); + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + // Get source folder ID from first attachment + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } } - String downloadAttachmentID3 = createResponse3.get(1); - // Step 3: Save entity draft - response = api.saveEntityDraft(appUrl, entityName, srvpath, downloadTestEntityID); - if (!response.equals("Saved")) { - api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); - fail("Could not save entity draft: " + response); - return; + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); } - // Step 4: Select first attachment - Download button should be enabled - // Verify download works with a single attachment selection - String singleDownloadResult = - api.downloadSelectedAttachments( - appUrl, entityName, facetName, downloadTestEntityID, List.of(downloadAttachmentID1)); - JSONArray singleResultArray = new JSONArray(singleDownloadResult); - assertEquals(1, singleResultArray.length(), "Expected 1 result in download response"); - JSONObject singleResult = singleResultArray.getJSONObject(0); - assertEquals( - "success", - singleResult.getString("status"), - "Download button should be enabled: single attachment download should succeed"); - assertTrue(singleResult.has("content"), "Downloaded attachment should have a content field"); - - // Step 5: Select all 3 and click download - String multiDownloadResult = - api.downloadSelectedAttachments( + assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + + String editSourceResponse = api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!editSourceResponse.equals("Entity in draft mode")) { + fail("Could not edit source entity back to draft mode"); + } + + // Create target entity + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } + + // Save target before move + String saveTargetResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetResponse.equals("Saved")) { + fail("Could not save target entity: " + saveTargetResponse); + } + + // Move attachments from draft source to target using sourceFacet + String targetFacet = serviceName + "." + entityName + "." + facetName; + Map moveResult = + api.moveAttachment( appUrl, entityName, facetName, - downloadTestEntityID, - List.of(downloadAttachmentID1, downloadAttachmentID2, downloadAttachmentID3)); - JSONArray multiResultArray = new JSONArray(multiDownloadResult); - assertEquals(3, multiResultArray.length(), "Expected 3 results in download response"); - for (int i = 0; i < multiResultArray.length(); i++) { - JSONObject result = multiResultArray.getJSONObject(i); - assertEquals( - "success", - result.getString("status"), - "Attachment " + (i + 1) + " should download successfully"); + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + null); + + if (moveResult == null) { + fail("Move operation returned null result"); + } + + // Verify attachments moved to target + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + assertTrue( + targetMetadataAfterMove.size() > 0, "Target entity should have attachments after move"); + assertEquals( + sourceCountBeforeMove, + targetMetadataAfterMove.size(), + "Target should have " + sourceCountBeforeMove + " attachments after move"); + + // Verify all expected attachments are in target + Set targetFileNames = + targetMetadataAfterMove.stream() + .map(m -> (String) m.get("fileName")) + .collect(java.util.stream.Collectors.toSet()); + + for (File file : files) { assertTrue( - result.has("content"), - "Attachment " + (i + 1) + " should have a content field in the response"); + targetFileNames.contains(file.getName()), + "Target should contain attachment: " + file.getName()); } - testStatus = true; - // Clean up - api.deleteEntity(appUrl, entityName, downloadTestEntityID); + // Now save the source entity + String saveSourceAfterMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceAfterMoveResponse.equals("Saved")) { + fail("Could not save source entity after move: " + saveSourceAfterMoveResponse); + } - if (!testStatus) { - fail("Multiple attachment download test failed"); + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals( + sourceCountBeforeMove, + sourceMetadataAfterMove.size(), + "Source entity in draft mode retains attachments after move (copy behavior)"); + + Set sourceFileNamesAfterMove = + sourceMetadataAfterMove.stream() + .map(m -> (String) m.get("fileName")) + .collect(java.util.stream.Collectors.toSet()); + + for (File file : files) { + assertTrue( + sourceFileNamesAfterMove.contains(file.getName()), + "Source (draft) should still contain attachment: " + file.getName()); } + + // Clean up - delete both entities + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); } @Test - @Order(77) - void testDownloadButtonDisabledWhenLinkSelected() throws IOException { + @Order(73) + public void testEditAttachmentFileNameAndMoveToTarget() throws Exception { System.out.println( - "Test (77): Download button enabled for pdf only; disabled when link is also selected"); + "Test (73): Edit attachment file name in Source Entity and move it to Target Entity"); - // Step 1: Create entity (already in draft mode) - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (response.equals("Could not create entity")) { - fail("Could not create entity"); - return; + // Create source entity and add attachment + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); } - String testEntityID = response; + // Add attachment with original name (sample.txt) ClassLoader classLoader = getClass().getClassLoader(); + File originalFile = new File(classLoader.getResource("sample.txt").getFile()); - // Step 2: Upload one pdf attachment (entity is already in draft mode) Map postData = new HashMap<>(); - postData.put("up__ID", testEntityID); - postData.put("mimeType", "application/pdf"); + postData.put("up__ID", moveSourceEntity); + postData.put("mimeType", "text/plain"); postData.put("createdAt", new Date().toString()); postData.put("createdBy", "test@test.com"); postData.put("modifiedBy", "test@test.com"); - File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); List createResponse = api.createAttachment( - appUrl, entityName, facetName, testEntityID, srvpath, postData, pdfFile); + appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, originalFile); if (!createResponse.get(0).equals("Attachment created")) { - api.deleteEntityDraft(appUrl, entityName, testEntityID); - fail("Could not upload sample.pdf"); - return; + fail("Could not create attachment in source entity"); } - String pdfAttachmentID = createResponse.get(1); - // Step 3: Create a link attachment (entity still in draft mode) - String linkResponse = - api.createLink( - appUrl, entityName, facetName, testEntityID, "TestLink", "https://www.example.com"); - if (!linkResponse.equals("Link created successfully")) { - api.deleteEntityDraft(appUrl, entityName, testEntityID); - fail("Could not create link attachment"); - return; + String attachmentId = createResponse.get(1); + assertNotNull(attachmentId, "Attachment ID should not be null"); + + // Save source entity + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); } - // Save entity draft - response = api.saveEntityDraft(appUrl, entityName, srvpath, testEntityID); - if (!response.equals("Saved")) { - api.deleteEntityDraft(appUrl, entityName, testEntityID); - fail("Could not save entity draft: " + response); - return; + // Verify original filename + List> metadataBeforeRename = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals(1, metadataBeforeRename.size(), "Source should have 1 attachment"); + assertEquals( + "sample.txt", + metadataBeforeRename.get(0).get("fileName"), + "Original filename should be sample.txt"); + + // Edit source entity back to draft mode + String editSourceResponse = api.editEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!editSourceResponse.equals("Entity in draft mode")) { + fail("Could not edit source entity to draft mode"); } - // Fetch metadata to find the link attachment ID (mimeType = "application/internet-shortcut") - List> allAttachments = - api.fetchEntityMetadata(appUrl, entityName, facetName, testEntityID); - String linkAttachmentID = - allAttachments.stream() - .filter( - a -> "application/internet-shortcut".equalsIgnoreCase((String) a.get("mimeType"))) - .map(a -> (String) a.get("ID")) - .findFirst() - .orElse(null); - if (linkAttachmentID == null) { - api.deleteEntity(appUrl, entityName, testEntityID); - fail("Could not find link attachment in entity metadata"); - return; + // Rename the attachment to testEdited.txt + String newFileName = "testEdited.txt"; + String renameResponse = + api.renameAttachment( + appUrl, entityName, facetName, moveSourceEntity, attachmentId, newFileName); + assertEquals("Renamed", renameResponse, "Attachment should be renamed successfully"); + + // Save source entity after rename + saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity after rename: " + saveSourceResponse); } - // Step 4: Select only the pdf → Download button should be enabled (succeeds) - String pdfOnlyResult = - api.downloadSelectedAttachments( - appUrl, entityName, facetName, testEntityID, List.of(pdfAttachmentID)); - JSONArray pdfOnlyArray = new JSONArray(pdfOnlyResult); - assertEquals(1, pdfOnlyArray.length(), "Expected 1 result when only pdf is selected"); + // Verify renamed filename in source + List> metadataAfterRename = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals(1, metadataAfterRename.size(), "Source should still have 1 attachment"); assertEquals( - "success", - pdfOnlyArray.getJSONObject(0).getString("status"), - "Download button should be enabled: pdf-only download should succeed"); - - // Step 5: Select both pdf and link → Download button should be disabled - // (link attachment returns error status, disabling the download) - String mixedResult = - api.downloadSelectedAttachments( + newFileName, + metadataAfterRename.get(0).get("fileName"), + "Filename should be updated to " + newFileName); + + // Get objectId and folderId for move operation + Map metadata = + api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + String objectId = metadata.get("objectId").toString(); + moveSourceFolderId = metadata.get("folderId").toString(); + assertNotNull(objectId, "Object ID should not be null"); + assertNotNull(moveSourceFolderId, "Folder ID should not be null"); + + moveObjectIds.clear(); + moveObjectIds.add(objectId); + + // Create target entity + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity"); + } + + // Save target before move + String saveTargetBeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTargetBeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity before move"); + } + + // Move attachment from source to target with sourceFacet + String sourceFacet = serviceName + "." + entityName + "." + facetName; + String targetFacet = serviceName + "." + entityName + "." + facetName; + Map moveResult = + api.moveAttachment( appUrl, entityName, facetName, - testEntityID, - List.of(pdfAttachmentID, linkAttachmentID)); - JSONArray mixedArray = new JSONArray(mixedResult); - assertEquals(2, mixedArray.length(), "Expected 2 results when pdf and link are selected"); - - // Find the result for the link attachment and assert it has error status - JSONObject linkResult = null; - for (int i = 0; i < mixedArray.length(); i++) { - JSONObject item = mixedArray.getJSONObject(i); - if (linkAttachmentID.equals(item.getString("id"))) { - linkResult = item; - break; - } + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + fail("Move operation returned null result"); } - assertNotNull(linkResult, "Result for link attachment should be present"); + + // Verify attachment moved to target with renamed filename + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + assertEquals(1, targetMetadataAfterMove.size(), "Target should have 1 attachment after move"); assertEquals( - "error", - linkResult.getString("status"), - "Download button should be disabled: link attachment download should return error"); + newFileName, + targetMetadataAfterMove.get(0).get("fileName"), + "Target should have attachment with renamed filename: " + newFileName); + + // Verify attachment removed from source + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); assertEquals( - "Download is not supported for link attachments", - linkResult.getString("message"), - "Error message for link attachment download should match"); + 0, + sourceMetadataAfterMove.size(), + "Source entity should have no attachments after move with sourceFacet"); - // Clean up - api.deleteEntity(appUrl, entityName, testEntityID); + // Clean up - delete both entities + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); } @Test - @Order(78) - void testDownloadMultipleAttachmentsInDraftState() throws IOException { + @Order(74) + public void testChainMoveAttachmentsFromSourceToTarget1ToTarget2() throws Exception { System.out.println( - "Test (78): Create entity in draft state, upload 3 attachments (pdf, txt, exe), and" - + " download before saving"); - boolean testStatus = false; + "Test (74): Move attachments from Source Entity to Target Entity 1 and then to Target Entity 2"); - // Step 1: Create entity draft (do NOT save) - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (response.equals("Could not create entity")) { - fail("Could not create entity"); - return; + // Create source entity and add attachments + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); } - String draftEntityID = response; + // Prepare sample files ClassLoader classLoader = getClass().getClassLoader(); + List files = new ArrayList<>(); + files.add(new File(classLoader.getResource("sample.pdf").getFile())); + files.add(new File(classLoader.getResource("sample.txt").getFile())); - // Step 2: Upload pdf, txt, exe while entity remains in draft state Map postData = new HashMap<>(); - postData.put("up__ID", draftEntityID); + postData.put("up__ID", moveSourceEntity); + postData.put("mimeType", "application/pdf"); postData.put("createdAt", new Date().toString()); postData.put("createdBy", "test@test.com"); postData.put("modifiedBy", "test@test.com"); - // Upload pdf - postData.put("mimeType", "application/pdf"); - File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); - List createResponse1 = - api.createAttachment( - appUrl, entityName, facetName, draftEntityID, srvpath, postData, pdfFile); - if (!createResponse1.get(0).equals("Attachment created")) { - api.deleteEntityDraft(appUrl, entityName, draftEntityID); - fail("Could not upload sample.pdf"); - return; + // Create attachments in source entity + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment in source entity"); + } } - String draftAttachmentID1 = createResponse1.get(1); - // Upload txt - postData.put("mimeType", "application/txt"); - File txtFile = new File(classLoader.getResource("sample.txt").getFile()); - List createResponse2 = - api.createAttachment( - appUrl, entityName, facetName, draftEntityID, srvpath, postData, txtFile); - if (!createResponse2.get(0).equals("Attachment created")) { - api.deleteEntityDraft(appUrl, entityName, draftEntityID); - fail("Could not upload sample.txt"); - return; + // Save source entity + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); } - String draftAttachmentID2 = createResponse2.get(1); - // Upload exe - postData.put("mimeType", "application/exe"); - File exeFile = new File(classLoader.getResource("sample.exe").getFile()); - List createResponse3 = - api.createAttachment( - appUrl, entityName, facetName, draftEntityID, srvpath, postData, exeFile); - if (!createResponse3.get(0).equals("Attachment created")) { - api.deleteEntityDraft(appUrl, entityName, draftEntityID); - fail("Could not upload sample.exe"); - return; + // Get count of attachments in source + int sourceCountInitial = sourceAttachmentIds.size(); + assertTrue(sourceCountInitial > 0, "Source should have attachments"); + + // Fetch object IDs from source entity + moveObjectIds.clear(); + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + // Get source folder ID from first attachment + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } } - String draftAttachmentID3 = createResponse3.get(1); - OkHttpClient client = - new OkHttpClient.Builder() - .connectTimeout(120, java.util.concurrent.TimeUnit.SECONDS) - .writeTimeout(120, java.util.concurrent.TimeUnit.SECONDS) - .readTimeout(120, java.util.concurrent.TimeUnit.SECONDS) - .build(); - ObjectMapper objectMapper = new ObjectMapper(); - - // Helper: build the draft download URL for a given first attachment ID - // IsActiveEntity=false targets the draft entity - java.util.function.BiFunction, String> callDraftDownload = - (firstId, ids) -> { - String url = - "https://" - + appUrl - + "/odata/v4/" - + srvpath - + "/" - + entityName - + "(ID=" - + draftEntityID - + ",IsActiveEntity=false)" - + "/" - + facetName - + "(up__ID=" - + draftEntityID - + ",ID=" - + firstId - + ",IsActiveEntity=false)" - + "/" - + srvpath - + ".downloadSelectedAttachments"; - - String idsParam = String.join(",", ids); - String jsonPayload = "{\"ids\": \"" + idsParam + "\"}"; - RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonPayload); - Request req = - new Request.Builder() - .url(url) - .post(body) - .addHeader("Authorization", "Bearer " + token) - .build(); - try (Response resp = client.newCall(req).execute()) { - if (!resp.isSuccessful()) { - throw new RuntimeException( - "Draft download failed: " + resp.code() + " - " + resp.body().string()); - } - String respBody = resp.body().string(); - Map responseMap = objectMapper.readValue(respBody, Map.class); - if (responseMap.containsKey("value")) { - return responseMap.get("value").toString(); - } - return respBody; - } catch (IOException e) { - throw new RuntimeException("Draft download error: " + e.getMessage(), e); - } - }; - - // Step 3: Select first attachment — Download button should be enabled even in draft state - String singleDownloadResult = - callDraftDownload.apply(draftAttachmentID1, List.of(draftAttachmentID1)); - JSONArray singleResultArray = new JSONArray(singleDownloadResult); - assertEquals(1, singleResultArray.length(), "Expected 1 result in download response"); - JSONObject singleResult = singleResultArray.getJSONObject(0); + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); + } + + assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); + + // Create Target Entity 1 + moveTargetEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity 1"); + } + + // Save target1 before move + String saveTarget1BeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity); + if (!saveTarget1BeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity 1 before move"); + } + + // Move attachments from source to Target Entity 1 with sourceFacet + String sourceFacet = serviceName + "." + entityName + "." + facetName; + String targetFacet = serviceName + "." + entityName + "." + facetName; + Map moveResult1 = + api.moveAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult1 == null) { + fail("Move operation from source to target 1 returned null result"); + } + + // Verify attachments moved to Target Entity 1 + List> target1MetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + assertTrue( + target1MetadataAfterMove.size() > 0, "Target entity 1 should have attachments after move"); assertEquals( - "success", - singleResult.getString("status"), - "Download button should be enabled in draft state: single attachment download should" - + " succeed"); - assertTrue(singleResult.has("content"), "Downloaded attachment should have a content field"); - - // Step 4: Select all 3 and download while entity is still in draft state - String multiDownloadResult = - callDraftDownload.apply( - draftAttachmentID1, - List.of(draftAttachmentID1, draftAttachmentID2, draftAttachmentID3)); - JSONArray multiResultArray = new JSONArray(multiDownloadResult); - assertEquals(3, multiResultArray.length(), "Expected 3 results in download response"); - for (int i = 0; i < multiResultArray.length(); i++) { - JSONObject result = multiResultArray.getJSONObject(i); - assertEquals( - "success", - result.getString("status"), - "Attachment " + (i + 1) + " should download successfully in draft state"); + sourceCountInitial, + target1MetadataAfterMove.size(), + "Target 1 should have " + sourceCountInitial + " attachments"); + + // Verify all expected files are in Target Entity 1 + Set target1FileNames = + target1MetadataAfterMove.stream() + .map(m -> (String) m.get("fileName")) + .collect(java.util.stream.Collectors.toSet()); + + for (File file : files) { assertTrue( - result.has("content"), - "Attachment " + (i + 1) + " should have a content field in the response"); + target1FileNames.contains(file.getName()), + "Target 1 should contain attachment: " + file.getName()); } - testStatus = true; - // Clean up — entity was never saved, so delete the draft - api.deleteEntityDraft(appUrl, entityName, draftEntityID); + // Verify attachments removed from source + List> sourceMetadataAfterFirstMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); + assertEquals( + 0, + sourceMetadataAfterFirstMove.size(), + "Source entity should have no attachments after move to target 1"); + + // Create Target Entity 2 + String moveTargetEntity2 = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity2.equals("Could not create entity")) { + fail("Could not create target entity 2"); + } - if (!testStatus) { - fail("Multiple attachment download in draft state test failed"); + // Save target2 before move + String saveTarget2BeforeMoveResponse = + api.saveEntityDraft(appUrl, entityName, srvpath, moveTargetEntity2); + if (!saveTarget2BeforeMoveResponse.equals("Saved")) { + fail("Could not save target entity 2 before move"); + } + + // Get new object IDs and folder ID from Target Entity 1 for second move + List target1AttachmentIds = new ArrayList<>(); + for (Map metadata : target1MetadataAfterMove) { + String attachmentId = metadata.get("ID").toString(); + target1AttachmentIds.add(attachmentId); + } + + moveObjectIds.clear(); + String target1FolderId = null; + for (String attachmentId : target1AttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facetName, moveTargetEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + // Get folder ID from first attachment + if (target1FolderId == null && metadata.containsKey("folderId")) { + target1FolderId = metadata.get("folderId").toString(); + } + } + } catch (IOException e) { + fail("Could not fetch attachment metadata from target 1: " + e.getMessage()); + } + } + + assertNotNull(target1FolderId, "Target 1 folder ID should not be null"); + + // Move attachments from Target Entity 1 to Target Entity 2 with sourceFacet + Map moveResult2 = + api.moveAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity2, + target1FolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult2 == null) { + fail("Move operation from target 1 to target 2 returned null result"); } + + // Verify attachments moved to Target Entity 2 + List> target2MetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity2); + assertTrue( + target2MetadataAfterMove.size() > 0, "Target entity 2 should have attachments after move"); + assertEquals( + sourceCountInitial, + target2MetadataAfterMove.size(), + "Target 2 should have " + sourceCountInitial + " attachments"); + + // Verify all expected files are in Target Entity 2 + Set target2FileNames = + target2MetadataAfterMove.stream() + .map(m -> (String) m.get("fileName")) + .collect(java.util.stream.Collectors.toSet()); + + for (File file : files) { + assertTrue( + target2FileNames.contains(file.getName()), + "Target 2 should contain attachment: " + file.getName()); + } + + // Verify attachments removed from Target Entity 1 + List> target1MetadataAfterSecondMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); + assertEquals( + 0, + target1MetadataAfterSecondMove.size(), + "Target entity 1 should have no attachments after move to target 2"); + + // Clean up - delete all three entities + api.deleteEntity(appUrl, entityName, moveTargetEntity2); + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); } @Test - @Order(79) - void testDownloadButtonWithPdfAndLinkInDraftState() throws IOException { - System.out.println( - "Test (79): Upload pdf and link, save entity, edit entity (draft state)," - + " download button enabled for pdf only, disabled when link also selected"); - - // Step 1: Create entity draft - String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); - if (response.equals("Could not create entity")) { - fail("Could not create entity"); - return; + @Order(75) + public void testMoveAttachmentsWithoutSDMRole() throws Exception { + System.out.println("Test (75): Move attachments when user does not have SDM Role"); + + // Create source entity with SDM role and add attachments + moveSourceEntity = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveSourceEntity.equals("Could not create entity")) { + fail("Could not create source entity"); } - String testEntityID = response; + // Prepare sample files ClassLoader classLoader = getClass().getClassLoader(); + List files = new ArrayList<>(); + files.add(new File(classLoader.getResource("sample.pdf").getFile())); + files.add(new File(classLoader.getResource("sample.txt").getFile())); - // Step 2: Upload one pdf attachment (entity in draft state) Map postData = new HashMap<>(); - postData.put("up__ID", testEntityID); + postData.put("up__ID", moveSourceEntity); postData.put("mimeType", "application/pdf"); postData.put("createdAt", new Date().toString()); postData.put("createdBy", "test@test.com"); postData.put("modifiedBy", "test@test.com"); - File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); - List createResponse = - api.createAttachment( - appUrl, entityName, facetName, testEntityID, srvpath, postData, pdfFile); - if (!createResponse.get(0).equals("Attachment created")) { - api.deleteEntityDraft(appUrl, entityName, testEntityID); - fail("Could not upload sample.pdf"); - return; + // Create attachments in source entity with SDM role + List sourceAttachmentIds = new ArrayList<>(); + for (File file : files) { + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, moveSourceEntity, srvpath, postData, file); + if (createResponse.get(0).equals("Attachment created")) { + sourceAttachmentIds.add(createResponse.get(1)); + } else { + fail("Could not create attachment in source entity"); + } } - // Capture pdf attachment ID directly from upload response (draft state, reliable) - String pdfAttachmentID = createResponse.get(1); - // Step 3: Create a link attachment (entity still in draft state) - String linkResponse = - api.createLink( - appUrl, entityName, facetName, testEntityID, "TestLink", "https://www.example.com"); - if (!linkResponse.equals("Link created successfully")) { - api.deleteEntityDraft(appUrl, entityName, testEntityID); - fail("Could not create link attachment"); - return; + // Save source entity with SDM role + String saveSourceResponse = api.saveEntityDraft(appUrl, entityName, srvpath, moveSourceEntity); + if (!saveSourceResponse.equals("Saved")) { + fail("Could not save source entity: " + saveSourceResponse); } - // Fetch link attachment ID from draft metadata while still in draft state - List> draftAttachments = - api.fetchEntityMetadataDraft(appUrl, entityName, facetName, testEntityID); - String linkAttachmentID = - draftAttachments.stream() - .filter( - a -> "application/internet-shortcut".equalsIgnoreCase((String) a.get("mimeType"))) - .map(a -> (String) a.get("ID")) - .findFirst() - .orElse(null); - if (linkAttachmentID == null) { - api.deleteEntityDraft(appUrl, entityName, testEntityID); - fail("Could not find link attachment in draft entity metadata"); - return; + // Get count of attachments in source + int sourceCountInitial = sourceAttachmentIds.size(); + assertTrue(sourceCountInitial > 0, "Source should have attachments"); + + // Fetch object IDs from source entity + moveObjectIds.clear(); + for (String attachmentId : sourceAttachmentIds) { + try { + Map metadata = + api.fetchMetadata(appUrl, entityName, facetName, moveSourceEntity, attachmentId); + if (metadata.containsKey("objectId")) { + moveObjectIds.add(metadata.get("objectId").toString()); + // Get source folder ID from first attachment + if (moveSourceFolderId == null && metadata.containsKey("folderId")) { + moveSourceFolderId = metadata.get("folderId").toString(); + } + } + } catch (IOException e) { + fail("Could not fetch attachment metadata: " + e.getMessage()); + } } - // Step 4: Save entity - response = api.saveEntityDraft(appUrl, entityName, srvpath, testEntityID); - if (!response.equals("Saved")) { - api.deleteEntityDraft(appUrl, entityName, testEntityID); - fail("Could not save entity draft: " + response); - return; + if (moveObjectIds.size() != sourceAttachmentIds.size()) { + fail("Could not fetch object IDs for all attachments"); } - // Step 5: Edit entity — puts it back into draft state - String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, testEntityID); - if (!editResponse.equals("Entity in draft mode")) { - api.deleteEntity(appUrl, entityName, testEntityID); - fail("Could not put entity into edit/draft mode: " + editResponse); - return; - } + assertNotNull(moveSourceFolderId, "Source folder ID should not be null"); - // Inline draft download helper (IsActiveEntity=false) - OkHttpClient client = - new OkHttpClient.Builder() - .connectTimeout(120, java.util.concurrent.TimeUnit.SECONDS) - .writeTimeout(120, java.util.concurrent.TimeUnit.SECONDS) - .readTimeout(120, java.util.concurrent.TimeUnit.SECONDS) - .build(); - ObjectMapper objectMapper = new ObjectMapper(); - - java.util.function.BiFunction, String> callDraftDownload = - (firstId, ids) -> { - String url = - "https://" - + appUrl - + "/odata/v4/" - + srvpath - + "/" - + entityName - + "(ID=" - + testEntityID - + ",IsActiveEntity=false)" - + "/" - + facetName - + "(up__ID=" - + testEntityID - + ",ID=" - + firstId - + ",IsActiveEntity=false)" - + "/" - + srvpath - + ".downloadSelectedAttachments"; - String idsParam = String.join(",", ids); - String jsonPayload = "{\"ids\": \"" + idsParam + "\"}"; - RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonPayload); - Request req = - new Request.Builder() - .url(url) - .post(body) - .addHeader("Authorization", "Bearer " + token) - .build(); - try (Response resp = client.newCall(req).execute()) { - if (!resp.isSuccessful()) { - throw new RuntimeException( - "Draft download failed: " + resp.code() + " - " + resp.body().string()); - } - String respBody = resp.body().string(); - Map responseMap = objectMapper.readValue(respBody, Map.class); - if (responseMap.containsKey("value")) { - return responseMap.get("value").toString(); - } - return respBody; - } catch (IOException e) { - throw new RuntimeException("Draft download error: " + e.getMessage(), e); - } - }; + // Create target entity with no SDM role + moveTargetEntity = apiNoRoles.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (moveTargetEntity.equals("Could not create entity")) { + fail("Could not create target entity with no SDM role"); + } - // Step 7: Select only pdf — Download button should be enabled (succeeds) - String pdfOnlyResult = callDraftDownload.apply(pdfAttachmentID, List.of(pdfAttachmentID)); - JSONArray pdfOnlyArray = new JSONArray(pdfOnlyResult); - assertEquals(1, pdfOnlyArray.length(), "Expected 1 result when only pdf is selected"); - assertEquals( - "success", - pdfOnlyArray.getJSONObject(0).getString("status"), - "Download button should be enabled in draft state: pdf-only download should succeed"); - - // Step 8: Select pdf + link — Download button should be disabled - // (link attachment returns error status, disabling the download) - String mixedResult = - callDraftDownload.apply(pdfAttachmentID, List.of(pdfAttachmentID, linkAttachmentID)); - JSONArray mixedArray = new JSONArray(mixedResult); - assertEquals(2, mixedArray.length(), "Expected 2 results when pdf and link are selected"); - - JSONObject linkResult = null; - for (int i = 0; i < mixedArray.length(); i++) { - JSONObject item = mixedArray.getJSONObject(i); - if (linkAttachmentID.equals(item.getString("id"))) { - linkResult = item; - break; + // Try to move attachments from source to target using user without SDM role + String sourceFacet = serviceName + "." + entityName + "." + facetName; + String targetFacet = serviceName + "." + entityName + "." + facetName; + Map moveResult = null; + boolean moveOperationFailed = false; + String errorMessage = null; + + try { + moveResult = + apiNoRoles.moveAttachment( + appUrl, + entityName, + facetName, + moveTargetEntity, + moveSourceFolderId, + moveObjectIds, + targetFacet, + sourceFacet); + + if (moveResult == null) { + moveOperationFailed = true; + errorMessage = "Move operation returned null"; + } else if (moveResult.containsKey("error")) { + moveOperationFailed = true; + errorMessage = moveResult.get("error").toString(); } + } catch (Exception e) { + moveOperationFailed = true; + errorMessage = e.getMessage(); } - assertNotNull(linkResult, "Result for link attachment should be present"); + + // Verify move operation failed + assertTrue(moveOperationFailed, "Move operation should fail when user does not have SDM role"); + assertNotNull(errorMessage, "Error message should be present when move operation fails"); + System.out.println("Move operation failed as expected. Error: " + errorMessage); + + // Verify attachments are still in source entity (not moved) + List> sourceMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveSourceEntity); assertEquals( - "error", - linkResult.getString("status"), - "Download button should be disabled in draft state: link attachment should return error"); + sourceCountInitial, + sourceMetadataAfterMove.size(), + "Source should still have all attachments after failed move"); + + // Verify target entity has no attachments + List> targetMetadataAfterMove = + api.fetchEntityMetadata(appUrl, entityName, facetName, moveTargetEntity); assertEquals( - "Download is not supported for link attachments", - linkResult.getString("message"), - "Error message for link attachment download should match"); + 0, targetMetadataAfterMove.size(), "Target should have no attachments after failed move"); - // Clean up - api.deleteEntity(appUrl, entityName, testEntityID); + // Clean up - delete both entities using SDM role + api.deleteEntity(appUrl, entityName, moveTargetEntity); + api.deleteEntity(appUrl, entityName, moveSourceEntity); } // @Test - // @Order(80) + // @Order(76) // void testUploadAttachmentExceedingMaximumFileSize() throws IOException { // System.out.println( // "Test (76) : Upload attachment exceeding maximum file size in references facet"); From a25718fae144fb67b99125ec1e48a2be3186dd44 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Thu, 28 May 2026 20:21:01 +0530 Subject: [PATCH 6/8] Changes suggested by bot --- sdm/src/test/java/integration/com/sap/cds/sdm/Api.java | 1 + sdm/src/test/java/integration/com/sap/cds/sdm/ApiMT.java | 1 + 2 files changed, 2 insertions(+) diff --git a/sdm/src/test/java/integration/com/sap/cds/sdm/Api.java b/sdm/src/test/java/integration/com/sap/cds/sdm/Api.java index a3fdacce0..a16d520de 100644 --- a/sdm/src/test/java/integration/com/sap/cds/sdm/Api.java +++ b/sdm/src/test/java/integration/com/sap/cds/sdm/Api.java @@ -1113,6 +1113,7 @@ public List> fetchEntityMetadataDraft( } } + @Override public String downloadSelectedAttachments( String appUrl, String entityName, String facetName, String entityID, List ids) throws IOException { diff --git a/sdm/src/test/java/integration/com/sap/cds/sdm/ApiMT.java b/sdm/src/test/java/integration/com/sap/cds/sdm/ApiMT.java index a51f8bd7a..99e0fa3b8 100644 --- a/sdm/src/test/java/integration/com/sap/cds/sdm/ApiMT.java +++ b/sdm/src/test/java/integration/com/sap/cds/sdm/ApiMT.java @@ -1051,6 +1051,7 @@ public List> fetchEntityMetadataDraft( } } + @Override public String downloadSelectedAttachments( String appUrl, String entityName, String facetName, String entityID, List ids) throws IOException { From e3239d6bdffc868fa5e4cb63b755b47c046bc692 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Thu, 28 May 2026 22:15:33 +0530 Subject: [PATCH 7/8] Tests in single & multi facet files --- .../sdm/IntegrationTest_MultipleFacet.java | 488 +++++++++++++++ .../cds/sdm/IntegrationTest_SingleFacet.java | 566 ++++++++++++++++++ 2 files changed, 1054 insertions(+) diff --git a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_MultipleFacet.java b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_MultipleFacet.java index 83bcc8909..97920a303 100644 --- a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_MultipleFacet.java +++ b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_MultipleFacet.java @@ -14,6 +14,7 @@ import java.util.stream.Collectors; import okhttp3.*; import okio.ByteString; +import org.json.JSONArray; import org.json.JSONObject; import org.junit.jupiter.api.*; @@ -6864,6 +6865,492 @@ public void testMoveAttachmentsWithoutSDMRole() throws Exception { } } + @Test + @Order(76) + void testDownloadAttachmentsAcrossMultipleFacets() throws IOException { + System.out.println( + "Test (76): Create entity, upload attachments to each facet, and verify" + + " download works across all facets"); + + // Step 1: Create entity + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create entity"); + return; + } + String downloadTestEntityID = response; + + ClassLoader classLoader = getClass().getClassLoader(); + Map postData = new HashMap<>(); + postData.put("up__ID", downloadTestEntityID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + postData.put("mimeType", "application/pdf"); + + // Step 2: Upload one pdf attachment to each of the 3 facets + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + String[] facetAttachmentIDs = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + List createResp = + api.createAttachment( + appUrl, entityName, facet[i], downloadTestEntityID, srvpath, postData, pdfFile); + if (!createResp.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); + fail("Could not upload pdf to facet: " + facet[i]); + return; + } + facetAttachmentIDs[i] = createResp.get(1); + } + + // Step 3: Save entity draft + response = api.saveEntityDraft(appUrl, entityName, srvpath, downloadTestEntityID); + if (!response.equals("Saved")) { + api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); + fail("Could not save entity draft: " + response); + return; + } + + // Step 4: Verify download works from each facet independently + for (int i = 0; i < facet.length; i++) { + String downloadResult = + api.downloadSelectedAttachments( + appUrl, entityName, facet[i], downloadTestEntityID, List.of(facetAttachmentIDs[i])); + JSONArray resultArray = new JSONArray(downloadResult); + assertEquals(1, resultArray.length(), "Expected 1 result from facet: " + facet[i]); + JSONObject result = resultArray.getJSONObject(0); + assertEquals( + "success", result.getString("status"), "Download should succeed for facet: " + facet[i]); + assertTrue( + result.has("content"), + "Downloaded attachment in facet " + facet[i] + " should have a content field"); + } + + // Step 5: Upload 2 more attachments to facet[0] and verify multi-download + List multiIDs = new ArrayList<>(); + multiIDs.add(facetAttachmentIDs[0]); + for (int i = 0; i < 2; i++) { + List extraResp = + api.createAttachment( + appUrl, entityName, facet[0], downloadTestEntityID, srvpath, postData, pdfFile); + if (!extraResp.get(0).equals("Attachment created")) { + api.deleteEntity(appUrl, entityName, downloadTestEntityID); + fail("Could not upload extra attachment to facet: " + facet[0]); + return; + } + multiIDs.add(extraResp.get(1)); + } + String multiResult = + api.downloadSelectedAttachments( + appUrl, entityName, facet[0], downloadTestEntityID, multiIDs); + JSONArray multiArray = new JSONArray(multiResult); + assertEquals(3, multiArray.length(), "Expected 3 results from multi-download in " + facet[0]); + for (int i = 0; i < multiArray.length(); i++) { + assertEquals( + "success", + multiArray.getJSONObject(i).getString("status"), + "Attachment " + (i + 1) + " in " + facet[0] + " should download successfully"); + } + + // Clean up + api.deleteEntity(appUrl, entityName, downloadTestEntityID); + } + + @Test + @Order(77) + void testDownloadButtonDisabledWhenLinkSelectedAcrossFacets() throws IOException { + System.out.println( + "Test (77): Upload pdf to one facet and link to another; verify download" + + " button enabled for pdf facet only, disabled when link-facet item selected"); + + // Step 1: Create entity + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create entity"); + return; + } + String testEntityID = response; + + ClassLoader classLoader = getClass().getClassLoader(); + + // Step 2: Upload pdf to facet[0] (attachments) + Map postData = new HashMap<>(); + postData.put("up__ID", testEntityID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List createResponse = + api.createAttachment( + appUrl, entityName, facet[0], testEntityID, srvpath, postData, pdfFile); + if (!createResponse.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not upload pdf to facet: " + facet[0]); + return; + } + String pdfAttachmentID = createResponse.get(1); + + // Step 3: Create a link in facet[1] (references) + String linkResponse = + api.createLink( + appUrl, entityName, facet[1], testEntityID, "TestLink", "https://www.example.com"); + if (!linkResponse.equals("Link created successfully")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not create link in facet: " + facet[1]); + return; + } + + // Step 4: Save entity + response = api.saveEntityDraft(appUrl, entityName, srvpath, testEntityID); + if (!response.equals("Saved")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not save entity draft: " + response); + return; + } + + // Fetch link attachment ID from facet[1] metadata + List> facet1Attachments = + api.fetchEntityMetadata(appUrl, entityName, facet[1], testEntityID); + String linkAttachmentID = + facet1Attachments.stream() + .filter( + a -> "application/internet-shortcut".equalsIgnoreCase((String) a.get("mimeType"))) + .map(a -> (String) a.get("ID")) + .findFirst() + .orElse(null); + if (linkAttachmentID == null) { + api.deleteEntity(appUrl, entityName, testEntityID); + fail("Could not find link attachment in facet: " + facet[1]); + return; + } + + // Step 5: Download pdf from facet[0] - Download button should be enabled + String pdfOnlyResult = + api.downloadSelectedAttachments( + appUrl, entityName, facet[0], testEntityID, List.of(pdfAttachmentID)); + JSONArray pdfOnlyArray = new JSONArray(pdfOnlyResult); + assertEquals(1, pdfOnlyArray.length(), "Expected 1 result for pdf download from " + facet[0]); + assertEquals( + "success", + pdfOnlyArray.getJSONObject(0).getString("status"), + "Download button should be enabled: pdf in " + facet[0] + " should download successfully"); + + // Step 6: Attempt to download link from facet[1] - Download button should be disabled + String linkResult = + api.downloadSelectedAttachments( + appUrl, entityName, facet[1], testEntityID, List.of(linkAttachmentID)); + JSONArray linkArray = new JSONArray(linkResult); + assertEquals( + 1, linkArray.length(), "Expected 1 result for link download attempt from " + facet[1]); + JSONObject linkItem = linkArray.getJSONObject(0); + assertEquals( + "error", + linkItem.getString("status"), + "Download button should be disabled: link in " + facet[1] + " should return error"); + assertEquals( + "Download is not supported for link attachments", + linkItem.getString("message"), + "Error message for link download in " + facet[1] + " should match"); + + // Clean up + api.deleteEntity(appUrl, entityName, testEntityID); + } + + @Test + @Order(78) + void testDownloadAttachmentsAcrossMultipleFacetsInDraftState() throws IOException { + System.out.println( + "Test (78): Create entity in draft state, upload attachments to each" + + " facet, download before saving"); + + // Step 1: Create entity draft (do NOT save) + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create entity"); + return; + } + String draftEntityID = response; + + ClassLoader classLoader = getClass().getClassLoader(); + Map postData = new HashMap<>(); + postData.put("up__ID", draftEntityID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + postData.put("mimeType", "application/pdf"); + + // Step 2: Upload one pdf to each of the 3 facets (entity stays in draft state) + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + String[] draftFacetAttachmentIDs = new String[facet.length]; + for (int i = 0; i < facet.length; i++) { + List createResp = + api.createAttachment( + appUrl, entityName, facet[i], draftEntityID, srvpath, postData, pdfFile); + if (!createResp.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, draftEntityID); + fail("Could not upload pdf to facet in draft state: " + facet[i]); + return; + } + draftFacetAttachmentIDs[i] = createResp.get(1); + } + + OkHttpClient client = + new OkHttpClient.Builder() + .connectTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .writeTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .readTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .build(); + ObjectMapper objectMapper = new ObjectMapper(); + + // Helper: call draft download for a specific facet and attachment list + java.util.function.BiFunction, String> callDraftDownload = + (facetAndId, ids) -> { + String[] parts = facetAndId.split("\\|"); + String facetName = parts[0]; + String firstId = parts[1]; + String url = + "https://" + + appUrl + + "/odata/v4/" + + srvpath + + "/" + + entityName + + "(ID=" + + draftEntityID + + ",IsActiveEntity=false)" + + "/" + + facetName + + "(up__ID=" + + draftEntityID + + ",ID=" + + firstId + + ",IsActiveEntity=false)" + + "/" + + srvpath + + ".downloadSelectedAttachments"; + String idsParam = String.join(",", ids); + String jsonPayload = "{\"ids\": \"" + idsParam + "\"}"; + RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonPayload); + Request req = + new Request.Builder() + .url(url) + .post(body) + .addHeader("Authorization", "Bearer " + token) + .build(); + try (Response resp = client.newCall(req).execute()) { + if (!resp.isSuccessful()) { + throw new RuntimeException( + "Draft download failed: " + resp.code() + " - " + resp.body().string()); + } + String respBody = resp.body().string(); + Map responseMap = objectMapper.readValue(respBody, Map.class); + if (responseMap.containsKey("value")) { + return responseMap.get("value").toString(); + } + return respBody; + } catch (IOException e) { + throw new RuntimeException("Draft download error: " + e.getMessage(), e); + } + }; + + // Step 3: Verify download works from each facet in draft state + for (int i = 0; i < facet.length; i++) { + String key = facet[i] + "|" + draftFacetAttachmentIDs[i]; + String downloadResult = callDraftDownload.apply(key, List.of(draftFacetAttachmentIDs[i])); + JSONArray resultArray = new JSONArray(downloadResult); + assertEquals( + 1, resultArray.length(), "Expected 1 result from facet in draft state: " + facet[i]); + JSONObject result = resultArray.getJSONObject(0); + assertEquals( + "success", + result.getString("status"), + "Download should succeed in draft state for facet: " + facet[i]); + assertTrue( + result.has("content"), + "Downloaded attachment in draft facet " + facet[i] + " should have a content field"); + } + + // Clean up - entity was never saved, so delete the draft + api.deleteEntityDraft(appUrl, entityName, draftEntityID); + } + + @Test + @Order(79) + void testDownloadButtonWithPdfAndLinkAcrossFacetsInDraftState() throws IOException { + System.out.println( + "Test (79): Upload pdf to one facet and link to another, save entity, edit" + + " (draft state), verify download button enabled for pdf facet, disabled for link" + + " facet"); + + // Step 1: Create entity draft + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create entity"); + return; + } + String testEntityID = response; + + ClassLoader classLoader = getClass().getClassLoader(); + + // Step 2: Upload pdf to facet[0] (attachments) + Map postData = new HashMap<>(); + postData.put("up__ID", testEntityID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List createResponse = + api.createAttachment( + appUrl, entityName, facet[0], testEntityID, srvpath, postData, pdfFile); + if (!createResponse.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not upload pdf to facet: " + facet[0]); + return; + } + String pdfAttachmentID = createResponse.get(1); + + // Step 3: Create a link in facet[1] (references) + String linkResponse = + api.createLink( + appUrl, entityName, facet[1], testEntityID, "TestLink", "https://www.example.com"); + if (!linkResponse.equals("Link created successfully")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not create link in facet: " + facet[1]); + return; + } + + // Fetch link attachment ID from draft metadata of facet[1] + List> draftFacet1Attachments = + api.fetchEntityMetadataDraft(appUrl, entityName, facet[1], testEntityID); + String linkAttachmentID = + draftFacet1Attachments.stream() + .filter( + a -> "application/internet-shortcut".equalsIgnoreCase((String) a.get("mimeType"))) + .map(a -> (String) a.get("ID")) + .findFirst() + .orElse(null); + if (linkAttachmentID == null) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not find link attachment in draft facet: " + facet[1]); + return; + } + + // Step 4: Save entity + response = api.saveEntityDraft(appUrl, entityName, srvpath, testEntityID); + if (!response.equals("Saved")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not save entity draft: " + response); + return; + } + + // Step 5: Edit entity - puts it back into draft state + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, testEntityID); + if (!editResponse.equals("Entity in draft mode")) { + api.deleteEntity(appUrl, entityName, testEntityID); + fail("Could not put entity into edit/draft mode: " + editResponse); + return; + } + + // Inline draft download helper (IsActiveEntity=false) + OkHttpClient client = + new OkHttpClient.Builder() + .connectTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .writeTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .readTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .build(); + ObjectMapper objectMapper = new ObjectMapper(); + + java.util.function.BiFunction, String> callDraftDownload = + (facetAndId, ids) -> { + String[] parts = facetAndId.split("\\|"); + String facetName = parts[0]; + String firstId = parts[1]; + String url = + "https://" + + appUrl + + "/odata/v4/" + + srvpath + + "/" + + entityName + + "(ID=" + + testEntityID + + ",IsActiveEntity=false)" + + "/" + + facetName + + "(up__ID=" + + testEntityID + + ",ID=" + + firstId + + ",IsActiveEntity=false)" + + "/" + + srvpath + + ".downloadSelectedAttachments"; + String idsParam = String.join(",", ids); + String jsonPayload = "{\"ids\": \"" + idsParam + "\"}"; + RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonPayload); + Request req = + new Request.Builder() + .url(url) + .post(body) + .addHeader("Authorization", "Bearer " + token) + .build(); + try (Response resp = client.newCall(req).execute()) { + if (!resp.isSuccessful()) { + throw new RuntimeException( + "Draft download failed: " + resp.code() + " - " + resp.body().string()); + } + String respBody = resp.body().string(); + Map responseMap = objectMapper.readValue(respBody, Map.class); + if (responseMap.containsKey("value")) { + return responseMap.get("value").toString(); + } + return respBody; + } catch (IOException e) { + throw new RuntimeException("Draft download error: " + e.getMessage(), e); + } + }; + + // Step 6: Select pdf from facet[0] in draft state - Download button should be enabled + String pdfOnlyResult = + callDraftDownload.apply(facet[0] + "|" + pdfAttachmentID, List.of(pdfAttachmentID)); + JSONArray pdfOnlyArray = new JSONArray(pdfOnlyResult); + assertEquals( + 1, + pdfOnlyArray.length(), + "Expected 1 result for pdf download from " + facet[0] + " in draft state"); + assertEquals( + "success", + pdfOnlyArray.getJSONObject(0).getString("status"), + "Download button should be enabled in draft state: pdf in " + facet[0] + " should succeed"); + + // Step 7: Select link from facet[1] in draft state - Download button should be disabled + String linkOnlyResult = + callDraftDownload.apply(facet[1] + "|" + linkAttachmentID, List.of(linkAttachmentID)); + JSONArray linkOnlyArray = new JSONArray(linkOnlyResult); + assertEquals( + 1, + linkOnlyArray.length(), + "Expected 1 result for link download attempt from " + facet[1] + " in draft state"); + JSONObject linkItem = linkOnlyArray.getJSONObject(0); + assertEquals( + "error", + linkItem.getString("status"), + "Download button should be disabled in draft state: link in " + + facet[1] + + " should return error"); + assertEquals( + "Download is not supported for link attachments", + linkItem.getString("message"), + "Error message for link download in " + facet[1] + " in draft state should match"); + + // Clean up + api.deleteEntity(appUrl, entityName, testEntityID); + } + // @Test // @Order(76) // void testUploadAttachmentExceedingMaximumFileSize() throws IOException { @@ -6922,4 +7409,5 @@ public void testMoveAttachmentsWithoutSDMRole() throws Exception { // // delete the draft entity // api.deleteEntityDraft(appUrl, entityName, testEntityID); // } + } diff --git a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java index 824b0d134..07bdbb898 100644 --- a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java +++ b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_SingleFacet.java @@ -14,6 +14,7 @@ import java.util.stream.Collectors; import okhttp3.*; import okio.ByteString; +import org.json.JSONArray; import org.json.JSONObject; import org.junit.jupiter.api.*; @@ -6361,6 +6362,571 @@ public void testMoveAttachmentsWithoutSDMRole() throws Exception { api.deleteEntity(appUrl, entityName, moveSourceEntity); } + @Test + @Order(76) + void testDownloadMultipleAttachments() throws IOException { + System.out.println( + "Test (76): Create entity, upload 3 attachments (pdf, txt, exe), and download all"); + boolean testStatus = false; + + // Step 1: Create entity + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create entity"); + return; + } + String downloadTestEntityID = response; + + ClassLoader classLoader = getClass().getClassLoader(); + + // Step 2: Upload pdf, txt, exe in one draft session + Map postData = new HashMap<>(); + postData.put("up__ID", downloadTestEntityID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + // Upload pdf + postData.put("mimeType", "application/pdf"); + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List createResponse1 = + api.createAttachment( + appUrl, entityName, facetName, downloadTestEntityID, srvpath, postData, pdfFile); + if (!createResponse1.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); + fail("Could not upload sample.pdf"); + return; + } + String downloadAttachmentID1 = createResponse1.get(1); + + // Upload txt + postData.put("mimeType", "application/txt"); + File txtFile = new File(classLoader.getResource("sample.txt").getFile()); + List createResponse2 = + api.createAttachment( + appUrl, entityName, facetName, downloadTestEntityID, srvpath, postData, txtFile); + if (!createResponse2.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); + fail("Could not upload sample.txt"); + return; + } + String downloadAttachmentID2 = createResponse2.get(1); + + // Upload exe + postData.put("mimeType", "application/exe"); + File exeFile = new File(classLoader.getResource("sample.exe").getFile()); + List createResponse3 = + api.createAttachment( + appUrl, entityName, facetName, downloadTestEntityID, srvpath, postData, exeFile); + if (!createResponse3.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); + fail("Could not upload sample.exe"); + return; + } + String downloadAttachmentID3 = createResponse3.get(1); + + // Step 3: Save entity draft + response = api.saveEntityDraft(appUrl, entityName, srvpath, downloadTestEntityID); + if (!response.equals("Saved")) { + api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); + fail("Could not save entity draft: " + response); + return; + } + + // Step 4: Select first attachment - Download button should be enabled + // Verify download works with a single attachment selection + String singleDownloadResult = + api.downloadSelectedAttachments( + appUrl, entityName, facetName, downloadTestEntityID, List.of(downloadAttachmentID1)); + JSONArray singleResultArray = new JSONArray(singleDownloadResult); + assertEquals(1, singleResultArray.length(), "Expected 1 result in download response"); + JSONObject singleResult = singleResultArray.getJSONObject(0); + assertEquals( + "success", + singleResult.getString("status"), + "Download button should be enabled: single attachment download should succeed"); + assertTrue(singleResult.has("content"), "Downloaded attachment should have a content field"); + + // Step 5: Select all 3 and click download + String multiDownloadResult = + api.downloadSelectedAttachments( + appUrl, + entityName, + facetName, + downloadTestEntityID, + List.of(downloadAttachmentID1, downloadAttachmentID2, downloadAttachmentID3)); + JSONArray multiResultArray = new JSONArray(multiDownloadResult); + assertEquals(3, multiResultArray.length(), "Expected 3 results in download response"); + for (int i = 0; i < multiResultArray.length(); i++) { + JSONObject result = multiResultArray.getJSONObject(i); + assertEquals( + "success", + result.getString("status"), + "Attachment " + (i + 1) + " should download successfully"); + assertTrue( + result.has("content"), + "Attachment " + (i + 1) + " should have a content field in the response"); + } + testStatus = true; + + // Clean up + api.deleteEntity(appUrl, entityName, downloadTestEntityID); + + if (!testStatus) { + fail("Multiple attachment download test failed"); + } + } + + @Test + @Order(77) + void testDownloadButtonDisabledWhenLinkSelected() throws IOException { + System.out.println( + "Test (77): Download button enabled for pdf only; disabled when link is also selected"); + + // Step 1: Create entity (already in draft mode) + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create entity"); + return; + } + String testEntityID = response; + + ClassLoader classLoader = getClass().getClassLoader(); + + // Step 2: Upload one pdf attachment (entity is already in draft mode) + Map postData = new HashMap<>(); + postData.put("up__ID", testEntityID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, testEntityID, srvpath, postData, pdfFile); + if (!createResponse.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not upload sample.pdf"); + return; + } + String pdfAttachmentID = createResponse.get(1); + + // Step 3: Create a link attachment (entity still in draft mode) + String linkResponse = + api.createLink( + appUrl, entityName, facetName, testEntityID, "TestLink", "https://www.example.com"); + if (!linkResponse.equals("Link created successfully")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not create link attachment"); + return; + } + + // Save entity draft + response = api.saveEntityDraft(appUrl, entityName, srvpath, testEntityID); + if (!response.equals("Saved")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not save entity draft: " + response); + return; + } + + // Fetch metadata to find the link attachment ID (mimeType = "application/internet-shortcut") + List> allAttachments = + api.fetchEntityMetadata(appUrl, entityName, facetName, testEntityID); + String linkAttachmentID = + allAttachments.stream() + .filter( + a -> "application/internet-shortcut".equalsIgnoreCase((String) a.get("mimeType"))) + .map(a -> (String) a.get("ID")) + .findFirst() + .orElse(null); + if (linkAttachmentID == null) { + api.deleteEntity(appUrl, entityName, testEntityID); + fail("Could not find link attachment in entity metadata"); + return; + } + + // Step 4: Select only the pdf - Download button should be enabled (succeeds) + String pdfOnlyResult = + api.downloadSelectedAttachments( + appUrl, entityName, facetName, testEntityID, List.of(pdfAttachmentID)); + JSONArray pdfOnlyArray = new JSONArray(pdfOnlyResult); + assertEquals(1, pdfOnlyArray.length(), "Expected 1 result when only pdf is selected"); + assertEquals( + "success", + pdfOnlyArray.getJSONObject(0).getString("status"), + "Download button should be enabled: pdf-only download should succeed"); + + // Step 5: Select both pdf and link - Download button should be disabled + // (link attachment returns error status, disabling the download) + String mixedResult = + api.downloadSelectedAttachments( + appUrl, + entityName, + facetName, + testEntityID, + List.of(pdfAttachmentID, linkAttachmentID)); + JSONArray mixedArray = new JSONArray(mixedResult); + assertEquals(2, mixedArray.length(), "Expected 2 results when pdf and link are selected"); + + // Find the result for the link attachment and assert it has error status + JSONObject linkResult = null; + for (int i = 0; i < mixedArray.length(); i++) { + JSONObject item = mixedArray.getJSONObject(i); + if (linkAttachmentID.equals(item.getString("id"))) { + linkResult = item; + break; + } + } + assertNotNull(linkResult, "Result for link attachment should be present"); + assertEquals( + "error", + linkResult.getString("status"), + "Download button should be disabled: link attachment download should return error"); + assertEquals( + "Download is not supported for link attachments", + linkResult.getString("message"), + "Error message for link attachment download should match"); + + // Clean up + api.deleteEntity(appUrl, entityName, testEntityID); + } + + @Test + @Order(78) + void testDownloadMultipleAttachmentsInDraftState() throws IOException { + System.out.println( + "Test (78): Create entity in draft state, upload 3 attachments (pdf, txt, exe), and" + + " download before saving"); + boolean testStatus = false; + + // Step 1: Create entity draft (do NOT save) + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create entity"); + return; + } + String draftEntityID = response; + + ClassLoader classLoader = getClass().getClassLoader(); + + // Step 2: Upload pdf, txt, exe while entity remains in draft state + Map postData = new HashMap<>(); + postData.put("up__ID", draftEntityID); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + // Upload pdf + postData.put("mimeType", "application/pdf"); + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List createResponse1 = + api.createAttachment( + appUrl, entityName, facetName, draftEntityID, srvpath, postData, pdfFile); + if (!createResponse1.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, draftEntityID); + fail("Could not upload sample.pdf"); + return; + } + String draftAttachmentID1 = createResponse1.get(1); + + // Upload txt + postData.put("mimeType", "application/txt"); + File txtFile = new File(classLoader.getResource("sample.txt").getFile()); + List createResponse2 = + api.createAttachment( + appUrl, entityName, facetName, draftEntityID, srvpath, postData, txtFile); + if (!createResponse2.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, draftEntityID); + fail("Could not upload sample.txt"); + return; + } + String draftAttachmentID2 = createResponse2.get(1); + + // Upload exe + postData.put("mimeType", "application/exe"); + File exeFile = new File(classLoader.getResource("sample.exe").getFile()); + List createResponse3 = + api.createAttachment( + appUrl, entityName, facetName, draftEntityID, srvpath, postData, exeFile); + if (!createResponse3.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, draftEntityID); + fail("Could not upload sample.exe"); + return; + } + String draftAttachmentID3 = createResponse3.get(1); + + OkHttpClient client = + new OkHttpClient.Builder() + .connectTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .writeTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .readTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .build(); + ObjectMapper objectMapper = new ObjectMapper(); + + // Helper: build the draft download URL for a given first attachment ID + // IsActiveEntity=false targets the draft entity + java.util.function.BiFunction, String> callDraftDownload = + (firstId, ids) -> { + String url = + "https://" + + appUrl + + "/odata/v4/" + + srvpath + + "/" + + entityName + + "(ID=" + + draftEntityID + + ",IsActiveEntity=false)" + + "/" + + facetName + + "(up__ID=" + + draftEntityID + + ",ID=" + + firstId + + ",IsActiveEntity=false)" + + "/" + + srvpath + + ".downloadSelectedAttachments"; + + String idsParam = String.join(",", ids); + String jsonPayload = "{\"ids\": \"" + idsParam + "\"}"; + RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonPayload); + Request req = + new Request.Builder() + .url(url) + .post(body) + .addHeader("Authorization", "Bearer " + token) + .build(); + try (Response resp = client.newCall(req).execute()) { + if (!resp.isSuccessful()) { + throw new RuntimeException( + "Draft download failed: " + resp.code() + " - " + resp.body().string()); + } + String respBody = resp.body().string(); + Map responseMap = objectMapper.readValue(respBody, Map.class); + if (responseMap.containsKey("value")) { + return responseMap.get("value").toString(); + } + return respBody; + } catch (IOException e) { + throw new RuntimeException("Draft download error: " + e.getMessage(), e); + } + }; + + // Step 3: Select first attachment - Download button should be enabled even in draft state + String singleDownloadResult = + callDraftDownload.apply(draftAttachmentID1, List.of(draftAttachmentID1)); + JSONArray singleResultArray = new JSONArray(singleDownloadResult); + assertEquals(1, singleResultArray.length(), "Expected 1 result in download response"); + JSONObject singleResult = singleResultArray.getJSONObject(0); + assertEquals( + "success", + singleResult.getString("status"), + "Download button should be enabled in draft state: single attachment download should" + + " succeed"); + assertTrue(singleResult.has("content"), "Downloaded attachment should have a content field"); + + // Step 4: Select all 3 and download while entity is still in draft state + String multiDownloadResult = + callDraftDownload.apply( + draftAttachmentID1, + List.of(draftAttachmentID1, draftAttachmentID2, draftAttachmentID3)); + JSONArray multiResultArray = new JSONArray(multiDownloadResult); + assertEquals(3, multiResultArray.length(), "Expected 3 results in download response"); + for (int i = 0; i < multiResultArray.length(); i++) { + JSONObject result = multiResultArray.getJSONObject(i); + assertEquals( + "success", + result.getString("status"), + "Attachment " + (i + 1) + " should download successfully in draft state"); + assertTrue( + result.has("content"), + "Attachment " + (i + 1) + " should have a content field in the response"); + } + testStatus = true; + + // Clean up - entity was never saved, so delete the draft + api.deleteEntityDraft(appUrl, entityName, draftEntityID); + + if (!testStatus) { + fail("Multiple attachment download in draft state test failed"); + } + } + + @Test + @Order(79) + void testDownloadButtonWithPdfAndLinkInDraftState() throws IOException { + System.out.println( + "Test (79): Upload pdf and link, save entity, edit entity (draft state)," + + " download button enabled for pdf only, disabled when link also selected"); + + // Step 1: Create entity draft + String response = api.createEntityDraft(appUrl, entityName, entityName2, srvpath); + if (response.equals("Could not create entity")) { + fail("Could not create entity"); + return; + } + String testEntityID = response; + + ClassLoader classLoader = getClass().getClassLoader(); + + // Step 2: Upload one pdf attachment (entity in draft state) + Map postData = new HashMap<>(); + postData.put("up__ID", testEntityID); + postData.put("mimeType", "application/pdf"); + postData.put("createdAt", new Date().toString()); + postData.put("createdBy", "test@test.com"); + postData.put("modifiedBy", "test@test.com"); + + File pdfFile = new File(classLoader.getResource("sample.pdf").getFile()); + List createResponse = + api.createAttachment( + appUrl, entityName, facetName, testEntityID, srvpath, postData, pdfFile); + if (!createResponse.get(0).equals("Attachment created")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not upload sample.pdf"); + return; + } + // Capture pdf attachment ID directly from upload response (draft state, reliable) + String pdfAttachmentID = createResponse.get(1); + + // Step 3: Create a link attachment (entity still in draft state) + String linkResponse = + api.createLink( + appUrl, entityName, facetName, testEntityID, "TestLink", "https://www.example.com"); + if (!linkResponse.equals("Link created successfully")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not create link attachment"); + return; + } + + // Fetch link attachment ID from draft metadata while still in draft state + List> draftAttachments = + api.fetchEntityMetadataDraft(appUrl, entityName, facetName, testEntityID); + String linkAttachmentID = + draftAttachments.stream() + .filter( + a -> "application/internet-shortcut".equalsIgnoreCase((String) a.get("mimeType"))) + .map(a -> (String) a.get("ID")) + .findFirst() + .orElse(null); + if (linkAttachmentID == null) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not find link attachment in draft entity metadata"); + return; + } + + // Step 4: Save entity + response = api.saveEntityDraft(appUrl, entityName, srvpath, testEntityID); + if (!response.equals("Saved")) { + api.deleteEntityDraft(appUrl, entityName, testEntityID); + fail("Could not save entity draft: " + response); + return; + } + + // Step 5: Edit entity - puts it back into draft state + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, testEntityID); + if (!editResponse.equals("Entity in draft mode")) { + api.deleteEntity(appUrl, entityName, testEntityID); + fail("Could not put entity into edit/draft mode: " + editResponse); + return; + } + + // Inline draft download helper (IsActiveEntity=false) + OkHttpClient client = + new OkHttpClient.Builder() + .connectTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .writeTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .readTimeout(120, java.util.concurrent.TimeUnit.SECONDS) + .build(); + ObjectMapper objectMapper = new ObjectMapper(); + + java.util.function.BiFunction, String> callDraftDownload = + (firstId, ids) -> { + String url = + "https://" + + appUrl + + "/odata/v4/" + + srvpath + + "/" + + entityName + + "(ID=" + + testEntityID + + ",IsActiveEntity=false)" + + "/" + + facetName + + "(up__ID=" + + testEntityID + + ",ID=" + + firstId + + ",IsActiveEntity=false)" + + "/" + + srvpath + + ".downloadSelectedAttachments"; + String idsParam = String.join(",", ids); + String jsonPayload = "{\"ids\": \"" + idsParam + "\"}"; + RequestBody body = RequestBody.create(MediaType.parse("application/json"), jsonPayload); + Request req = + new Request.Builder() + .url(url) + .post(body) + .addHeader("Authorization", "Bearer " + token) + .build(); + try (Response resp = client.newCall(req).execute()) { + if (!resp.isSuccessful()) { + throw new RuntimeException( + "Draft download failed: " + resp.code() + " - " + resp.body().string()); + } + String respBody = resp.body().string(); + Map responseMap = objectMapper.readValue(respBody, Map.class); + if (responseMap.containsKey("value")) { + return responseMap.get("value").toString(); + } + return respBody; + } catch (IOException e) { + throw new RuntimeException("Draft download error: " + e.getMessage(), e); + } + }; + + // Step 7: Select only pdf - Download button should be enabled (succeeds) + String pdfOnlyResult = callDraftDownload.apply(pdfAttachmentID, List.of(pdfAttachmentID)); + JSONArray pdfOnlyArray = new JSONArray(pdfOnlyResult); + assertEquals(1, pdfOnlyArray.length(), "Expected 1 result when only pdf is selected"); + assertEquals( + "success", + pdfOnlyArray.getJSONObject(0).getString("status"), + "Download button should be enabled in draft state: pdf-only download should succeed"); + + // Step 8: Select pdf + link - Download button should be disabled + // (link attachment returns error status, disabling the download) + String mixedResult = + callDraftDownload.apply(pdfAttachmentID, List.of(pdfAttachmentID, linkAttachmentID)); + JSONArray mixedArray = new JSONArray(mixedResult); + assertEquals(2, mixedArray.length(), "Expected 2 results when pdf and link are selected"); + + JSONObject linkResult = null; + for (int i = 0; i < mixedArray.length(); i++) { + JSONObject item = mixedArray.getJSONObject(i); + if (linkAttachmentID.equals(item.getString("id"))) { + linkResult = item; + break; + } + } + assertNotNull(linkResult, "Result for link attachment should be present"); + assertEquals( + "error", + linkResult.getString("status"), + "Download button should be disabled in draft state: link attachment should return error"); + assertEquals( + "Download is not supported for link attachments", + linkResult.getString("message"), + "Error message for link attachment download should match"); + + // Clean up + api.deleteEntity(appUrl, entityName, testEntityID); + } + // @Test // @Order(76) // void testUploadAttachmentExceedingMaximumFileSize() throws IOException { From 564047c48c6813ca1775341efe678fd8fd248320 Mon Sep 17 00:00:00 2001 From: PujaDeshmukh17 Date: Mon, 1 Jun 2026 14:43:13 +0530 Subject: [PATCH 8/8] Changes to multiple facet --- .../sdm/IntegrationTest_MultipleFacet.java | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_MultipleFacet.java b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_MultipleFacet.java index 97920a303..2c33c9df1 100644 --- a/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_MultipleFacet.java +++ b/sdm/src/test/java/integration/com/sap/cds/sdm/IntegrationTest_MultipleFacet.java @@ -6926,7 +6926,15 @@ void testDownloadAttachmentsAcrossMultipleFacets() throws IOException { "Downloaded attachment in facet " + facet[i] + " should have a content field"); } - // Step 5: Upload 2 more attachments to facet[0] and verify multi-download + // Step 5: Edit entity back to draft, upload 2 more attachments to facet[0], save, and verify + // multi-download + String editResponse = api.editEntityDraft(appUrl, entityName, srvpath, downloadTestEntityID); + if (!editResponse.equals("Entity in draft mode")) { + api.deleteEntity(appUrl, entityName, downloadTestEntityID); + fail("Could not edit entity back to draft mode: " + editResponse); + return; + } + List multiIDs = new ArrayList<>(); multiIDs.add(facetAttachmentIDs[0]); for (int i = 0; i < 2; i++) { @@ -6934,12 +6942,20 @@ void testDownloadAttachmentsAcrossMultipleFacets() throws IOException { api.createAttachment( appUrl, entityName, facet[0], downloadTestEntityID, srvpath, postData, pdfFile); if (!extraResp.get(0).equals("Attachment created")) { - api.deleteEntity(appUrl, entityName, downloadTestEntityID); + api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); fail("Could not upload extra attachment to facet: " + facet[0]); return; } multiIDs.add(extraResp.get(1)); } + + response = api.saveEntityDraft(appUrl, entityName, srvpath, downloadTestEntityID); + if (!response.equals("Saved")) { + api.deleteEntityDraft(appUrl, entityName, downloadTestEntityID); + fail("Could not save entity draft after extra uploads: " + response); + return; + } + String multiResult = api.downloadSelectedAttachments( appUrl, entityName, facet[0], downloadTestEntityID, multiIDs);